module Laser
  module Analysis
    # Laser representation of a method. This name is tweaked so it doesn't
    # collide with ::Method.
    class LaserMethod
      extend ModuleExtensions
      cattr_accessor_with_default :default_dispatched, true
      attr_reader :name, :proc, :arglist, :owner
      alias arguments arglist
      attr_accessor :arity

      def initialize(name, base_proc)
        @name = name
        @type_instantiations = {}
        @proc = base_proc
        @argument_annotations = nil
        @dispatched = LaserMethod.default_dispatched
        if base_proc  # always true except some ugly test cases
          @arglist = base_proc.arguments
          @arity = Arity.for_arglist(@arglist)
        else
          @arglist = []
          @arity = nil
        end
        yield self if block_given?
      end

      def owner=(mod)
        @owner = mod
        verify_override_safety
      end

      # Returns the method this method overrides: what gets called by `super`.
      def super_method
        owner.parent && owner.parent.instance_method(name)
      end

      ################## Potentially Annotated Properties ######################

      %w(special pure builtin predictable mutation annotated_return annotated_yield_usage
         overloads annotated_raise_frequency raises).each do |attr|
        define_method(attr) do
          instance_variable_get("@#{attr}") || (self.proc ? self.proc.send(attr) : nil)
        end
        define_method("#{attr}=") do |val|
          instance_variable_set("@#{attr}", val)
        end
      end

      def overloads
        @overloads || (self.proc ? self.proc.overloads : {})
      end

      def predictable
        @predictable || (self.proc ? self.proc.predictable : true)
      end

      # Gets all annotations with the same name as an argument.
      def argument_annotations
        @argument_annotations ||=
          if @proc
            arguments.map do |arg|
              @proc.annotations[arg.name] if @proc.annotations.has_key?(arg.name)
            end.compact
          else
            []
          end
      end

      # Combines all known return types for this method into one union type.
      def combined_return_type
        Types::UnionType.new(@type_instantiations.keys.map do |self_type, *argtypes, blk_type|
          return_type_for_types(self_type, argtypes, blk_type)
        end)
      end

      def yield_type
        return annotated_yield_usage if annotated_yield_usage
        return @yield_type if @yield_type
        if builtin
          :ignored
        else
          master_cfg.analyze(method: self)
          @yield_type = master_cfg.yield_type
        end
      end

      def yield_arity
        return @yield_arity if @yield_arity
        master_cfg.analyze(method: self)
        @yield_arity = master_cfg.yield_arity
      end

      def raise_frequency_for_types(self_type, arg_types = [], block_type = nil)
        block_type ||= Types::NILCLASS
        unless arg_types_unify_with_annotations?(arg_types)
          return Frequency::ALWAYS
        end
        return annotated_raise_frequency if annotated_raise_frequency
        return Frequency::MAYBE if builtin || special
        cfg_for_types(self_type, arg_types, block_type).raise_frequency
      end

      def raise_type_for_types(self_type, arg_types = [], block_type = nil)
        block_type ||= Types::NILCLASS
        Laser.debug_puts("Calculating raise type for #{owner.name}##{name} with types "+
                 "#{self_type.inspect} #{arg_types.inspect} #{block_type.inspect}")
        unless arg_types_unify_with_annotations?(arg_types)
          return ClassRegistry['TypeError'].as_type  # needs parameterization later
        end
        if raises
          Laser.debug_puts("Raise type is annotated: #{self.raises.inspect}")
          return self.raises
        end
        if builtin || special
          if annotated_raise_frequency == Frequency::NEVER
            self.raises = Types::EMPTY
            Laser.debug_puts("Raise type is annotated: #{self.raises.inspect}")
            return self.raises
          end
          Laser.debug_puts("Builtin/special: Types::TOP")
          return Types::TOP
        end
        cfg_for_types(self_type, arg_types, block_type).raise_type
      end

      def return_type_for_types(self_type, arg_types = [], block_type = nil)
        block_type ||= Types::NILCLASS
        return annotated_return if annotated_return
        unless overloads.empty?
          return overload_for_arg_types(arg_types)
        end
        if builtin || special
          return Types::TOP
        end
        result = cfg_for_types(self_type, arg_types, block_type).return_type
        check_return_type_against_expectations(result)
        result
      end

      def valid_arity?(num_args)
        @arity ? @arity.include?(num_args) : true
      end

      def overload_for_arg_types(arg_types)
        overloads.each do |overload_args, proc_type|
          # compatible arg count
          if overload_args.size == arg_types.size
            # all types unify?
            if overload_args.zip(arg_types).all? { |spec, concrete| Types.subtype?(concrete, spec) }
              return proc_type.subtypes[1]
            end
          end
        end
        raise TypeError.new("No overload found for #{self.inspect} with arg types #{arg_types.inspect}")
      end

      # Checks if the given argument types correctly unify with any
      # user-requested restrictions.
      def arg_types_unify_with_annotations?(arg_types)
        if argument_annotations.any?
          # Match actual arguments against formal arguments
          formal_assignments = assign_formals(arg_types)
          # Check annotations for formals against actual types
          argument_annotations.each do |note_list|
            note_list.each do |type_annotation|
              expected = type_annotation.type
              given = formal_assignments[type_annotation.name]
              # no given -> optional arg that's left out.
              return false if given && !Types.subtype?(given, expected)
            end
          end
        end
        true
      end

      def check_return_type_against_expectations(return_type)
        if (expectation = Types::EXPECTATIONS[self.name]) &&
            !Types.subtype?(return_type, expectation)
          @proc.ast_node.add_error(ImproperOverrideTypeError.new(
            "All methods named #{self.name} should return a subtype of #{expectation.inspect}",
            @proc.ast_node))
        end
      end

      def verify_override_safety
        overridden = super_method
        return unless overridden
        if OverrideSafetyInfo.warn_on_any_override?(overridden)
          @proc.ast_node.add_error(DangerousOverrideError.new(
            OverrideSafetyInfo.warning_message(overridden, name),
            @proc.ast_node))
        elsif OverrideSafetyInfo.needs_super_on_override?(overridden)
          unless master_cfg.guaranteed_super_on_success?
            @proc.ast_node.add_error(OverrideWithoutSuperError.new(
              OverrideSafetyInfo.warning_message(overridden, name),
              @proc.ast_node))
          end
        end
      end

      def dispatched?
        @dispatched
      end

      def been_used!
        @dispatched = true
      end

      def master_cfg
        @master_cfg ||= @proc.ssa_cfg.tap do |cfg|
          cfg.bind_self_type(Types::ClassType.new(owner.path, :covariant))
        end
      end

      def cfg_for_types(self_type, arg_types = [], block_type = nil)
        block_type ||= Types::NILCLASS
        Laser.debug_puts("Calculating CFG(#{owner.name}##{name}, #{self_type.inspect}, #{arg_types.inspect}, #{block_type.inspect})")
        @type_instantiations[[self_type, *arg_types, block_type]] ||= master_cfg.dup.tap do |cfg|
          cfg.bind_self_type(self_type)
          cfg.bind_formal_types(arg_types)
          cfg.bind_block_type(block_type)
          cfg.analyze(method: self)
        end
      end

      def simulate_with_args(new_self, args, block, opts)
        self_type = Utilities.type_for(new_self)
        formal_types = args.map { |arg| Utilities.type_for(arg) }
        block_type = Utilities.type_for(block)
        cfg_for_types(self_type, formal_types, block_type).dup.simulate(
            args, opts.merge(self: new_self,
                           method: self,
                            block: block,
                      start_block: @proc.start_block))
      end

      # Maps a sequence of objects (one per actual argument) to
      # the corresponding formal argument.
      def assign_formals(actual_objs)
        args = arguments
        result_array = []
        num_required = args.count { |arg| arg.kind == :positional }
        num_optional = actual_objs.size - num_required
        current_arg = 0
        current_actual = 0
        rest = []
        while num_required > 0 || num_optional > 0
          next_arg = args[current_arg]
          if next_arg.kind == :positional
            result_array << [next_arg.name, actual_objs[current_actual]]
            num_required -= 1
            current_actual += 1
            current_arg += 1
          elsif next_arg.kind == :optional && num_optional > 0
            result_array << [next_arg.name, actual_objs[current_actual]]
            num_optional -= 1
            current_actual += 1
            current_arg += 1
          elsif next_arg.kind == :optional && num_optional == 0
            current_arg += 1
          elsif next_arg.kind == :rest && num_optional > 0
            rest << actual_objs[current_actual]
            current_actual += 1
            num_optional -= 1
          elsif next_arg.kind == :rest
            result_array << [next_arg.name, rest]
          end
        end
        Hash[*result_array.flatten(1)]
      end

      def dup
        result = LaserMethod.new(name, proc)
        result.owner = self.owner
        result.arity = self.arity
        result
      end

      def refine_arity(new_arity)
        return new_arity if @arity.nil?
        new_begin = [new_arity.begin, @arity.begin].min
        new_end = [new_arity.end, @arity.end].max
        new_begin..new_end
      end
    end
  end
end

{'repo_name': 'michaeledgar/laser', 'stars': '388', 'repo_language': 'Ruby', 'file_name': 'g.10.34.graphml', 'mime_type': 'text/xml', 'hash': -1948959704019045003, 'source_dataset': 'data'}


<?php
include "../conn/conn.php";

$flag=$_POST[flag];
$filename =$_POST[filename];

$path = "sql/" . $filename;

//$filehandle = fopen($path, "w");

	$mysqlstr = "mysqldump -uroot -phust db_wms >sql/".$_POST[filename];
	//$mysqlstr = "mysqldump -uroot -phust db_wms >D:/AppServ/www/wms/system/sql/".$_POST[filename];

	exec($mysqlstr);

	if(!empty($path) and !is_null($path) and ($flag==1)){
		$filename=basename($path);
		$file=fopen($path,"r");
		header("Content-type:application/octet-stream");
		header("Accept-ranges:bytes");
		header("Accept-length:".filesize($path));
		header("Content-Disposition:attachment;filename=".$filename);
		echo fread($file,filesize($path));
		fclose($file);
		exit;
	}
	echo "<script>alert('数据备份成功!');window.location='../desk.php';</script>";
?>

{'repo_name': 'FeMiner/wms', 'stars': '221', 'repo_language': 'PHP', 'file_name': 'inquire_inout_item.php', 'mime_type': 'text/x-php', 'hash': 4150560706444215031, 'source_dataset': 'data'}


fileFormatVersion: 2
guid: c1eded52540dd0a4988d5d4d76382da9
timeCreated: 1457042024
licenseType: Pro
ShaderImporter:
  defaultTextures: []
  userData:
  assetBundleName:
  assetBundleVariant:

{'repo_name': 'ValveSoftware/openvr', 'stars': '4286', 'repo_language': 'C++', 'file_name': 'openvr_api.json', 'mime_type': 'text/plain', 'hash': -3723654998533173643, 'source_dataset': 'data'}


/*
 * (C) 2007-2017 Alibaba Group Holding Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * See the AUTHORS file for names of contributors.
 *
 */

#ifndef TAIR_ITEM_DATA_INFO
#define TAIR_ITEM_DATA_INFO

#include "log.hpp"
#include "define.hpp"

namespace tair {
class DataBuffer;

typedef struct _item_meta {
    uint16_t magic;
    uint16_t checksum;
    uint16_t keysize; // key size max: 64KB
    uint16_t version;
    uint32_t prefixsize; // prefix size
    uint32_t valsize : 24; // value size
    uint8_t flag; // for extends
    uint32_t cdate; // item create time
    uint32_t mdate; // item last modified time
    uint32_t edate; // expire date

    void encode(DataBuffer *output, bool is_new = false) const;

    bool decode(DataBuffer *input);

    _item_meta &operator=(const _item_meta &rhs) {
        if (this != &rhs) {
            memcpy(this, &rhs, sizeof(*this));
        }
        return *this;
    }

    void log_self() {
        log_debug(
                "meta info of data: keysize[%d], valsize[%d], prefixsize[%d], version[%d], flag[%d], cdate[%d], mdate[%d], edate[%d]",
                keysize, valsize, prefixsize, version, flag, cdate, mdate, edate);
    }
} item_meta_info;

static const size_t ITEM_HEADER_LEN = sizeof(item_meta_info);

typedef struct _ItemData {
    item_meta_info header;
    char m_data[0];
} item_data_info;
}
#endif

{'repo_name': 'alibaba/tair', 'stars': '1579', 'repo_language': 'C++', 'file_name': 'Makefile.am', 'mime_type': 'text/plain', 'hash': -563320275791439575, 'source_dataset': 'data'}


package io.vertx.sqlclient.templates;

/**
 * Mapper for {@link LocalDateTimeDataObject}.
 * NOTE: This class has been automatically generated from the {@link LocalDateTimeDataObject} original class using Vert.x codegen.
 */
@io.vertx.codegen.annotations.VertxGen
public interface LocalDateTimeDataObjectRowMapper extends io.vertx.sqlclient.templates.RowMapper<LocalDateTimeDataObject> {

  @io.vertx.codegen.annotations.GenIgnore
  LocalDateTimeDataObjectRowMapper INSTANCE = new LocalDateTimeDataObjectRowMapper() { };

  @io.vertx.codegen.annotations.GenIgnore
  java.util.stream.Collector<io.vertx.sqlclient.Row, ?, java.util.List<LocalDateTimeDataObject>> COLLECTOR = java.util.stream.Collectors.mapping(INSTANCE::map, java.util.stream.Collectors.toList());

  @io.vertx.codegen.annotations.GenIgnore
  default LocalDateTimeDataObject map(io.vertx.sqlclient.Row row) {
    LocalDateTimeDataObject obj = new LocalDateTimeDataObject();
    Object val;
    int idx;
    if ((idx = row.getColumnIndex("localDateTime")) != -1 && (val = row.getLocalDateTime(idx)) != null) {
      obj.setLocalDateTime((java.time.LocalDateTime)val);
    }
    return obj;
  }
}

{'repo_name': 'eclipse-vertx/vertx-sql-client', 'stars': '610', 'repo_language': 'Java', 'file_name': 'db2.sh', 'mime_type': 'text/x-shellscript', 'hash': 7428158994524035975, 'source_dataset': 'data'}


/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2011-2020 Quake3e project

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/
// load time compiler and execution environment for x86, 32-bit and 64-bit

#include "vm_local.h"
#include "../ui/ui_public.h"
#include "../cgame/cg_public.h"
#include "../game/g_public.h"

#ifdef _WIN32
#include <windows.h>
#endif

#ifdef __FreeBSD__
#include <sys/types.h>
#endif

#ifndef _WIN32
#include <sys/mman.h> // for PROT_ stuff
#endif

/* need this on NX enabled systems (i386 with PAE kernel or
 * noexec32=on x86_64) */
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
#define VM_X86_MMAP
#endif

//#define VM_LOG_SYSCALLS
#define JUMP_OPTIMIZE 0

#if JUMP_OPTIMIZE
#define NUM_PASSES 7
#else
#define NUM_PASSES 3
#endif

#define REGS_OPTIMIZE
#define CONST_OPTIMIZE
#define MACRO_OPTIMIZE
#define MISC_OPTIMIZE

static void *VM_Alloc_Compiled( vm_t *vm, int codeLength, int tableLength );
static void VM_Destroy_Compiled( vm_t *vm );

/*
  -------------
  eax	scratch
  ebx*	dataBase
  ecx	scratch (required for shifts)
  edx	scratch (required for divisions)
  esi*	program stack
  edi*	opstack
  ebp*  current proc stack ( dataBase + program stack )
  -------------
  rax	scratch
  rbx*	dataBase
  rcx	scratch (required for shifts)
  rdx	scratch (required for divisions)
  rsi*	programStack
  rdi*	opstack
  rbp*  current proc stack ( dataBase + program stack )
  r8	instructionPointers
  r9    dataMask
  r12*  systemCall
  r13*  stackBottom
  r14*  opStackTop
  xmm0  scratch
  xmm1  scratch
  xmm2  scratch
  xmm3  scratch
  xmm4  scratch
  xmm5  scratch

  Windows  ABI: you are required to preserve the XMM6-XMM15 registers
  System V ABI: you don't have to preserve any of the XMM registers

  Example how data segment will look like during vmMain execution:
  | .... |
  |------| vm->programStack -=36 (8+12+16) // set by vmMain
  | ???? | +0 - unused, reserved for interpreter
  | ???? | +4 - unused, reserved for interpreter
  |-------
  | arg0 | +8  \
  | arg4 | +12  | - passed arguments, accessible from subroutines
  | arg8 | +16 /
  |------|
  | loc0 | +20 \
  | loc4 | +24  \ - locals, accessible only from local scope
  | loc8 | +28  /
  | lc12 | +32 /
  |------| vm->programStack -= 24 ( 8 + MAX_VMMAIN_CALL_ARGS*4 ) // set by VM_CallCompiled()
  | ???? | +0 - unused, reserved for interpreter
  | ???? | +4 - unused, reserved for interpreter
  | arg0 | +8  \
  | arg1 | +12  \ - passed arguments, accessible from vmMain
  | arg2 | +16  /
  | arg3 | +20 /
  |------| vm->programStack = vm->dataMask + 1 // set by VM_Create()

  jump/call opStack rules:

  1) opStack must be 8 before conditional jump
  2) opStack must be 4 before unconditional jump
  3) opStack must be >=4 before OP_CALL
  4) opStack must remain the same after OP_CALL
  5) you may not jump in/call locations with opStack != 0

*/

#define ISS8(V) ( (V) >= -128 && (V) <= 127 )
#define ISU8(V) ( (V) >= 0 && (V) <= 127 )

#define REWIND(N) { compiledOfs -= (N); instructionOffsets[ ip-1 ] = compiledOfs; };

typedef enum
{
	REG_EAX = 0,
	REG_ECX
} reg_t;

typedef enum
{
	LAST_COMMAND_NONE = 0,
	LAST_COMMAND_MOV_EDI_EAX,
	LAST_COMMAND_MOV_EDI_CONST,
	LAST_COMMAND_MOV_EAX_EDI,
	LAST_COMMAND_MOV_EAX_EDI_CALL,
	LAST_COMMAND_SUB_DI_4,
	LAST_COMMAND_SUB_DI_8,
	LAST_COMMAND_SUB_DI_12,
	LAST_COMMAND_STORE_FLOAT_EDI,
	LAST_COMMAND_STORE_FLOAT_EDI_X87,
	LAST_COMMAND_STORE_FLOAT_EDI_SSE
} ELastCommand;

typedef enum
{
	FUNC_ENTR = 0,
	FUNC_CALL,
	FUNC_SYSC,
	FUNC_FTOL,
	FUNC_BCPY,
	FUNC_NCPY,
	FUNC_PSOF,
	FUNC_OSOF,
	FUNC_BADJ,
	FUNC_ERRJ,
	FUNC_DATA,
	FUNC_LAST
} func_t;

// macro opcode sequences
typedef enum {
	MOP_UNDEF = OP_MAX,
	MOP_ADD4,
	MOP_SUB4,
	MOP_BAND4,
	MOP_BOR4,
	MOP_NCPY,
} macro_op_t;

static	byte     *code;
static	int      compiledOfs;
static	int      *instructionOffsets;
static	intptr_t *instructionPointers;

static  instruction_t *inst = NULL;
static  instruction_t *ci;
static  instruction_t *ni;

static int fp_cw[2] = { 0x0000, 0x0F7F }; // [0] - current value, [1] - round towards zero

static	int	ip, pass;
static	int	lastConst;
static	opcode_t	pop1;

static	ELastCommand	LastCommand;

int		funcOffset[FUNC_LAST];

#ifdef DEBUG_VM
static int	errParam = 0;
#endif

static void ErrJump( void )
{
	Com_Error( ERR_DROP, "program tried to execute code outside VM" );
}

static void BadJump( void )
{
	Com_Error( ERR_DROP, "program tried to execute code at bad location inside VM" );
}

static void BadStack( void )
{
	Com_Error( ERR_DROP, "program tried to overflow program stack" );
}

static void BadOpStack( void )
{
	Com_Error( ERR_DROP, "program tried to overflow opcode stack" );
}

static void BadData( void )
{
#ifdef DEBUG_VM
	Com_Error( ERR_DROP, "program tried to read/write out of data segment at %i", errParam );
#else
	Com_Error( ERR_DROP, "program tried to read/write out of data segment" );
#endif
}

static void (*const errJumpPtr)(void) = ErrJump;
static void (*const badJumpPtr)(void) = BadJump;
static void (*const badStackPtr)(void) = BadStack;
static void (*const badOpStackPtr)(void) = BadOpStack;
static void (*const badDataPtr)(void) = BadData;

static void VM_FreeBuffers( void )
{
	// should be freed in reversed allocation order
	Z_Free( instructionOffsets );
	Z_Free( inst );
}

static const ID_INLINE qboolean HasFCOM( void )
{
#if id386
	return ( CPU_Flags & CPU_FCOM );
#else
	return qtrue; // assume idx64
#endif
}

static const ID_INLINE qboolean HasSSEFP( void )
{
#if id386
	return ( CPU_Flags & CPU_SSE ) && ( CPU_Flags & CPU_SSE2 );
#else
	return qtrue; // assume idx64
#endif
}

static void Emit1( int v )
{
	if ( code )
	{
		code[ compiledOfs ] = v;
	}
	compiledOfs++;

	LastCommand = LAST_COMMAND_NONE;
}

static void Emit4( int v )
{
	Emit1( v & 255 );
	Emit1( ( v >> 8 ) & 255 );
	Emit1( ( v >> 16 ) & 255 );
	Emit1( ( v >> 24 ) & 255 );
}

#if idx64
static void Emit8( int64_t v )
{
	Emit1( ( v >> 0 ) & 255 );
	Emit1( ( v >> 8 ) & 255 );
	Emit1( ( v >> 16 ) & 255 );
	Emit1( ( v >> 24 ) & 255 );
	Emit1( ( v >> 32 ) & 255 );
	Emit1( ( v >> 40 ) & 255 );
	Emit1( ( v >> 48 ) & 255 );
	Emit1( ( v >> 56 ) & 255 );
}
#endif

static void EmitPtr( const void *ptr )
{
#if idx64
	Emit8( (intptr_t)ptr );
#else
	Emit4( (intptr_t)ptr );
#endif
}

static int Hex( int c )
{
	if ( c >= '0' && c <= '9' ) {
		return c - '0';
	}
	if ( c >= 'A' && c <= 'F' ) {
		return 10 + c - 'A';
	}
	if ( c >= 'a' && c <= 'f' ) {
		return 10 + c - 'a';
	}

	VM_FreeBuffers();
	Com_Error( ERR_DROP, "Hex: bad char '%c'", c );

	return 0;
}

static void EmitString( const char *string )
{
	int		c1, c2;
	int		v;

	while ( 1 ) {
		c1 = string[0];
		c2 = string[1];

		v = ( Hex( c1 ) << 4 ) | Hex( c2 );
		Emit1( v );

		if ( !string[2] ) {
			break;
		}
		string += 3;
	}
}

static void EmitRexString( const char *string )
{
#if idx64
	Emit1( 0x48 );
#endif
	EmitString( string );
}

static void EmitAlign( int align )
{
	int i, n;

	n = compiledOfs & ( align - 1 );

	for ( i = 0; i < n ; i++ )
		EmitString( "90" );	// nop
}

static void EmitCommand( ELastCommand command )
{
	switch( command )
	{
		case LAST_COMMAND_MOV_EDI_EAX:
			EmitString( "89 07" );		// mov dword ptr [edi], eax
			break;

		case LAST_COMMAND_MOV_EAX_EDI:
			EmitString( "8B 07" );		// mov eax, dword ptr [edi]
			break;

		case LAST_COMMAND_SUB_DI_4:
			EmitRexString( "83 EF 04" );	// sub edi, 4
			break;

		case LAST_COMMAND_SUB_DI_8:
			EmitRexString( "83 EF 08" );	// sub edi, 8
			break;

		case LAST_COMMAND_SUB_DI_12:
			EmitRexString( "83 EF 0C" );	// sub edi, 12
			break;

		case LAST_COMMAND_STORE_FLOAT_EDI_SSE:
			EmitString( "F3 0F 11 07" );	// movss dword ptr [edi], xmm0
			break;

		case LAST_COMMAND_STORE_FLOAT_EDI_X87:
			EmitString( "D9 1F" );			// fstp dword ptr [edi]
			break;

		case LAST_COMMAND_STORE_FLOAT_EDI:	// meta command
			if ( HasSSEFP() ) {
				EmitString( "F3 0F 11 07" );// movss dword ptr [edi], xmm0
				command = LAST_COMMAND_STORE_FLOAT_EDI_SSE;
			} else {
				EmitString( "D9 1F" );		// fstp dword ptr [edi]
				command = LAST_COMMAND_STORE_FLOAT_EDI_X87;
			}
			break;

		default:
			break;
	}
	LastCommand = command;
}

static void EmitAddEDI4( vm_t *vm )
{
#ifdef REGS_OPTIMIZE
	if ( LastCommand == LAST_COMMAND_NONE )
	{
		EmitRexString( "83 C7 04" );			// add edi,4
		return;
	}

	if ( LastCommand == LAST_COMMAND_SUB_DI_4 ) // sub edi, 4
	{
#if idx64
		REWIND( 4 );
#else
		REWIND( 3 );
#endif
		LastCommand = LAST_COMMAND_NONE;
		return;
	}

	if ( LastCommand == LAST_COMMAND_SUB_DI_8 ) // sub edi, 8
	{
#if idx64
		REWIND( 4 );
#else
		REWIND( 3 );
#endif
		EmitCommand( LAST_COMMAND_SUB_DI_4 );
		return;
	}

	if ( LastCommand == LAST_COMMAND_SUB_DI_12 ) // sub edi, 12
	{
#if idx64
		REWIND( 4 );
#else
		REWIND( 3 );
#endif
		EmitCommand( LAST_COMMAND_SUB_DI_8 );
		return;
	}
#endif // REGS_OPTIMIZE

	EmitRexString( "83 C7 04" );				// add edi,4
}

static void EmitMovEAXEDI( vm_t *vm )
{
	opcode_t pop = pop1;
	pop1 = OP_UNDEF;
#ifdef REGS_OPTIMIZE
	if ( LastCommand == LAST_COMMAND_NONE )
	{
		EmitString( "8B 07" );		// mov eax, dword ptr [edi]
		return;
	}

	if ( LastCommand == LAST_COMMAND_MOV_EAX_EDI )
		return;

	if ( LastCommand == LAST_COMMAND_MOV_EAX_EDI_CALL )
		return;

	if ( LastCommand == LAST_COMMAND_MOV_EDI_EAX ) // mov dword ptr [edi], eax
	{
		REWIND( 2 );
		LastCommand = LAST_COMMAND_NONE;
		return;
	}

	if ( pop == OP_DIVI || pop == OP_DIVU || pop == OP_MULI || pop == OP_MULU ||
		pop == OP_STORE4 || pop == OP_STORE2 || pop == OP_STORE1 )
	{
		return;
	}

	if ( LastCommand == LAST_COMMAND_MOV_EDI_CONST ) // mov dword ptr [edi], 0x12345678
	{
		REWIND( 6 );
		if ( lastConst == 0 ) {
			EmitString( "31 C0" );		// xor eax, eax
		} else {
			EmitString( "B8" );			// mov	eax, 0x12345678
			Emit4( lastConst );
		}
		return;
	}
#endif
	EmitString( "8B 07" );				// mov eax, dword ptr [edi]
}

void EmitMovECXEDI( vm_t *vm )
{
	opcode_t pop = pop1;
	pop1 = OP_UNDEF;
#ifdef REGS_OPTIMIZE
	if ( LastCommand == LAST_COMMAND_NONE )
	{
		EmitString( "8B 0F" );		// mov ecx, dword ptr [edi]
		return;
	}

	if ( LastCommand == LAST_COMMAND_MOV_EAX_EDI_CALL )
	{
		EmitString( "89 C1" );		// mov ecx, eax
		return;
	}

	if ( LastCommand == LAST_COMMAND_MOV_EAX_EDI ) // mov eax, dword ptr [edi]
	{
		REWIND( 2 );
		EmitString( "8B 0F" );		// mov ecx, dword ptr [edi]
		return;
	}

	if ( LastCommand == LAST_COMMAND_MOV_EDI_EAX ) // mov dword ptr [edi], eax
	{
		REWIND( 2 );
		EmitString( "89 C1" );		// mov ecx, eax
		return;
	}

	if (pop == OP_DIVI || pop == OP_DIVU || pop == OP_MULI || pop == OP_MULU ||
		pop == OP_STORE4 || pop == OP_STORE2 || pop == OP_STORE1 )
	{
		EmitString( "89 C1" );		// mov ecx, eax
		return;
	}

	if ( LastCommand == LAST_COMMAND_MOV_EDI_CONST ) // mov dword ptr [edi], 0x12345678
	{
		REWIND( 6 );
		EmitString( "B9" );			// mov ecx, 0x12345678
		Emit4( lastConst );
		return;
	}
#endif
	EmitString( "8B 0F" );			// mov ecx, dword ptr [edi]
}

static void EmitCheckReg( vm_t *vm, instruction_t *ins, int reg, int size )
{
	int n;

#ifdef MISC_OPTIMIZE
	if ( ins->safe ) {
		return;
	}
#endif

	if ( !( vm_rtChecks->integer & 8 ) || vm->forceDataMask ) {
		if ( vm->forceDataMask ) {
			if ( reg == REG_EAX )
				EmitString( "25" ); 	// and eax, 0x12345678
			else
				EmitString( "81 E1" );  // and ecx, 0x12345678
			Emit4( vm->dataMask );
		}
		return;
	}

#ifdef DEBUG_VM
	EmitString( "50" );			// push eax
	EmitRexString( "B8" );		// mov eax, &errParam
	EmitPtr( &errParam );
	EmitString( "C7 00" );		// mov [rax], ip-1
	Emit4( ip-1 );
	EmitString( "58" );			// pop eax
#endif

#if idx64
	if ( reg == REG_EAX )
		EmitString( "44 39 C8" );// cmp eax, r9d // vm->dataMask
	else
		EmitString( "44 39 C9" );// cmp ecx, r9d // vm->dataMask
#else
	if ( reg == REG_EAX )
		EmitString( "3D" );		// cmp eax, 0x12345678
	else
		EmitString( "81 F9" );	// cmp ecx, 0x12345678

	Emit4( vm->dataMask - (size - 1) );
#endif

	// error reporting
	EmitString( "0F 87" );		// ja +errorFunction
	n = funcOffset[FUNC_DATA] - compiledOfs;
	Emit4( n - 6 );
}

static int EmitLoadFloatEDI_SSE( vm_t *vm )
{
	// movss dword ptr [edi], xmm0
#ifdef REGS_OPTIMIZE
	if ( LastCommand == LAST_COMMAND_STORE_FLOAT_EDI_SSE )
	{
		if ( !vm )
			return 1;
		REWIND( 4 );
		LastCommand = LAST_COMMAND_NONE;
		return 1;
	}
#endif
	EmitString( "F3 0F 10 07" ); // movss xmm0, dword ptr [edi]
	return 0;
}

static int EmitLoadFloatEDI_X87( vm_t *vm )
{
	// fstp dword ptr [edi]
#ifdef REGS_OPTIMIZE
	if ( LastCommand == LAST_COMMAND_STORE_FLOAT_EDI_X87 )
	{
		if ( !vm )
			return 1;
		REWIND( 2 );
		LastCommand = LAST_COMMAND_NONE;
		return 1;
	}
#endif
	EmitString( "D9 07" );		// fld dword ptr [edi]
	return 0;
}

static int EmitLoadFloatEDI( vm_t *vm )
{
	if ( HasSSEFP() )
		return EmitLoadFloatEDI_SSE( vm );
	else
		return EmitLoadFloatEDI_X87( vm );
}

#if JUMP_OPTIMIZE
const char *NearJumpStr( int op )
{
	switch ( op )
	{
		case OP_EQF:
		case OP_EQ:  return "74"; // je

		case OP_NEF:
		case OP_NE:  return "75"; // jne

		case OP_LTI: return "7C"; // jl
		case OP_LEI: return "7E"; // jle
		case OP_GTI: return "7F"; // jg
		case OP_GEI: return "7D"; // jge

		case OP_LTF:
		case OP_LTU: return "72"; // jb

		case OP_LEF:
		case OP_LEU: return "76"; // jbe

		case OP_GTF:
		case OP_GTU: return "77"; // ja

		case OP_GEF:
		case OP_GEU: return "73"; // jae

		case OP_JUMP: return "EB";   // jmp

		//default:
		//	Com_Error( ERR_DROP, "Bad opcode %i", op );
	};
	return NULL;
}
#endif

const char *FarJumpStr( int op, int *n )
{
	switch ( op )
	{
		case OP_EQF:
		case OP_EQ:  *n = 2; return "0F 84"; // je

		case OP_NEF:
		case OP_NE:  *n = 2; return "0F 85"; // jne

		case OP_LTI: *n = 2; return "0F 8C"; // jl
		case OP_LEI: *n = 2; return "0F 8E"; // jle
		case OP_GTI: *n = 2; return "0F 8F"; // jg
		case OP_GEI: *n = 2; return "0F 8D"; // jge

		case OP_LTF:
		case OP_LTU: *n = 2; return "0F 82"; // jb

		case OP_LEF:
		case OP_LEU: *n = 2; return "0F 86"; // jbe

		case OP_GTF:
		case OP_GTU: *n = 2; return "0F 87"; // ja

		case OP_GEF:
		case OP_GEU: *n = 2; return "0F 83"; // jae

		case OP_JUMP: *n = 1; return "E9";   // jmp
	};
	return NULL;
}

void EmitJump( vm_t *vm, instruction_t *i, int op, int addr )
{
	const char *str;
	int v, jump_size = 0;

	v = instructionOffsets[ addr ] - compiledOfs;

#if JUMP_OPTIMIZE
	if ( i->njump ) {
		// can happen
		if ( v < -126 || v > 129 ) {
			str = FarJumpStr( op, &jump_size );
			EmitString( str );
			Emit4( v - 4 - jump_size );
			i->njump = 0;
			return;
		}
		EmitString( NearJumpStr( op ) );
		Emit1( v - 2 );
		return;
	}

	if ( pass >= 2 && pass < NUM_PASSES-2 ) {
		if ( v >= -126 && v <= 129 ) {
			EmitString( NearJumpStr( op ) );
			Emit1( v - 2 );
			i->njump = 1;
			return;
		}
	}
#endif

	str = FarJumpStr( op, &jump_size );
	if ( jump_size == 0 ) {
		Com_Error( ERR_DROP, "VM_CompileX86 error: %s\n", "bad jump size" );
	} else {
		EmitString( str );
		Emit4( v - 4 - jump_size );
	}
}

void EmitFloatJump( vm_t *vm, instruction_t *i, int op, int addr )
{
	switch ( op ) {
		case OP_EQF:
			EmitString( "80 E4 40" );	// and ah,0x40
			EmitJump( vm, i, OP_NE, addr );
			break;

		case OP_NEF:
			EmitString( "80 E4 40" );	// and ah,0x40
			EmitJump( vm, i, OP_EQ, addr );
			break;

		case OP_LTF:
			EmitString( "80 E4 01" );	// and ah,0x01
			EmitJump( vm, i, OP_NE, addr );
			break;

		case OP_LEF:
			EmitString( "80 E4 41" );	// and ah,0x41
			EmitJump( vm, i, OP_NE, addr );
			break;

		case OP_GTF:
			EmitString( "80 E4 41" );	// and ah,0x41
			EmitJump( vm, i, OP_EQ, addr );
			break;

		case OP_GEF:
			EmitString( "80 E4 01" );	// and ah,0x01
			EmitJump( vm, i, OP_EQ, addr );
			break;
	};

}

static void EmitCallAddr( vm_t *vm, int addr )
{
	int v;
	v = instructionOffsets[ addr ] - compiledOfs;
	EmitString( "E8" );
	Emit4( v - 5 );
}

static void EmitCallOffset( func_t Func )
{
	int  v;
	v = funcOffset[ Func ] - compiledOfs;
	EmitString( "E8" );		// call +funcOffset[ Func ]
	Emit4( v - 5 );
}

#if idx64
static qboolean above4G( const void *ptr )
{
	if ( (unsigned long long)ptr > 0xFFFFFFFFULL )
		return qtrue;
	else
		return qfalse;
}
#endif

#ifdef _WIN32
#define SHADOW_BASE 40
#else // linux/*BSD ABI
#define SHADOW_BASE 8
#endif

#define PUSH_STACK  32
#define PARAM_STACK 128

static void EmitCallFunc( vm_t *vm )
{
	static int sysCallOffset = 0;
	int n;

	EmitString( "85 C0" );				// test eax, eax
	EmitString( "7C" );					// jl +offset (SystemCall)
	Emit1( sysCallOffset );				// will be valid after first pass
sysCallOffset = compiledOfs;

	// jump target range check
	if ( vm_rtChecks->integer & 4 ) {
		EmitString( "3D" );					// cmp eax, vm->instructionCount
		Emit4( vm->instructionCount );
		EmitString( "0F 83" );				// jae +funcOffset[FUNC_ERRJ]
		n = funcOffset[FUNC_ERRJ] - compiledOfs;
		Emit4( n - 6 );
	}

	EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4

	// save proc base and programStack
	EmitString( "55" );				// push ebp
	EmitString( "56" );				// push esi

	// calling another vm function
#if idx64
	EmitString( "41 FF 14 C0" );		// call dword ptr [r8+rax*8]
#else
	EmitString( "8D 0C 85" );			// lea ecx, [vm->instructionPointers+eax*4]
	EmitPtr( instructionPointers );
	EmitString( "FF 11" );				// call dword ptr [ecx]
#endif

	// restore proc base and programStack so there is
	// no need to validate programStack anymore
	EmitString( "5E" );				// pop esi
	EmitString( "5D" );				// pop ebp

	EmitString( "C3" );				// ret

sysCallOffset = compiledOfs - sysCallOffset;

	// systemCall:
	// convert negative num to system call number
	// and store right before the first arg
	EmitString( "F7 D0" );          // not eax

	EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4

	// we may jump here from ConstOptimize() also
funcOffset[FUNC_SYSC] = compiledOfs;

#if idx64
	// allocate stack for shadow(win32)+parameters
	EmitString( "48 81 EC" );				// sub rsp, 200
	Emit4( SHADOW_BASE + PUSH_STACK + PARAM_STACK );

	// save scratch registers
	EmitString( "48 8D 54 24" );			// lea rdx, [rsp+SHADOW_BASE]
	Emit1( SHADOW_BASE );
	EmitString( "48 89 32" );				// mov [rdx+00], rsi
	EmitString( "48 89 7A 08" );			// mov [rdx+08], rdi
	EmitString( "4C 89 42 10" );			// mov [rdx+16], r8
	EmitString( "4C 89 4A 18" );			// mov [rdx+24], r9

	// ecx = &int64_params[0]
	EmitString( "48 8D 4C 24" );			// lea rcx, [rsp+SHADOW_BASE+PUSH_STACK]
	Emit1( SHADOW_BASE + PUSH_STACK );

	// save syscallNum
	EmitString( "48 89 01" );				// mov [rcx], rax

	// vm->programStack = programStack - 4;
	if ( above4G( &vm->programStack ) ) {
		EmitString( "48 BA" );				// mov rdx, &vm->programStack
		EmitPtr( &vm->programStack );
	} else {
		EmitString( "BA" );					// mov edx, &vm->programStack
		Emit4( (intptr_t)&vm->programStack );
	}
	//EmitString( "8D 46 FC" );				// lea eax, [esi-4]
	EmitString( "8D 46 F8" );				// lea eax, [esi-8]
	EmitString( "89 02" );					// mov [rdx], eax
	//EmitString( "89 32" );				// mov dword ptr [rdx], esi

	// params = (vm->dataBase + programStack + 8);
	EmitString( "48 8D 74 33 08" );			// lea rsi, [rbx+rsi+8]

	// rcx = &int64_params[1]
	EmitString( "48 83 C1 08" );			// add rcx, 8

	// dest_params[1-15] = params[1-15];
	EmitString( "31 D2" );					// xor edx, edx
	// loop
	EmitString( "48 63 04 96" );			// movsxd rax, dword [rsi+rdx*4]
	EmitString( "48 89 04 D1" );			// mov qword ptr[rcx+rdx*8], rax
	EmitString( "48 83 C2 01" );			// add rdx, 1
	EmitString( "48 83 FA" );				// cmp rdx, 15
	Emit1( (PARAM_STACK/8) - 1 );
	EmitString( "7C EE" );					// jl -18

#ifdef _WIN32
	// rcx = &int64_params[0]
	EmitString( "48 83 E9 08" );			// sub rcx, 8
#else // linux/*BSD ABI
	// rdi = &int64_params[0]
	EmitString( "48 8D 79 F8" );			// lea rdi, [rcx-8]
#endif

	// currentVm->systemCall( param );
	EmitString( "41 FF D4" );				// call r12

	// restore registers
	EmitString( "48 8D 54 24" );			// lea rdx, [rsp+SHADOW_BASE]
	Emit1( SHADOW_BASE );
	EmitString( "48 8B 32" );				// mov rsi, [rdx+00]
	EmitString( "48 8B 7A 08" );			// mov rdi, [rdx+08]
	EmitString( "4C 8B 42 10" );			// mov r8,  [rdx+16]
	EmitString( "4C 8B 4A 18" );			// mov r9,  [rdx+24]

	// we added the return value: *(opstack+1) = eax
	EmitAddEDI4( vm );						// add edi, 4
	EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax

	// return stack
	EmitString( "48 81 C4" );				// add rsp, 200
	Emit4( SHADOW_BASE + PUSH_STACK + PARAM_STACK );

	EmitRexString( "8D 2C 33" );			// lea rbp, [rbx+rsi]

	EmitString( "C3" );						// ret

#else // i386

	// params = (int *)((byte *)currentVM->dataBase + programStack + 4);
	EmitString( "8D 4D 04" );				// lea ecx, [ebp+4]

	// function prologue
	EmitString( "55" );						// push ebp
	EmitRexString( "89 E5" );				// mov ebp, esp
	EmitRexString( "83 EC 04" );			// sub esp, 4
	// align stack before call
	EmitRexString( "83 E4 F0" );			// and esp, -16

	// ABI note: esi/edi must not change during call!

	// currentVM->programStack = programStack - 4;
	EmitString( "8D 56 FC" );				// lea edx, [esi-4]
	EmitString( "89 15" );					// mov [&vm->programStack], edx
	EmitPtr( &vm->programStack );

	// params[0] = syscallNum
	EmitString( "89 01" );					// mov [ecx], eax

	// cdecl - set params
	EmitString( "89 0C 24" );				// mov [esp], ecx

	// currentVm->systemCall( param );
	EmitString( "FF 15" );					// call dword ptr [&currentVM->systemCall]
	EmitPtr( &vm->systemCall );

	// we added the return value: *(opstack+1) = eax
#if 0
	EmitAddEDI4( vm );						// add edi, 4
	EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov [edi], eax
#else // break dependency from edi value?
	EmitString( "89 47 04" );				// mov [edi+4], eax
	EmitAddEDI4( vm );						// add edi, 4
#endif

	// function epilogue
	EmitRexString( "89 EC" );				// mov esp, ebp
	EmitString( "5D" );						// pop ebp
	EmitString( "C3" );						// ret
#endif
}

static void EmitFTOLFunc( vm_t *vm )
{
	EmitRexString( "B8" );		// mov eax, &fp_cw[0]
	EmitPtr( &fp_cw[0] );
	EmitString( "9B D9 38" );	// fnstcw word ptr [eax]
	EmitString( "D9 68 04" );	// fldcw word ptr [eax+4]
	EmitString( "DB 1F" );		// fistp dword ptr [edi]
	EmitString( "D9 28" );		// fldcw word ptr [eax]
	EmitString( "C3" );			// ret
}

static void EmitBCPYFunc( vm_t *vm )
{
	// FIXME: range check
	EmitString( "56" );						// push esi
	EmitString( "57" );						// push edi
	EmitString( "8B 37" );					// mov esi,[edi]
	EmitString( "8B 7F FC" );				// mov edi,[edi-4]
	EmitString( "B8" );						// mov eax, datamask
	Emit4( vm->dataMask );
	EmitString( "21 C6" );					// and esi, eax
	EmitString( "21 C7" );					// and edi, eax
#if idx64
	EmitString( "48 01 DE" );				// add rsi, rbx
	EmitString( "48 01 DF" );				// add rdi, rbx
#else
	EmitString( "03 F3" );					// add esi, ebx
	EmitString( "03 FB" );					// add edi, ebx
#endif
	EmitString( "F3 A5" );					// rep movsd
	EmitString( "5F" );						// pop edi
	EmitString( "5E" );						// pop esi
	EmitCommand( LAST_COMMAND_SUB_DI_8 );	// sub edi, 8
	EmitString( "C3" );						// ret
}

static void EmitPSOFFunc( vm_t *vm )
{
	EmitRexString( "B8" );			// mov eax, badStackPtr
	EmitPtr( &badStackPtr );
	EmitString( "FF 10" );			// call [eax]
	EmitString( "C3" );				// ret
}

static void EmitOSOFFunc( vm_t *vm )
{
	EmitRexString( "B8" );			// mov eax, badOptackPtr
	EmitPtr( &badOpStackPtr );
	EmitString( "FF 10" );			// call [eax]
	EmitString( "C3" );				// ret
}

static void EmitBADJFunc( vm_t *vm )
{
	EmitRexString( "B8" );			// mov eax, badJumpPtr
	EmitPtr( &badJumpPtr );
	EmitString( "FF 10" );			// call [eax]
	EmitString( "C3" );				// ret
}

static void EmitERRJFunc( vm_t *vm )
{
	EmitRexString( "B8" );			// mov eax, errJumpPtr
	EmitPtr( &errJumpPtr );
	EmitString( "FF 10" );			// call [eax]
	EmitString( "C3" );				// ret
}

static void EmitDATAFunc( vm_t *vm )
{
	EmitRexString( "B8" );			// mov eax, badDataPtr
	EmitPtr( &badDataPtr );
	EmitString( "FF 10" );			// call [eax]
	EmitString( "C3" );				// ret
}

static void EmitNCPYFunc( vm_t *vm )
{
	static int Lend, Lcopy, Lpadz, Lpop0, Lpop1; // jump labels
	int n;

	//EmitString( "8B 4D 10" );	// mov ecx, dword ptr [ebp+16] // counter
	EmitString( "89 C1" );		// mov ecx, eax // get cached value from previous OP_ARG instruction
	EmitString( "85 C9" );		// test ecx, ecx
	EmitString( "74" );			// je +Lend
	Emit1( Lend );	Lend = compiledOfs;
	EmitString( "57" );			// push edi
	EmitString( "8B 55 0C" );	// mov edx, dword ptr [ebp+12] // source
	EmitString( "8B 7D 08" );	// mov edi, dword ptr [ebp+08] // destination
	EmitRexString( "01 DA" );	// add edx, ebx // + vm->dataBase

#if 0
	if ( vm->forceDataMask )
	{
#if idx64
		EmitString( "44 21 CF" );	// and edi, r9d
#else
		EmitString( "81 E7" );		// and edi, vm->dataMask
		Emit4( vm->dataMask );
#endif
		EmitRexString( "01 DF" );	// add edi, ebx // + vm->dataBase
	}
	else
#endif
	if ( vm_rtChecks->integer & 8 ) // security checks
	{
		EmitString( "89 F8" );		// mov eax, edi
		EmitString( "09 C8" );		// or eax, ecx
		EmitString( "3D" );			// cmp eax, vm->dataMask
		Emit4( vm->dataMask );
		EmitString( "0F 87" );		// ja +errorFunction
		n = funcOffset[FUNC_DATA] - compiledOfs;
		Emit4( n - 6 );
		EmitString( "8D 04 0F" );	// lea eax, dword ptr [edi + ecx]
		EmitRexString( "01 DF" );	// add edi, ebx // + vm->dataBase
		EmitString( "3D" );			// cmp eax, vm->dataMask
		Emit4( vm->dataMask );
		EmitString( "0F 87" );		// ja +errorFunction
		n = funcOffset[FUNC_DATA] - compiledOfs;
		Emit4( n - 6 );
	}
	else
	{
		EmitRexString( "01 DF" );	// add edi, ebx // + vm->dataBase
	}

Lcopy = compiledOfs - Lcopy;
	EmitString( "8A 02" );		// mov al, dword ptr [edx]
	EmitString( "88 07" );		// mov dword ptr [edi], al
	EmitRexString( "83 C2 01" );// add edx, 1
	EmitRexString( "83 C7 01" );// add edi, 1
	EmitRexString( "84 C0" );	// test al, al
	EmitString( "74" );			// je +Lpadz
	Emit1( Lpadz );	Lpadz = compiledOfs;
	EmitString( "83 E9 01" );	// sub ecx, 1
	EmitString( "75" );			// jne +Lcopy
	Emit1( Lcopy );	Lcopy = compiledOfs;
	EmitString( "5F" );			// pop edi
	EmitString( "C3" );			// ret
Lpadz = compiledOfs - Lpadz;
	EmitString( "85 C9" );		// test ecx, ecx
	EmitString( "74" );			// je +Lpop0
	Emit1( Lpop0 );	Lpop0 = compiledOfs;
#if 0
	// zero only one char
	EmitString( "31 C0" );		// xor eax, eax
	EmitString( "88 07" );		// mov dword ptr [edi], al
#else
	// zero all remaining chars
	EmitString( "83 E9 01" );	// sub ecx, 1
	EmitString( "74" );			// je +Lpop1
	Emit1( Lpop1 );	Lpop1 = compiledOfs;
	EmitString( "89 CA" );		// mov edx, ecx
	EmitString( "C1 E9 02" );	// shr ecx, 2
	EmitString( "31 C0" );		// xor eax, eax
	EmitString( "83 E2 03" );	// and edx, 3
	EmitString( "F3 AB" );		// rep stosd
	EmitString( "89 D1" );		// mov ecx, edx
	//EmitString( "83 E1 03" );	// and ecx, 3
	EmitString( "F3 AA" );		// rep stosb
Lpop1 = compiledOfs - Lpop1;
#endif
Lpop0 = compiledOfs - Lpop0;
	EmitString( "5F" );			// pop edi
Lend = compiledOfs - Lend;
	EmitString( "C3" );			// ret
}

/*
=================
EmitFCalcEDI
=================
*/
static void EmitFCalcEDI( int op )
{
	switch ( op )
	{
		case OP_ADDF: EmitString( "D8 07" ); break; // fadd dword ptr [edi]
		case OP_SUBF: EmitString( "D8 27" ); break;	// fsub dword ptr [edi]
		case OP_MULF: EmitString( "D8 0F" ); break; // fmul dword ptr [edi]
		case OP_DIVF: EmitString( "D8 37" ); break; // fdiv dword ptr [edi]
		default: Com_Error( ERR_DROP, "bad float op" ); break;
	};
}

/*
=================
EmitFCalcPop
=================
*/
static void EmitFCalcPop( int op )
{
	switch ( op )
	{
		case OP_ADDF: EmitString( "DE C1" ); break; // faddp
		case OP_SUBF: EmitString( "DE E9" ); break; // fsubp
		case OP_MULF: EmitString( "DE C9" ); break; // fmulp
		case OP_DIVF: EmitString( "DE F9" ); break; // fdivp
		default: Com_Error( ERR_DROP, "bad opcode %02x", op ); break;
	};
}

/*
=================
CommuteFloatOp
=================
*/
static int CommuteFloatOp( int op )
{
	switch ( op ) {
		case OP_LEF: return OP_GEF;
		case OP_LTF: return OP_GTF;
		case OP_GEF: return OP_LEF;
		case OP_GTF: return OP_LTF;
		default: return op;
	}
}

static qboolean IsFloorTrap( const vm_t *vm, const int trap )
{
	if ( trap == ~CG_FLOOR && vm->index == VM_CGAME )
		return qtrue;

	if ( trap == ~UI_FLOOR && vm->index == VM_UI )
		return qtrue;

	if ( trap == ~G_FLOOR && vm->index == VM_GAME )
		return qtrue;

	return qfalse;
}

static qboolean IsCeilTrap( const vm_t *vm, const int trap )
{
	if ( trap == ~CG_CEIL && vm->index == VM_CGAME )
		return qtrue;

	if ( trap == ~UI_CEIL && vm->index == VM_UI )
		return qtrue;

	if ( trap == ~G_CEIL && vm->index == VM_GAME )
		return qtrue;

	return qfalse;
}

/*
=================
ConstOptimize
=================
*/
#ifdef CONST_OPTIMIZE
static qboolean ConstOptimize( vm_t *vm )
{
	int v;
	int op1;
	qboolean sign_extend;

	op1 = ni->op;

	switch ( op1 ) {

	case OP_LOAD4:
		EmitAddEDI4( vm );
		if ( ISS8( ci->value ) ) {
			EmitString( "8B 43" );		// mov eax, dword ptr [ebx+0x7F]
			Emit1( ci->value );
		} else {
			EmitString( "8B 83" );		// mov eax, dword ptr [ebx+0x12345678]
			Emit4( ci->value );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX ); // mov dword ptr [edi], eax
		ip += 1;
		return qtrue;

	case OP_LOAD2:
		EmitAddEDI4( vm );
		sign_extend = ( (ci+2)->op == OP_SEX16 );
		if ( ISS8( ci->value ) ) {
			if ( sign_extend ) {
				EmitString( "0F BF 43" );	// movsx eax, word ptr [ebx+0x7F]
				ip += 1;
			} else {
				EmitString( "0F B7 43" );	// movzx eax, word ptr [ebx+0x7F]
			}
			Emit1( ci->value );
		} else {
			if ( sign_extend ) {
				EmitString( "0F BF 83" );	// movsx eax, word ptr [ebx+0x12345678]
				ip += 1;
			} else {
				EmitString( "0F B7 83" );	// movzx eax, word ptr [ebx+0x12345678]
			}
			Emit4( ci->value );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX ); // mov dword ptr [edi], eax
		ip += 1;
		return qtrue;

	case OP_LOAD1:
		EmitAddEDI4( vm );
		sign_extend = ( (ci+2)->op == OP_SEX8 );
		if ( ISS8( ci->value ) ) {
			if ( sign_extend ) {
				EmitString( "0F BE 43" );	// movsx eax, byte ptr [ebx+0x7F]
				ip += 1;
			} else {
				EmitString( "0F B6 43" );	// movzx eax, byte ptr [ebx+0x7F]
			}
			Emit1( ci->value );
		} else {
			if ( sign_extend ) {
				EmitString( "0F BE 83" );	// movsx eax, word ptr [ebx+0x12345678]
				ip += 1;
			} else {
				EmitString( "0F B6 83" );	// movzx eax, word ptr [ebx+0x12345678]
			}
			Emit4( ci->value );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX ); // mov dword ptr [edi], eax
		ip += 1;
		return qtrue;

	case OP_STORE4:
		EmitMovEAXEDI( vm );
		if ( !ci->value ) {
			EmitString( "31 C9" );		// xor ecx, ecx
		} else {
			EmitString( "B9" );			// mov	ecx, 0x12345678
			Emit4( ci->value );
		}
		EmitCheckReg( vm, ni, REG_EAX, 4 );
		EmitString( "89 0C 03" );		// mov dword ptr [ebx + eax], ecx
		EmitCommand( LAST_COMMAND_SUB_DI_4 );		// sub edi, 4
		ip += 1;
		return qtrue;

	case OP_STORE2:
		EmitMovEAXEDI( vm );
		if ( !ci->value ) {
			EmitString( "31 C9" );		// xor ecx, ecx
		} else {
			EmitString( "B9" );			// mov	ecx, 0x12345678
			Emit4( ci->value );
		}
		EmitCheckReg( vm, ni, REG_EAX, 2 );
		EmitString( "66 89 0C 03" );	// mov word ptr [ebx + eax], cx
		EmitCommand( LAST_COMMAND_SUB_DI_4 ); // sub edi, 4
		ip += 1;
		return qtrue;

	case OP_STORE1:
		EmitMovEAXEDI( vm );
		if ( !ci->value ) {
			EmitString( "31 C9" );		// xor ecx, ecx
		} else {
			EmitString( "B9" );			// mov	ecx, 0x12345678
			Emit4( ci->value );
		}
		EmitCheckReg( vm, ni, REG_EAX, 1 );
		EmitString( "88 0C 03" );		// mov byte ptr [ebx + eax], cl
		EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
		ip += 1;
		return qtrue;

	case OP_ADD:
		v = ci->value;
		EmitMovEAXEDI( vm );
		if ( ISS8( v ) ) {
			EmitString( "83 C0" );	// add eax, 0x7F
			Emit1( v );
		} else {
			EmitString( "05" );		// add eax, 0x12345678
			Emit4( v );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1; // OP_ADD
		return qtrue;

	case OP_SUB:
		v = ci->value;
		EmitMovEAXEDI( vm );
		if ( ISS8( v ) ) {
			EmitString( "83 E8" );	// sub eax, 0x7F
			Emit1( v );
		} else {
			EmitString( "2D" );		// sub eax, 0x12345678
			Emit4( v );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_MULI:
		v = ci->value;
		EmitMovEAXEDI( vm );
		if ( ISS8( v ) ) {
			EmitString( "6B C0" );	// imul eax, 0x7F
			Emit1( v );
		} else {
			EmitString( "69 C0" );	// imul eax, 0x12345678
			Emit4( v );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_MULF:
	case OP_DIVF:
	case OP_ADDF:
	case OP_SUBF:
		v = ci->value;
		EmitLoadFloatEDI( vm );
		if ( HasSSEFP() ) {
			EmitString( "B8" );					// mov eax, 0x12345678
			Emit4( v );
			EmitString( "66 0F 6E C8" );		// movd xmm1, eax
			switch( op1 ) {
				case OP_ADDF: EmitString( "0F 58 C1" ); break;	// addps xmm0, xmm1
				case OP_SUBF: EmitString( "0F 5C C1" ); break;	// subps xmm0, xmm1
				case OP_MULF: EmitString( "0F 59 C1" ); break;	// mulps xmm0, xmm1
				case OP_DIVF: EmitString( "0F 5E C1" ); break;	// divps xmm0, xmm1
			}
		} else {
			EmitString( "C7 45 00" );	// mov dword ptr [ebp], 0x12345678
			Emit4( v );
			EmitString( "D9 45 00" );	// fld dword ptr [ebp]
			EmitFCalcPop( op1 );		// fmulp/fdivp/faddp/fsubp
		}
		EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI );
		ip +=1;
		return qtrue;

	case OP_LSH:
		v = ci->value;
		if ( v < 0 || v > 31 )
			break;
		EmitMovEAXEDI( vm );
		EmitString( "C1 E0" );	// shl eax, 0x12
		Emit1( v );
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1; // OP_LSH
		return qtrue;

	case OP_RSHI:
		v = ci->value;
		if ( v < 0 || v > 31 )
			break;
		EmitMovEAXEDI( vm );
		EmitString( "C1 F8" );	// sar eax, 0x12
		Emit1( v );
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_RSHU:
		v = ci->value;
		if ( v < 0 || v > 31 )
			break;
		EmitMovEAXEDI( vm );
		EmitString( "C1 E8" );	// shr eax, 0x12
		Emit1( v );
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_BAND:
		v = ci->value;
		EmitMovEAXEDI( vm );
		if ( ISU8( v ) ) {
			EmitString( "83 E0" ); // and eax, 0x7F
			Emit1( v );
		} else {
			EmitString( "25" ); // and eax, 0x12345678
			Emit4( v );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_BOR:
		v = ci->value;
		EmitMovEAXEDI( vm );
		if ( ISU8( v ) ) {
			EmitString( "83 C8" ); // or eax, 0x7F
			Emit1( v );
		} else {
			EmitString( "0D" );    // or eax, 0x12345678
			Emit4( v );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_BXOR:
		v = ci->value;
		EmitMovEAXEDI( vm );
		if ( ISU8( v ) ) {
			EmitString( "83 F0" );	// xor eax, 0x7F
			Emit1( v );
		} else {
			EmitString( "35" );		// xor eax, 0x12345678
			Emit4( v );
		}
		EmitCommand( LAST_COMMAND_MOV_EDI_EAX );
		ip += 1;
		return qtrue;

	case OP_JUMP:
		EmitJump( vm, ni, ni->op, ci->value );
		ip += 1; // OP_JUMP
		return qtrue;

	case OP_CALL:
#ifdef VM_LOG_SYSCALLS
		// [dataBase + programStack + 0] = ip;
		EmitString( "C7 45 00" );	// mov dword ptr [ebp], 0x12345678
		Emit4( ip );
#endif
		v = ci->value;
		// try to inline some syscalls
		if ( HasSSEFP() && v == ~TRAP_SQRT ) {
			// inline SSE implementation of sin/cos is too problematic...
			EmitString( "F3 0F 10 45 08" );		// movss xmm0, dword ptr [ebp + 8]
			EmitAddEDI4( vm );
			EmitString( "F3 0F 51 C0" );		// sqrtss xmm0, xmm0
			EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI );
			ip += 1;
			return qtrue;
		} else if ( v == ~TRAP_SIN || v == ~TRAP_COS || v == ~TRAP_SQRT ) {
			EmitString( "D9 45 08" );		// fld dword ptr [ebp + 8]
			switch ( v ) {
				case ~TRAP_SQRT: EmitString( "D9 FA" ); break; // fsqrt
				case ~TRAP_SIN: EmitString( "D9 FE" ); break;  // fsin
				case ~TRAP_COS: EmitString( "D9 FF" ); break;  // fcos
			}
			EmitAddEDI4( vm );								// add edi, 4
			EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI_X87 );// fstp dword ptr[edi]
			ip += 1;
			return qtrue;
		} else if ( IsFloorTrap( vm, v ) && (CPU_Flags & CPU_SSE41) ) {
			EmitString( "F3 0F 10 45 08" );		// movss xmm0, dword ptr [ebp + 8]
			EmitAddEDI4( vm );
			EmitString( "66 0F 3A 0A C0 01" );	// roundss xmm0, xmm0, 1 (exceptions not masked)
			EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI );
			ip += 1;
			return qtrue;
		} else if ( IsCeilTrap( vm, v ) && (CPU_Flags & CPU_SSE41) ) {
			EmitString( "F3 0F 10 45 08" );		// movss xmm0, dword ptr [ebp + 8]
			EmitAddEDI4( vm );
			EmitString( "66 0F 3A 0A C0 02" );	// roundss xmm0, xmm0, 2 (exceptions not masked)
			EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI );
			ip += 1;
			return qtrue;
		}

		if ( v < 0 ) // syscall
		{
			EmitString( "B8" );		// mov eax, 0x12345678
			Emit4( ~v );
			EmitCallOffset( FUNC_SYSC );
			LastCommand = LAST_COMMAND_MOV_EAX_EDI_CALL;
			ip += 1; // OP_CALL
			return qtrue;
		}
		EmitString( "55" );	// push ebp
		EmitString( "56" );	// push rsi
		EmitString( "53" );	// push rbx
		EmitCallAddr( vm, v ); // call +addr
		EmitString( "5B" );	// pop rbx
		EmitString( "5E" );	// pop rsi
		EmitString( "5D" );	// pop ebp
		ip += 1; // OP_CALL
		return qtrue;

	case OP_EQF:
	case OP_NEF:
	case OP_LTF:
	case OP_LEF:
	case OP_GTF:
	case OP_GEF:
		if ( !HasFCOM() )
			return qfalse;
		EmitLoadFloatEDI( vm );
		EmitCommand( LAST_COMMAND_SUB_DI_4 );
		v = ci->value;
		if ( HasSSEFP() ) {
			if ( v == 0 ) {
				EmitString( "0F 57 C9" );			// xorps xmm1, xmm1
			} else {
				EmitString( "B8" );					// mov eax, 0x12345678
				Emit4( v );
				EmitString( "66 0F 6E C8" );		// movd xmm1, eax
			}
			EmitString( "0F 2F C1" );				// comiss xmm0, xmm1
			EmitJump( vm, ni, ni->op, ni->value );
		} else {
			if ( v == 0 ) {
				EmitString( "D9 EE" );		// fldz
			} else {
				EmitString( "C7 45 00" );	// mov [ebp], 0x12345678
				Emit4( v );
				EmitString( "D9 45 00" );	// fld dword ptr [ebp]
			}
			EmitString( "DF E9" );		// fucomip
			EmitString( "DD D8" );		// fstp st(0)
			EmitJump( vm, ni, CommuteFloatOp( ni->op ), ni->value );
		}
		ip +=1;
		return qtrue;

	case OP_EQ:
	case OP_NE:
	case OP_GEI:
	case OP_GTI:
	case OP_GTU:
	case OP_GEU:
	case OP_LTU:
	case OP_LEU:
	case OP_LEI:
	case OP_LTI:
		EmitMovEAXEDI( vm );
		EmitCommand( LAST_COMMAND_SUB_DI_4 );
		v = ci->value;
		if ( v == 0 && ( op1 == OP_EQ || op1 == OP_NE ) ) {
			EmitString( "85 C0" );		// test eax, eax
		} else {
			if ( ISS8( v ) ) {
				EmitString( "83 F8" );	// cmp eax, 0x7F
				Emit1( v );
			} else {
				EmitString( "3D" );		 // cmp eax, 0xFFFFFFFF
				Emit4( v );
			}
		}
		EmitJump( vm, ni, ni->op, ni->value );
		ip += 1;
		return qtrue;

	default:
		break;
	}

	return qfalse;
}
#endif // CONST_OPTIMIZE

/*
=================
VM_FindMOps

Search for known macro-op sequences
=================
*/
#ifdef MACRO_OPTIMIZE
static void VM_FindMOps( instruction_t *buf, int instructionCount )
{
	int n, v, op0;
	instruction_t *i;

	i = buf;
	n = 0;

	while ( n < instructionCount )
	{
		op0 = i->op;
		if ( op0 == OP_LOCAL ) {
			// OP_LOCAL + OP_LOCAL + OP_LOAD4 + OP_CONST + OP_XXX + OP_STORE4
			if ( (i+1)->op == OP_LOCAL && i->value == (i+1)->value && (i+2)->op == OP_LOAD4 && (i+3)->op == OP_CONST && (i+4)->op != OP_UNDEF && (i+5)->op == OP_STORE4 ) {
				v = (i+4)->op;
				if ( v == OP_ADD ) {
					i->op = MOP_ADD4;
					i += 6; n += 6;
					continue;
				}
				if ( v == OP_SUB ) {
					i->op = MOP_SUB4;
					i += 6; n += 6;
					continue;
				}
				if ( v == OP_BAND ) {
					i->op = MOP_BAND4;
					i += 6; n += 6;
					continue;
				}
				if ( v == OP_BOR ) {
					i->op = MOP_BOR4;
					i += 6; n += 6;
					continue;
				}
			}
		} else if ( op0 == OP_CONST && (i+1)->op == OP_CALL && (i+2)->op == OP_POP && i >= buf+6 && (i-1)->op == OP_ARG && !i->jused ) {
			// some void function( arg1, arg2, arg3 )
			if ( i->value == ~TRAP_STRNCPY ) {
				i->op = MOP_NCPY;
				i += 3; n += 3;
				continue;
			}
		}

		i++;
		n++;
	}
}

/*
=================
EmitMOPs
=================
*/
static qboolean EmitMOPs( vm_t *vm, int op )
{
	int v, n;
	switch ( op )
	{
		//[local] += CONST
		case MOP_ADD4:
			n = inst[ip+2].value;
			v = ci->value; // local variable address
			if ( ISS8( n ) ) {
				if ( ISS8( v ) ) {
					EmitString( "83 45" );	// add dword ptr [ebp + 0x7F], 0x12
					Emit1( v );
					Emit1( n );
				} else {
					EmitString( "83 85" );	// add dword ptr [ebp + 0x12345678], 0x12
					Emit4( v );
					Emit1( n );
				}
			} else {
				if ( ISS8( v ) ) {
					EmitString( "81 45" );	// add dword ptr [ebp + 0x7F], 0x12345678
					Emit1( v );
					Emit4( n );
				} else {
					EmitString( "81 85" );	// add dword ptr [ebp + 0x12345678], 0x12345678
					Emit4( v );
					Emit4( n );
				}
			}
			ip += 5;
			return qtrue;

		//[local] -= CONST
		case MOP_SUB4:
			n = inst[ip+2].value;
			v = ci->value; // local variable address
			if ( ISS8( n ) ) {
				if ( ISS8( v ) ) {
					EmitString( "83 6D" );	// sub dword ptr [ebp + 0x7F], 0x12
					Emit1( v );
					Emit1( n );
				} else {
					EmitString( "83 AD" );	// sub dword ptr [ebp + 0x12345678], 0x12
					Emit4( v );
					Emit1( n );
				}
			} else {
				if ( ISS8( v ) ) {
					EmitString( "81 6D" );	// sub dword ptr [ebp + 0x7F], 0x12345678
					Emit1( v );
					Emit4( n );
				} else {
					EmitString( "81 AD" );	// sub dword ptr[esi+0x12345678], 0x12345678
					Emit4( v );
					Emit4( n );
				}
			}
			ip += 5;
			return qtrue;

		//[local] &= CONST
		case MOP_BAND4:
			n = inst[ip+2].value;
			v = ci->value; // local variable address
			if ( ISS8( n ) ) {
				if ( ISS8( v ) ) {
					EmitString( "83 65" );	// and dword ptr [ebp + 0x7F], 0x12
					Emit1( v );
					Emit1( n );
				} else {
					EmitString( "83 A5" );	// and dword ptr [ebp + 0x12345678], 0x12
					Emit4( v );
					Emit1( n );
				}
			} else {
				if ( ISS8( v ) ) {
					EmitString( "81 65" );	// and dword ptr [ebp + 0x7F], 0x12345678
					Emit1( v );
					Emit4( n );
				} else {
					EmitString( "81 A5" );	// and dword ptr [ebp + 0x12345678], 0x12345678
					Emit4( v );
					Emit4( n );
				}
			}
			ip += 5;
			return qtrue;

		//[local] |= CONST
		case MOP_BOR4:
			n = inst[ip+2].value;
			v = ci->value; // local variable address
			if ( ISS8( n ) ) {
				if ( ISS8( v ) ) {
					EmitString( "83 4D" );	// or dword ptr [ebp + 0x7F], 0x12
					Emit1( v );
					Emit1( n );
				} else {
					EmitString( "83 8D" );	// or dword ptr [ebp + 0x12345678], 0x12
					Emit4( v );
					Emit1( n );
				}
			} else {
				if ( ISS8( v ) ) {
					EmitString( "81 4D" );	// or dword ptr [ebp + 0x7F], 0x12345678
					Emit1( v );
					Emit4( n );
				} else {
					EmitString( "81 8D" );	// or dword ptr [ebp + 0x12345678], 0x12345678
					Emit4( v );
					Emit4( n );
				}
			}
			ip += 5;
			return qtrue;

		// const + call + pop
		case MOP_NCPY:
			EmitCallOffset( FUNC_NCPY );
			ip += 2;
			return qtrue;

	};
	return qfalse;
}
#endif // MACRO_OPTIMIZE

/*
=================
VM_Compile
=================
*/
qboolean VM_Compile( vm_t *vm, vmHeader_t *header ) {
	const char	*errMsg;
	int		instructionCount;
	int		proc_base;
	int		proc_len;
	int		i, n, v;
	qboolean	wantres;

	inst = (instruction_t*)Z_Malloc( (header->instructionCount + 8 ) * sizeof( instruction_t ) );
	instructionOffsets = (int*)Z_Malloc( header->instructionCount * sizeof( int ) );

	errMsg = VM_LoadInstructions( (byte *) header + header->codeOffset, header->codeLength, header->instructionCount, inst );
	if ( !errMsg ) {
		errMsg = VM_CheckInstructions( inst, vm->instructionCount, vm->jumpTableTargets, vm->numJumpTableTargets, vm->exactDataLength );
	}
	if ( errMsg ) {
		VM_FreeBuffers();
		Com_Printf( "VM_CompileX86 error: %s\n", errMsg );
		return qfalse;
	}

	VM_ReplaceInstructions( vm, inst );

#ifdef MACRO_OPTIMIZE
	VM_FindMOps( inst, vm->instructionCount );
#endif

	code = NULL; // we will allocate memory later, after last defined pass
	instructionPointers = NULL;

	memset( funcOffset, 0, sizeof( funcOffset ) );

	instructionCount = header->instructionCount;

	for( pass = 0; pass < NUM_PASSES; pass++ )
	{
__compile:
	pop1 = OP_UNDEF;
	lastConst = 0;

	// translate all instructions
	ip = 0;
	compiledOfs = 0;
	LastCommand = LAST_COMMAND_NONE;

	proc_base = -1;
	proc_len = 0;

#if idx64

	EmitString( "53" );				// push rbx
	EmitString( "56" );				// push rsi
	EmitString( "57" );				// push rdi
	EmitString( "55" );				// push rbp
	EmitString( "41 54" );			// push r12
	EmitString( "41 55" );			// push r13
	EmitString( "41 56" );			// push r14
	EmitString( "41 57" );			// push r15

	if ( above4G( vm->dataBase ) ) {
		EmitString( "48 BB" );		// mov rbx, vm->dataBase
		EmitPtr( vm->dataBase );
	} else {
		EmitString( "BB" );			// mov ebx, vm->dataBase
		Emit4( (intptr_t)vm->dataBase );
	}

	EmitString( "49 B8" );			// mov r8, vm->instructionPointers
	EmitPtr( instructionPointers );

	EmitString( "41 B9" );			// mov r9d, vm->dataMask
	Emit4( vm->dataMask );

	EmitString( "41 BD" );			// mov r13d, vm->stackBottom
	Emit4( vm->stackBottom );

	if ( above4G( &vm->opStack ) ) {
		EmitString( "48 B8" );		// mov rax, &vm->opStack
		EmitPtr( &vm->opStack );
	} else {
		EmitString( "B8" );			// mov eax, &vm->opStack
		Emit4( (intptr_t)&vm->opStack );
	}
	EmitRexString( "8B 38" );		// mov rdi, [rax]

	if ( above4G( vm->systemCall ) ) {
		EmitString( "49 BC" );		// mov r12, &vm->systemCall
		EmitPtr( vm->systemCall );
	} else {
		EmitString( "41 BC" );		// mov r12d, &vm->systemCall
		Emit4( (intptr_t)vm->systemCall );
	}

	if ( above4G( &vm->programStack ) ) {
		EmitString( "48 B8" );		// mov rax, &vm->programStack
		EmitPtr( &vm->programStack );
	} else {
		EmitString( "B8" );			// mov eax, &vm->programStack
		Emit4( (intptr_t)&vm->programStack );
	}
	EmitString( "8B 30" );			// mov esi, dword ptr [rax]

	//EmitString( "48 B8" );		// mov rax, &vm->opStackTop
	//EmitPtr( &vm->opStackTop );
	//EmitString( "4C 8B 30" );		// mov r14, [rax]

	EmitString( "4C 8D B7" );		// lea r14, [opStack + opStackSize - 1]
	Emit4( sizeof( int ) * MAX_OPSTACK_SIZE - 1 );

#else  // id386

	EmitString( "60" );				// pushad

	EmitString( "BB" );				// mov ebx, vm->dataBase
	EmitPtr( vm->dataBase );

	EmitString( "8B 35" );			// mov esi, [vm->programStack]
	EmitPtr( &vm->programStack );

	EmitString( "8B 3D" );			// mov edi, [vm->opStack]
	EmitPtr( &vm->opStack );

#endif

	EmitCallOffset( FUNC_ENTR );

#if idx64

#ifdef DEBUG_VM
	EmitRexString( "B8" );			// mov rax, &vm->programStack
	EmitPtr( &vm->programStack );
	EmitString( "89 30" );			// mov [rax], esi
#endif

	if ( above4G( &vm->opStack ) ) {
		EmitString( "48 B8" );		// mov rax, &vm->opStack
		EmitPtr( &vm->opStack );
	} else {
		EmitString( "B8" );			// mov eax, &vm->opStack
		Emit4( (intptr_t)&vm->opStack );
	}
	EmitRexString( "89 38" );		// mov [rax], rdi

	EmitString( "41 5F" );			// pop r15
	EmitString( "41 5E" );			// pop r14
	EmitString( "41 5D" );			// pop r13
	EmitString( "41 5C" );			// pop r12
	EmitString( "5D" );				// pop rbp
	EmitString( "5F" );				// pop rdi
	EmitString( "5E" );				// pop rsi
	EmitString( "5B" );				// pop rbx

#else // id386

#ifdef DEBUG_VM
	EmitString( "89 35" );			// [vm->programStack], esi
	EmitPtr( &vm->programStack );
#endif

	EmitString( "89 3D" );			// [vm->opStack], edi
	EmitPtr( &vm->opStack );

	EmitString( "61" );				// popad
#endif

	EmitString( "C3" );				// ret

	EmitAlign( 4 );

	// main function entry offset
	funcOffset[FUNC_ENTR] = compiledOfs;

	while ( ip < instructionCount )
	{
		instructionOffsets[ ip ] = compiledOfs;

		ci = &inst[ ip ];
		ni = &inst[ ip + 1 ];
		ip++;

		if ( ci->jused ) {
			// we can safely perform rewind-optimizations only in case if
			// we are 100% sure that current instruction is not a jump label
			LastCommand = LAST_COMMAND_NONE;
			pop1 = OP_UNDEF;
		}

		switch ( ci->op ) {

		case OP_UNDEF:
			break;

		case OP_IGNORE:
			ip += ci->value;
			break;

		case OP_BREAK:
			EmitString( "CC" );		// int 3
			break;

		case OP_ENTER:
			v = ci->value;
			if ( ISU8( v ) ) {
				EmitString( "83 EE" );		// sub	esi, 0x12
				Emit1( v );
			} else {
				EmitString( "81 EE" );		// sub	esi, 0x12345678
				Emit4( v );
			}

			proc_base = ip;

			// locate endproc
			for ( proc_len = -1, i = ip; i < instructionCount; i++ ) {
				if ( inst[ i ].op == OP_PUSH && inst[ i + 1 ].op == OP_LEAVE ) {
					proc_len = i - proc_base;
					break;
				}
			}

			// should never happen because equal check in VM_CheckInstructions() but anyway
			if ( proc_len == -1 ) {
				VM_FreeBuffers();
				Com_Printf( "VM_CompileX86 error: %s\n", "missing proc end" );
				return qfalse;
			}

			// programStack overflow check
			if ( vm_rtChecks->integer & 1 ) {
#if idx64
				//EmitString( "4C 39 EE" );		// cmp	rsi, r13
				EmitString( "44 39 EE" );		// cmp	esi, r13d
#else
				EmitString( "81 FE" );			// cmp	esi, vm->stackBottom
				Emit4( vm->stackBottom );
#endif
				EmitString( "0F 8C" );			// jl +funcOffset[FUNC_PSOF]
				n = funcOffset[FUNC_PSOF] - compiledOfs;
				Emit4( n - 6 );
			}

			// opStack overflow check
			if ( vm_rtChecks->integer & 2 ) {
				if ( ISU8( ci->opStack ) ) {
					EmitRexString( "8D 47" );		// lea eax, [edi+0x7F]
					Emit1( ci->opStack );
				} else {
					EmitRexString( "8D 87" );		// lea eax, [edi+0x12345678]
					Emit4( ci->opStack );
				}
#if idx64
				EmitString( "4C 39 F0" );		// cmp rax, r14
#else
				EmitString( "3B 05" );			// cmp eax, [&vm->opStackTop]
				EmitPtr( &vm->opStackTop );
#endif
				EmitString( "0F 87" );			// ja +funcOffset[FUNC_OSOF]
				n = funcOffset[FUNC_OSOF] - compiledOfs;
				Emit4( n - 6 );
			}

			EmitRexString( "8D 2C 33" );		// lea ebp, [ebx+esi]
			break;

		case OP_CONST:
#ifdef CONST_OPTIMIZE
			if ( ConstOptimize( vm ) )
				break;
#endif
			EmitAddEDI4( vm );					// add edi, 4
			EmitString( "C7 07" );				// mov dword ptr [edi], 0x12345678
			lastConst = ci->value;
			Emit4( lastConst );
			LastCommand = LAST_COMMAND_MOV_EDI_CONST;
			break;

		case OP_LOCAL:
			// optimization: merge OP_LOCAL + OP_LOAD4
#ifdef MISC_OPTIMIZE
			if ( ni->op == OP_LOAD4 ) {
				EmitAddEDI4( vm );				// add edi, 4
				v = ci->value;
				if ( ISU8( v ) ) {
					EmitString( "8B 45" );		// mov eax, dword ptr [ebp + 0x7F]
					Emit1( v );
				} else {
					EmitString( "8B 85" );		// mov eax, dword ptr [ebp + 0x12345678]
					Emit4( v );
				}
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX ); // mov dword ptr [edi], eax
				ip++;
				break;
			}

			// optimization: merge OP_LOCAL + OP_LOAD2
			if ( ni->op == OP_LOAD2 ) {
				EmitAddEDI4( vm );				// add edi, 4
				v = ci->value;
				if ( ISU8( v ) ) {
					EmitString( "0F B7 45" );	// movzx eax, word ptr [ebp + 0x7F]
					Emit1( v );
				} else {
					EmitString( "0F B7 85" );	// movzx eax, word ptr [ebp + 0x12345678]
					Emit4( v );
				}
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX ); // mov dword ptr [edi], eax
				ip++;
				break;
			}

			// optimization: merge OP_LOCAL + OP_LOAD1
			if ( ni->op == OP_LOAD1 ) {
				EmitAddEDI4( vm );				// add edi, 4
				v = ci->value;
				if ( ISU8( v ) ) {
					EmitString( "0F B6 45" );	// movzx eax, byte ptr [ebp + 0x7F]
					Emit1( v );
				} else {
					EmitString( "0F B6 85" );	// movzx eax, byte ptr [ebp + 0x12345678]
					Emit4( v );
				}
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX ); // mov dword ptr [edi], eax
				ip++;
				break;
			}
#endif // MISC_OPTIMIZE

			// TODO: i = j + k;
			// TODO: i = j - k;

			EmitAddEDI4( vm );					// add edi, 4
			v = ci->value;
			if ( ISU8( v ) ) {
				EmitString( "8D 46" );			// lea eax, [esi + 0x7F]
				Emit1( v );
			} else {
				EmitString( "8D 86" );			// lea eax, [esi + 0x12345678]
				Emit4( v );
			}
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			break;

		case OP_ARG:
#ifdef MISC_OPTIMIZE
			if ( LastCommand == LAST_COMMAND_STORE_FLOAT_EDI_SSE ) {
				REWIND(4);
				v = ci->value;
				if ( ISU8( v ) ) {
					EmitString( "F3 0F 11 45" ); // movss dword ptr [ebp + 0x7F], xmm0
					Emit1( v );
				} else {
					EmitString( "F3 0F 11 85" ); // movss dword ptr [ebp + 0x12345678], xmm0
					Emit4( v );
				}
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				break;
			}
#endif
			EmitMovEAXEDI( vm );					// mov	eax, dword ptr [edi]
#ifdef MACRO_OPTIMIZE
			if ( (ci+1)->op == MOP_NCPY && !(ci+1)->jused ) {
				// we will read counter from eax
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				break;
			}
#endif
			v = ci->value;
			if ( ISU8( v ) ) {
				EmitString( "89 45" );				// mov	dword ptr [ebp + 0x7F], eax
				Emit1( v );
			} else {
				EmitString( "89 85" );				// mov	dword ptr [ebp + 0x12345678], eax
				Emit4( v );
			}
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_CALL:
#ifdef VM_LOG_SYSCALLS
			// [dataBase + programStack + 0] = ip-1;
			EmitString( "C7 45 00" );				// mov dword ptr [ebp], 0x12345678
			Emit4( ip-1 );
#endif
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitCallOffset( FUNC_CALL );			// call +FUNC_CALL
			break;

		case OP_PUSH:
			EmitAddEDI4( vm );						// add edi, 4
			if ( ni->op == OP_LEAVE ) {
				proc_base = -1;
			}
			break;

		case OP_POP:
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_LEAVE:
#ifdef DEBUG_VM
			v = ci->value;
			if ( ISU8( v ) ) {
				EmitString( "83 C6" );		// add	esi, 0x12
				Emit1( v );
			} else {
				EmitString( "81 C6" );		// add	esi, 0x12345678
				Emit4( v );
			}
#endif
			EmitString( "C3" );							// ret
			break;

		case OP_LOAD4:
#ifdef MISC_OPTIMIZE
			if ( LastCommand == LAST_COMMAND_MOV_EDI_EAX ) {
				REWIND( 2 );
				EmitCheckReg( vm, ci, REG_EAX, 4 );		// range check eax
				EmitString( "8B 04 03" );				// mov	eax, dword ptr [ebx + eax]
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
				break;
			}
#endif
			EmitMovECXEDI( vm );						// mov ecx, dword ptr [edi]
			EmitCheckReg( vm, ci, REG_ECX, 4 );			// range check ecx
			EmitString( "8B 04 0B" );					// mov	eax, dword ptr [ebx + ecx]
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );	// mov dword ptr [edi], eax
			break;

		case OP_LOAD2:
#ifdef MISC_OPTIMIZE
			if ( LastCommand == LAST_COMMAND_MOV_EDI_EAX ) {
				REWIND( 2 );
				EmitCheckReg( vm, ci, REG_EAX, 2 );		// range check eax
				if ( ni->op == OP_SEX16 ) {
					EmitString( "0F BF 04 03" );		// movsx eax, word ptr [ebx + eax]
					ip++;
				} else {
					EmitString( "0F B7 04 03" );		// movzx eax, word ptr [ebx + eax]
				}
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
				break;
			}
#endif
			EmitMovECXEDI( vm );						// mov ecx, dword ptr [edi]
			EmitCheckReg( vm, ci, REG_ECX, 2 );			// range check ecx
			if ( ni->op == OP_SEX16 ) {
				EmitString( "0F BF 04 0B" );			// movsx eax, word ptr [ebx + ecx]
				ip++;
			} else {
				EmitString( "0F B7 04 0B" );			// movzx eax, word ptr [ebx + ecx]
			}
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );	// mov dword ptr [edi], eax
			break;

		case OP_LOAD1:
#ifdef MISC_OPTIMIZE
			if ( LastCommand == LAST_COMMAND_MOV_EDI_EAX ) {
				REWIND( 2 );
				EmitCheckReg( vm, ci, REG_EAX, 1 );		// range check eax
				if ( ni->op == OP_SEX8 ) {
					EmitString( "0F BE 04 03" );		// movsx eax, byte ptr [ebx + eax]
					ip++;
				} else {
					EmitString( "0F B6 04 03" );		// movzx eax, byte ptr [ebx + eax]
				}
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
				break;
			}
#endif
			EmitMovECXEDI( vm );						// mov ecx, dword ptr [edi]
			EmitCheckReg( vm, ci, REG_ECX, 1 );			// range check ecx
			if ( ni->op == OP_SEX8 ) {
				EmitString( "0F BE 04 0B" );			// movsx eax, byte ptr [ebx + ecx]
				ip++;
			} else {
				EmitString( "0F B6 04 0B" );			// movzx eax, byte ptr [ebx + ecx]
			}
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );	// mov dword ptr [edi], eax
			break;

		case OP_STORE4:
			EmitMovEAXEDI( vm );						// mov eax, dword ptr [edi]
			EmitString( "8B 4F FC" );					// mov ecx, dword ptr [edi-4]
			EmitCheckReg( vm, ci, REG_ECX, 4 );			// range check
			EmitString( "89 04 0B" );					// mov dword ptr [ebx + ecx], eax
			EmitCommand( LAST_COMMAND_SUB_DI_8 );		// sub edi, 8
			break;

		case OP_STORE2:
			EmitMovEAXEDI( vm );						// mov eax, dword ptr [edi]
			EmitString( "8B 4F FC" );					// mov ecx, dword ptr [edi-4]
			EmitCheckReg( vm, ci, REG_ECX, 2 );			// range check
			EmitString( "66 89 04 0B" );				// mov word ptr [ebx + ecx], ax
			EmitCommand( LAST_COMMAND_SUB_DI_8 );		// sub edi, 8
			break;

		case OP_STORE1:
			EmitMovEAXEDI( vm );						// mov eax, dword ptr [edi]
			EmitString( "8B 4F FC" );					// mov ecx, dword ptr [edi-4]
			EmitCheckReg( vm, ci, REG_ECX, 1 );			// range check
			EmitString( "88 04 0B" );					// mov byte ptr [ebx + ecx], al
			EmitCommand( LAST_COMMAND_SUB_DI_8 );		// sub edi, 8
			break;

		case OP_EQ:
		case OP_NE:
		case OP_LTI:
		case OP_LEI:
		case OP_GTI:
		case OP_GEI:
		case OP_LTU:
		case OP_LEU:
		case OP_GTU:
		case OP_GEU:
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitCommand( LAST_COMMAND_SUB_DI_8 );	// sub edi, 8
			EmitString( "39 47 04" );				// cmp dword ptr [edi+4], eax
			EmitJump( vm, ci, ci->op, ci->value );
			break;

		case OP_EQF:
		case OP_NEF:
		case OP_LTF:
		case OP_LEF:
		case OP_GTF:
		case OP_GEF:
			if ( HasFCOM() ) {
				EmitLoadFloatEDI( vm );
				if ( HasSSEFP() ) {
					EmitString( "F3 0F 10 4F FC" );			// movss xmm1, dword ptr [edi-4]
					EmitCommand( LAST_COMMAND_SUB_DI_8 );	// sub edi, 8
					EmitString( "0F 2F C8" );				// comiss xmm1, xmm0
				} else {
					EmitCommand( LAST_COMMAND_SUB_DI_8 );	// sub edi, 8
					//EmitString( "D9 47 08" );				// fld dword ptr [edi+8]
					EmitString( "D9 47 04" );				// fld dword ptr [edi+4]
					EmitString( "DF E9" );					// fucomip
					EmitString( "DD D8" );					// fstp st(0)
				}
				EmitJump( vm, ci, ci->op, ci->value );
			} else {
				EmitCommand( LAST_COMMAND_SUB_DI_8 );	// sub edi, 8
				EmitString( "D9 47 04" );				// fld dword ptr [edi+4]
				EmitString( "D8 5F 08" );				// fcomp dword ptr [edi+8]
				EmitString( "DF E0" );					// fnstsw ax
				EmitFloatJump( vm, ci, ci->op, ci->value );
			}
			pop1 = OP_UNDEF;
			break;

		case OP_NEGI:
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitString( "F7 D8" );					// neg eax
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			break;

		case OP_ADD:
			wantres = ( ops[ ni->op ].stack <= 0 );
			EmitMovEAXEDI( vm );						// mov eax, dword ptr [edi]
			if ( wantres ) {
				EmitString( "03 47 FC" );				// add eax, dword ptr [edi-4]
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			} else {
				EmitString( "01 47 FC" );				// add dword ptr [edi-4],eax
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			}
			break;

		case OP_SUB:
			wantres = ( ops[ ni->op ].stack <= 0 );
			if ( wantres ) {
				EmitMovECXEDI( vm );					// mov ecx,dword ptr [edi]
				EmitString( "8B 47 FC" );				// mov eax,dword ptr [edi-4]
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				EmitString( "29 C8" );					// sub eax, ecx
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			} else {
				EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
				EmitString( "29 47 FC" );				// sub dword ptr [edi-4],eax
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			}
			break;

		case OP_DIVI:
			wantres = ( ops[ ni->op ].stack <= 0 );
			EmitMovECXEDI( vm );					// mov ecx,dword ptr [edi]
			EmitString( "8B 47 FC" );				// mov eax,dword ptr [edi-4]
			if ( wantres ) {
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				EmitString( "99" );						// cdq
				EmitString( "F7 F9" );					// idiv ecx
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			} else{
				EmitString( "99" );						// cdq
				EmitString( "F7 F9" );					// idiv ecx
				EmitString( "89 47 FC" );				// mov dword ptr [edi-4],eax
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			}
			break;

		case OP_DIVU:
			EmitString( "8B 47 FC" );				// mov eax,dword ptr [edi-4]
			EmitString( "33 D2" );					// xor edx, edx
			EmitString( "F7 37" );					// div dword ptr [edi]
			EmitString( "89 47 FC" );				// mov dword ptr [edi-4],eax
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_MODI:
			EmitString( "8B 47 FC" );				// mov eax,dword ptr [edi-4]
			EmitString( "99" );						// cdq
			EmitString( "F7 3F" );					// idiv dword ptr [edi]
			EmitString( "89 57 FC" );				// mov dword ptr [edi-4],edx
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_MODU:
			EmitString( "8B 47 FC" );				// mov eax,dword ptr [edi-4]
			EmitString( "33 D2" );					// xor edx, edx
			EmitString( "F7 37" );					// div dword ptr [edi]
			EmitString( "89 57 FC" );				// mov dword ptr [edi-4],edx
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_MULI:
			wantres = ( ops[ ni->op ].stack <= 0 );
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitString( "F7 6F FC" );				// imul eax, dword ptr [edi-4]
			if ( wantres ) {
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			} else {
				EmitString( "89 47 FC" );				// mov dword ptr [edi-4],eax
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			}
			break;

		case OP_MULU:
			EmitString( "8B 47 FC" );				// mov eax,dword ptr [edi-4]
			EmitString( "F7 27" );					// mul dword ptr [edi]
			EmitString( "89 47 FC" );				// mov dword ptr [edi-4],eax
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_BAND:
			wantres = ( ops[ ni->op ].stack <= 0 );
			EmitMovEAXEDI( vm );						// mov eax, dword ptr [edi]
			if ( wantres ) {
				EmitString( "23 47 FC" );				// and eax, dword ptr [edi-4]
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			} else {
				EmitString( "21 47 FC" );				// and dword ptr [edi-4],eax
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			}
			break;

		case OP_BOR:
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitString( "09 47 FC" );				// or dword ptr [edi-4],eax
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_BXOR:
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitString( "31 47 FC" );				// xor dword ptr [edi-4],eax
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_BCOM:
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitString( "F7 D0" );					// not eax
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			break;

		case OP_LSH:
			EmitMovECXEDI( vm );					// mov ecx, dword ptr [edi]
			EmitString( "D3 67 FC" );				// shl dword ptr [edi-4], cl
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_RSHI:
			EmitMovECXEDI( vm );					// mov ecx, dword ptr [edi]
			EmitString( "D3 7F FC" );				// sar dword ptr [edi-4], cl
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_RSHU:
			EmitMovECXEDI( vm );					// mov ecx, dword ptr [edi]
			EmitString( "D3 6F FC" );				// shr dword ptr [edi-4], cl
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			break;

		case OP_NEGF:
			//if ( !ci->fpu )
			EmitLoadFloatEDI( vm );					// fld dword ptr [edi] | movss xmm0, dword ptr [edi]
			if ( HasSSEFP() ) {
				EmitString( "0F 57 C9" );			// xorps xmm1, xmm1
				EmitString( "0F 5C C8" );			// subps xmm1, xmm0
				EmitString( "0F 28 C1" );			// movaps xmm0, xmm1
			} else {
				EmitString( "D9 E0" );				// fchs
			}
			//if ( !ci->fpu || ci->store )
			EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI ); // fstp dword ptr [edi]
			break;

		case OP_ADDF:
		case OP_SUBF:
		case OP_DIVF:
		case OP_MULF:
			wantres = ( ops[ ni->op ].stack <= 0 );
			if ( HasSSEFP() ) {
				if ( LastCommand == LAST_COMMAND_STORE_FLOAT_EDI_SSE ) {
					REWIND(4);
					EmitString( "0F 28 C8" );		// movaps xmm1, xmm0
				} else {
					EmitString( "F3 0F 10 0F" );	// movss xmm1, dword ptr [edi]
				}
				EmitString( "F3 0F 10 47 FC" );		// movss xmm0, dword ptr [edi-4]
				if ( wantres ) {
					if ( ni->op == OP_STORE4 ) {
						 EmitString( "8B 47 F8" );	// mov eax, dword ptr [edi-8]
						 EmitCheckReg( vm, ni, REG_EAX, 4 );
					} else if ( ni->op == OP_ARG ) {
						//
					} else {
						EmitCommand( LAST_COMMAND_SUB_DI_4 );
					}
				}
				switch( ci->op ) {
					case OP_ADDF: EmitString( "0F 58 C1" ); break;	// addps xmm0, xmm1
					case OP_SUBF: EmitString( "0F 5C C1" ); break;	// subps xmm0, xmm1
					case OP_MULF: EmitString( "0F 59 C1" ); break;	// mulps xmm0, xmm1
					case OP_DIVF: EmitString( "0F 5E C1" ); break;	// divps xmm0, xmm1
				}
				if ( wantres ) {
					if ( ni->op == OP_STORE4 ) {
						EmitString( "F3 0F 11 04 03" );		// movss dword ptr [ebx + eax], xmm0
						EmitCommand( LAST_COMMAND_SUB_DI_12 );
						pop1 = OP_UNDEF;
						ip++; // OP_STORE4
					} else if ( ni->op == OP_ARG ) {
						v = ni->value;
						if ( ISU8( v ) ) {
							EmitString( "F3 0F 11 45" ); // movss dword ptr [ebp + 0x7F], xmm0
							Emit1( v );
						} else {
							EmitString( "F3 0F 11 85" ); // movss dword ptr [ebp + 0x12345678], xmm0
							Emit4( v );
						}
						EmitCommand( LAST_COMMAND_SUB_DI_8 );
						pop1 = OP_UNDEF;
						ip++; // OP_ARG
					} else {
						EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI );
					}
				} else {
					EmitString( "F3 0F 11 47 FC" );			// movss dword ptr [edi-4], xmm0
					EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
				}
			} else {
				EmitString( "D9 47 FC" );			// fld dword ptr [edi-4]
				EmitFCalcEDI( ci->op );				// fadd|fsub|fmul|fdiv dword ptr [edi]
				EmitString( "D9 5F FC" );			// fstp dword ptr [edi-4]
				EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4
			}
			break;

		case OP_CVIF:
			if ( HasSSEFP() ) {
				if ( LastCommand == LAST_COMMAND_MOV_EDI_EAX ) {
					REWIND(2);
					EmitString( "F3 0F 2A C0" );	// cvtsi2ss xmm0, eax
				} else {
					EmitString( "F3 0F 2A 07" );	// cvtsi2ss xmm0, dword ptr [edi]
				}
			} else {
				EmitString( "DB 07" );				// fild dword ptr [edi]
			}
			//if ( !ci->fpu )
			EmitCommand( LAST_COMMAND_STORE_FLOAT_EDI );
			break;

		case OP_CVFI:
			if ( HasSSEFP() ) {
				// assume that rounding mode in MXCSR is correctly set in 64-bit environment
				EmitLoadFloatEDI_SSE( vm );				// movss xmm0, dword ptr [edi]
				EmitString( "F3 0F 2C C0" );			// cvttss2si eax, xmm0
				EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			} else {
				EmitLoadFloatEDI_X87( vm );			// fld dword ptr [edi]
				// call the library conversion function
				EmitCallOffset( FUNC_FTOL );	// call +FUNC_FTOL
			}
			break;

		case OP_SEX8:
			EmitString( "0F BE 07" );				// movsx eax, byte ptr [edi]
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			break;

		case OP_SEX16:
			EmitString( "0F BF 07" );				// movsx eax, word ptr [edi]
			EmitCommand( LAST_COMMAND_MOV_EDI_EAX );// mov dword ptr [edi], eax
			break;

		case OP_BLOCK_COPY:
			EmitString( "B9" );						// mov ecx, 0x12345678
			Emit4( ci->value >> 2 );
			EmitCallOffset( FUNC_BCPY );
			break;

		case OP_JUMP:
			EmitMovEAXEDI( vm );					// mov eax, dword ptr [edi]
			EmitCommand( LAST_COMMAND_SUB_DI_4 );	// sub edi, 4

			// jump target range check
			if ( vm_rtChecks->integer & 4 ) {
				if ( proc_base != -1 ) {
					// allow jump within local function scope only
					EmitString( "89 C2" );			// mov edx, eax
					if ( ISU8( proc_base ) ) {
						EmitString( "83 EA" );		// sub edx, 0x7F
						Emit1( proc_base );
					} else {
						EmitString( "81 EA" );		// sub edx, 0x12345678
						Emit4( proc_base );
					}
					if ( ISU8( proc_len ) ) {
						EmitString( "83 FA" );		// cmp edx, 0x7F
						Emit1( proc_len );
					} else {
						EmitString( "81 FA" );		// cmp edx, 0x12345678
						Emit4( proc_len );
					}
				} else {
					EmitString( "3D" );				// cmp eax, 0x12345678
					Emit4( vm->instructionCount );
				}
				EmitString( "0F 87" );				// ja +funcOffset[FUNC_BADJ]
				n = funcOffset[FUNC_BADJ] - compiledOfs;
				Emit4( n - 6 );
			}
#if idx64
			EmitString( "41 FF 24 C0" );			// jmp dword ptr [r8 + rax*8]
#else
			EmitString( "FF 24 85" );				// jmp dword ptr [instructionPointers + eax * 4]
			EmitPtr( instructionPointers );
#endif
			break;

#ifdef MACRO_OPTIMIZE
		case MOP_ADD4:
		case MOP_SUB4:
		case MOP_BAND4:
		case MOP_BOR4:
		case MOP_NCPY:
			if ( !EmitMOPs( vm, ci->op ) )
				Com_Error( ERR_FATAL, "VM_CompileX86: bad opcode %02X", ci->op );
			break;
#endif
		default:
			Com_Error( ERR_FATAL, "VM_CompileX86: bad opcode %02X", ci->op );
			VM_FreeBuffers();
			return qfalse;
		}

		pop1 = (opcode_t)ci->op;
	} // while( ip < header->instructionCount )

		// ****************
		// system functions
		// ****************
		EmitAlign( 8 );
		funcOffset[FUNC_CALL] = compiledOfs;
		EmitCallFunc( vm );

		EmitAlign( 8 );
		funcOffset[FUNC_FTOL] = compiledOfs;
		EmitFTOLFunc( vm );

		EmitAlign( 8 );
		funcOffset[FUNC_BCPY] = compiledOfs;
		EmitBCPYFunc( vm );

		EmitAlign( 8 );
		funcOffset[FUNC_NCPY] = compiledOfs;
		EmitNCPYFunc( vm );

		// ***************
		// error functions
		// ***************

		// bad jump
		EmitAlign( 8 );
		funcOffset[FUNC_BADJ] = compiledOfs;
		EmitBADJFunc( vm );

		// error jump
		EmitAlign( 8 );
		funcOffset[FUNC_ERRJ] = compiledOfs;
		EmitERRJFunc( vm );

		// programStack overflow
		EmitAlign( 8 );
		funcOffset[FUNC_PSOF] = compiledOfs;
		EmitPSOFFunc( vm );

		// opStack overflow
		EmitAlign( 8 );
		funcOffset[FUNC_OSOF] = compiledOfs;
		EmitOSOFFunc( vm );

		// read/write access violation
		EmitAlign( 8 );
		funcOffset[FUNC_DATA] = compiledOfs;
		EmitDATAFunc( vm );

		EmitAlign( sizeof( intptr_t ) ); // for instructionPointers

	} // for( pass = 0; pass < n; pass++ )

	n = header->instructionCount * sizeof( intptr_t );

	if ( code == NULL ) {
		code = (byte*)VM_Alloc_Compiled( vm, PAD(compiledOfs,8), n );
		if ( code == NULL ) {
			return qfalse;
		}
		instructionPointers = (intptr_t*)(byte*)(code + PAD(compiledOfs,8));
		//vm->instructionPointers = instructionPointers; // for debug purposes?
		pass = NUM_PASSES-1; // repeat last pass
		goto __compile;
	}

	// offset all the instruction pointers for the new location
	for ( i = 0 ; i < header->instructionCount ; i++ ) {
		if ( !inst[i].jused ) {
			instructionPointers[ i ] = (intptr_t)badJumpPtr;
			continue;
		}
		instructionPointers[ i ] = (intptr_t)vm->codeBase.ptr + instructionOffsets[ i ];
	}

	VM_FreeBuffers();

#ifdef VM_X86_MMAP
	if ( mprotect( vm->codeBase.ptr, vm->codeSize, PROT_READ|PROT_EXEC ) ) {
		VM_Destroy_Compiled( vm );
		Com_Printf( S_COLOR_YELLOW "VM_CompileX86: mprotect failed\n" );
		return qfalse;
	}
#elif _WIN32
	{
		DWORD oldProtect = 0;

		// remove write permissions.
		if ( !VirtualProtect( vm->codeBase.ptr, vm->codeSize, PAGE_EXECUTE_READ, &oldProtect ) ) {
			VM_Destroy_Compiled( vm );
			Com_Printf( S_COLOR_YELLOW "VM_CompileX86: VirtualProtect failed\n" );
			return qfalse;
		}
	}
#endif

	vm->destroy = VM_Destroy_Compiled;

	Com_Printf( "VM file %s compiled to %i bytes of code\n", vm->name, compiledOfs );

	return qtrue;
}

/*
=================
VM_Alloc_Compiled
=================
*/
static void *VM_Alloc_Compiled( vm_t *vm, int codeLength, int tableLength )
{
	void	*ptr;
	int		length;

	length = codeLength + tableLength;
#ifdef VM_X86_MMAP
	ptr = mmap( NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
	if ( ptr == MAP_FAILED ) {
		Com_Error( ERR_FATAL, "VM_CompileX86: mmap failed" );
		return NULL;
	}
#elif _WIN32
	// allocate memory with EXECUTE permissions under windows.
	ptr = VirtualAlloc( NULL, length, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
	if ( !ptr ) {
		Com_Error( ERR_FATAL, "VM_CompileX86: VirtualAlloc failed" );
		return NULL;
	}
#else
	ptr = malloc( length );
	if ( !ptr ) {
		Com_Error( ERR_FATAL, "VM_CompileX86: malloc failed" );
		return NULL;
	}
#endif
	vm->codeBase.ptr = (byte*)ptr;
	vm->codeLength = codeLength;
	vm->codeSize = length;

	return vm->codeBase.ptr;
}

/*
==============
VM_Destroy_Compiled
==============
*/
static void VM_Destroy_Compiled( vm_t* vm )
{
#ifdef VM_X86_MMAP
	munmap( vm->codeBase.ptr, vm->codeSize );
#elif _WIN32
	VirtualFree( vm->codeBase.ptr, 0, MEM_RELEASE );
#else
	free( vm->codeBase.ptr );
#endif
	vm->codeBase.ptr = NULL;
}

/*
==============
VM_CallCompiled

This function is called directly by the generated code
==============
*/
int VM_CallCompiled( vm_t *vm, int nargs, int *args )
{
	int		opStack[MAX_OPSTACK_SIZE];
	unsigned int stackOnEntry;
	int		*image;
#if id386
	int		*oldOpTop;
#endif
	int		i;

	// we might be called recursively, so this might not be the very top
	stackOnEntry = vm->programStack;
#if id386
	oldOpTop = vm->opStackTop;
#endif

	vm->programStack -= (MAX_VMMAIN_CALL_ARGS+2)*4;

	// set up the stack frame
	image = (int*)( vm->dataBase + vm->programStack );
	for ( i = 0; i < nargs; i++ ) {
		image[ i + 2 ] = args[ i ];
	}

	// these only needed for interpreter:
	// image[1] =  0;	// return stack
	// image[0] = -1;	// will terminate loop on return

	opStack[1] = 0;

	vm->opStack = opStack;
#if id386
	vm->opStackTop = opStack + ARRAY_LEN( opStack ) - 1;
#endif

	vm->codeBase.func(); // go into generated code

	if ( vm->opStack != &opStack[1] ) {
		Com_Error( ERR_DROP, "opStack corrupted in compiled code" );
	}

#ifdef DEBUG_VM
	if ( vm->programStack != stackOnEntry - CALL_PSTACK ) {
		Com_Error( ERR_DROP, "programStack corrupted in compiled code" );
	}
#endif

	vm->programStack = stackOnEntry;
#if id386
	vm->opStackTop = oldOpTop;
#endif

	return vm->opStack[0];
}

{'repo_name': 'ec-/Quake3e', 'stars': '153', 'repo_language': 'C', 'file_name': 'sv_snapshot.c', 'mime_type': 'text/x-c', 'hash': 3337813978792535296, 'source_dataset': 'data'}


/*
 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "gsskrb5_locl.h"

OM_uint32 GSSAPI_CALLCONV _gsskrb5_display_name
           (OM_uint32 * minor_status,
            const gss_name_t input_name,
            gss_buffer_t output_name_buffer,
            gss_OID * output_name_type
           )
{
    krb5_context context;
    krb5_const_principal name = (krb5_const_principal)input_name;
    krb5_error_code kret;
    char *buf;
    size_t len;

    GSSAPI_KRB5_INIT (&context);

    kret = krb5_unparse_name_flags (context, name,
				    KRB5_PRINCIPAL_UNPARSE_DISPLAY, &buf);
    if (kret) {
	*minor_status = kret;
	return GSS_S_FAILURE;
    }
    len = strlen (buf);
    output_name_buffer->length = len;
    output_name_buffer->value  = malloc(len + 1);
    if (output_name_buffer->value == NULL) {
	free (buf);
	*minor_status = ENOMEM;
	return GSS_S_FAILURE;
    }
    memcpy (output_name_buffer->value, buf, len);
    ((char *)output_name_buffer->value)[len] = '\0';
    free (buf);
    if (output_name_type)
	*output_name_type = GSS_KRB5_NT_PRINCIPAL_NAME;
    *minor_status = 0;
    return GSS_S_COMPLETE;
}

{'repo_name': 'samba-team/samba', 'stars': '470', 'repo_language': 'C', 'file_name': 'blackbox_start_backup.sh', 'mime_type': 'text/x-shellscript', 'hash': -1963837253884843547, 'source_dataset': 'data'}