#include "opendylan/run-time.h"

/* Generated by Open Dylan 2014.1 for x86_64-linux */

/* Typedefs for referenced classes */

typedef struct {
  dylan_value wrapper;
  dylan_value mangler_buffer_;
  dylan_value mangler_table_;
} _KLmanglerGVdfmc_mangling;

typedef struct {
  dylan_value wrapper;
  dylan_value instanceQ_iep_;
  dylan_value debug_name_;
  dylan_value class_implementation_class_;
  dylan_value class_subtype_bit_;
  dylan_value class_module_;
} _KLclassGVKd;

#define  define__KLmm_wrapperGVKi(nrepeated) \
  typedef struct { \
    dylan_value wrapper; \
    dylan_value mm_wrapper_implementation_class_; \
    dylan_value mm_wrapper_subtype_mask_; \
    DWORD mm_wrapper_fixed_part_; \
    DWORD mm_wrapper_variable_part_; \
    dylan_value mm_wrapper_number_patterns_; \
    DWORD mm_wrapper_pattern_element_[nrepeated+1]; \
  } _KLmm_wrapperGVKi_##nrepeated;
define__KLmm_wrapperGVKi(1);
define__KLmm_wrapperGVKi(0);

#define  define__KLsimple_object_vectorGVKd(nrepeated) \
  typedef struct { \
    dylan_value wrapper; \
    dylan_value size_; \
    dylan_value vector_element_[nrepeated+1]; \
  } _KLsimple_object_vectorGVKd_##nrepeated;
define__KLsimple_object_vectorGVKd(2);
define__KLsimple_object_vectorGVKd(256);
define__KLsimple_object_vectorGVKd(3);
define__KLsimple_object_vectorGVKd(1);
define__KLsimple_object_vectorGVKd(0);

typedef struct {
  dylan_value wrapper;
  dylan_value head_;
  dylan_value tail_;
} _KLempty_listGVKd;

typedef struct {
  dylan_value wrapper;
} _KLobjectGVKd;

typedef struct {
  dylan_value wrapper;
} _KLunboundGVKe;

typedef struct {
  dylan_value wrapper;
} _KLbooleanGVKd;

typedef struct {
  dylan_value wrapper;
  dylan_value xep_;
  dylan_value function_signature_;
  dylan_value mep_;
} _KLsimple_methodGVKe;

typedef struct {
  dylan_value wrapper;
  dylan_value instanceQ_iep_;
  dylan_value debug_name_;
  dylan_value class_implementation_class_;
  dylan_value class_subtype_bit_;
  dylan_value class_module_;
} _KLfunction_classGVKi;

#define  define__KLbyte_stringGVKd(nrepeated) \
  typedef struct { \
    dylan_value wrapper; \
    dylan_value size_; \
    char string_element_[nrepeated+1]; \
  } _KLbyte_stringGVKd_##nrepeated;
define__KLbyte_stringGVKd(1);
define__KLbyte_stringGVKd(55);

typedef struct {
  dylan_value wrapper;
  dylan_value head_;
  dylan_value tail_;
} _KLpairGVKd;

typedef struct {
  dylan_value wrapper;
  dylan_value xep_;
  dylan_value function_signature_;
  dylan_value mep_;
  dylan_value iep_;
  dylan_value keyword_specifiers_;
} _KLkeyword_methodGVKe;

typedef struct {
  dylan_value wrapper;
  dylan_value symbol_name_;
} _KLsymbolGVKd;

typedef struct {
  dylan_value wrapper;
  dylan_value signature_properties_;
  dylan_value signature_required_;
  dylan_value signature_keys_;
  dylan_value signature_key_types_;
} _KLkeyword_signatureGVKe;


/* Typedefs for defined classes */


/* Referenced object declarations */

dylan_value KmakeVKdMdfmc_manglingM0I (dylan_value, dylan_value);
extern _KLclassGVKd KLmanglerGVdfmc_mangling;
extern _KLclassGVKd KLclassGVKd;
extern _KLmm_wrapperGVKi_0 KLclassGVKdW;
extern _KLclassGVKd KLmm_wrapperGVKi;
extern _KLmm_wrapperGVKi_1 KLmm_wrapperGVKiW;
extern _KLmm_wrapperGVKi_0 KLmanglerGVdfmc_manglingW;
extern _KLsimple_object_vectorGVKd_0 KPempty_vectorVKi;
extern _KLclassGVKd KLsimple_object_vectorGVKd;
extern _KLmm_wrapperGVKi_0 KLsimple_object_vectorGVKdW;
extern _KLempty_listGVKd KPempty_listVKi;
extern _KLclassGVKd KLempty_listGVKd;
extern _KLmm_wrapperGVKi_0 KLempty_listGVKdW;
extern _KLclassGVKd KLobjectGVKd;
extern _KLmm_wrapperGVKi_0 KLobjectGVKdW;
extern _KLunboundGVKe KPunboundVKi;
extern _KLclassGVKd KLunboundGVKe;
extern _KLmm_wrapperGVKi_0 KLunboundGVKeW;
extern _KLbooleanGVKd KPtrueVKi;
extern _KLclassGVKd KLbooleanGVKd;
extern _KLmm_wrapperGVKi_0 KLbooleanGVKdW;
extern _KLbooleanGVKd KPfalseVKi;
extern DWORD dlopen (DBSTR);
extern DWORD dlsym (DWORD, DBSTR);
extern _KLsimple_methodGVKe Kmangle_binding_spreadVdfmc_manglingMM0;
extern _KLfunction_classGVKi KLsimple_methodGVKe;
extern _KLclassGVKd KLfunction_classGVKi;
extern _KLmm_wrapperGVKi_0 KLfunction_classGVKiW;
extern _KLmm_wrapperGVKi_0 KLsimple_methodGVKeW;
dylan_value Kconcatenate_asVKdMM3I (dylan_value, dylan_value, dylan_value);
extern _KLclassGVKd KLbyte_stringGVKd;
extern _KLmm_wrapperGVKi_0 KLbyte_stringGVKdW;
dylan_value KerrorVKdMM1I (dylan_value, dylan_value);
extern _KLclassGVKd KLpairGVKd;
extern _KLmm_wrapperGVKi_0 KLpairGVKdW;
extern _KLsimple_methodGVKe Kconcatenate_asVKdMM0;
extern _KLsimple_methodGVKe Kconcatenate_asVKdMM1;
extern _KLfunction_classGVKi KLkeyword_methodGVKe;
extern _KLmm_wrapperGVKi_0 KLkeyword_methodGVKeW;
extern _KLsymbolGVKd KJdefault_;
extern _KLclassGVKd KLsymbolGVKd;
extern _KLmm_wrapperGVKi_0 KLsymbolGVKdW;
extern _KLclassGVKd KLkeyword_signatureGVKe;
extern _KLmm_wrapperGVKi_0 KLkeyword_signatureGVKeW;
extern _KLsimple_object_vectorGVKd_256 KDsignature_LobjectG_typesVKi;

/* Defined object declarations */

extern _KLkeyword_methodGVKe Kvariable_valueVvariable_searchMM0;
dylan_value Kvariable_valueVvariable_searchMM0I (dylan_value, dylan_value, dylan_value, dylan_value, dylan_value);
static _KLpairGVKd K3;
static _KLpairGVKd K4;
static _KLbyte_stringGVKd_55 K5;
static _KLbyte_stringGVKd_1 K6;
static _KLkeyword_signatureGVKe K7;
static _KLsimple_object_vectorGVKd_2 K8;
static _KLsimple_object_vectorGVKd_1 K9;

/* Indirection variables */


/* Variables */

dylan_value TmanglerTVvariable_search = &KPunboundVKi;
dylan_value Tdl_handleTVvariable_search = &KPfalseVKi;
dylan_value constant_prefix_stringVvariable_search = &K6;

/* Objects */

_KLkeyword_methodGVKe Kvariable_valueVvariable_searchMM0 = {
  &KLkeyword_methodGVKeW /* wrapper */,
  &rest_key_xep_5,
  &K7,
  &key_mep_5,
  &Kvariable_valueVvariable_searchMM0I,
  &K8
};

static _KLpairGVKd K3 = {
  &KLpairGVKdW /* wrapper */,
  &Kconcatenate_asVKdMM1,
  &K4
};

static _KLpairGVKd K4 = {
  &KLpairGVKdW /* wrapper */,
  &Kconcatenate_asVKdMM0,
  &KPempty_listVKi
};

static _KLbyte_stringGVKd_55 K5 = {
  &KLbyte_stringGVKdW /* wrapper */,
  (dylan_value) 221,
  "Failed to locate variable %= in module %= of library %="
};

static _KLbyte_stringGVKd_1 K6 = {
  &KLbyte_stringGVKdW /* wrapper */,
  (dylan_value) 5,
  "K"
};

static _KLkeyword_signatureGVKe K7 = {
  &KLkeyword_signatureGVKeW /* wrapper */,
  (dylan_value) 42206221,
  &KDsignature_LobjectG_typesVKi,
  &K9,
  &KDsignature_LobjectG_typesVKi
};

static _KLsimple_object_vectorGVKd_2 K8 = {
  &KLsimple_object_vectorGVKdW /* wrapper */,
  (dylan_value) 9,
  &KJdefault_,
  &KPfalseVKi
};

static _KLsimple_object_vectorGVKd_1 K9 = {
  &KLsimple_object_vectorGVKdW /* wrapper */,
  (dylan_value) 5,
  &KJdefault_
};

/* Code */

dylan_value Kvariable_valueVvariable_searchMM0I (dylan_value variable_name_, dylan_value module_name_, dylan_value library_name_, dylan_value Urest_, dylan_value default_) {
  dylan_value T6;
  dylan_value T7;
  dylan_value T8;
  D T9;
  DWORD T10;
  dylan_value T11;
  dylan_value T12;
  DWORD T13;
  DBSTR T14;
  DWORD T15;
  D val_;
  dylan_value T17;
  D T18_0;
  D T19_0;
  dylan_value mangled_name_;
  _KLsimple_object_vectorGVKd_1 T21 = {&KLsimple_object_vectorGVKdW, (dylan_value) 5};
  dylan_value T22_0;
  _KLsimple_object_vectorGVKd_3 T23 = {&KLsimple_object_vectorGVKdW, (dylan_value) 13};
  dylan_value T24;
  dylan_value T25_0;

  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:13
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:24
  T6 = primitive_read_thread_variable(TmanglerTVvariable_search);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:23
  T7 = CALL4(&Kmangle_binding_spreadVdfmc_manglingMM0, T6, variable_name_, module_name_, library_name_);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:21
  T21.vector_element_[0] = T7;
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:21
  MIEP_CALL_PROLOG(&K3);
  T22_0 = Kconcatenate_asVKdMM3I(&KLbyte_stringGVKd, constant_prefix_stringVvariable_search, &T21);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:21
  mangled_name_ = T22_0;
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:25
  T8 = Tdl_handleTVvariable_search;
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:25
  if (T8 == &KPfalseVKi) {
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:31
    T9 = primitive_cast_raw_as_pointer(0);
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:28
    T10 = dlopen(T9);
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:27
    T11 = primitive_wrap_machine_word(T10);
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:26
    Tdl_handleTVvariable_search = T11;
  }
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:25
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:39
  T12 = Tdl_handleTVvariable_search;
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:39
  T13 = primitive_unwrap_machine_word(T12);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:40
  T14 = primitive_string_as_raw(mangled_name_);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:36
  T15 = dlsym(T13, T14);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:35
  val_ = primitive_cast_raw_as_pointer(T15);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:42
  T17 = primitive_machine_word_equalQ(val_,0);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:42
  if (T17 != &KPfalseVKi) {
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:17
    T23.vector_element_[0] = variable_name_;
    T23.vector_element_[1] = module_name_;
    T23.vector_element_[2] = library_name_;
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:17
    T24 = KerrorVKdMM1I(&K5, &T23);
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:43
    T25_0 = T24;
    T19_0 = T25_0;
  } else {
    // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:44
    T18_0 = val_;
    T19_0 = T18_0;
  }
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:42
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:13
  MV_SET_COUNT(1);
  return(T19_0);
}


/* SYSTEM INIT CODE */

void _Init_variable_search__X_unix_variable_value_for_system () {
  extern dylan_value KPresolve_symbolVKiI(dylan_value);
  return;
}

/* USER INIT CODE */

void _Init_variable_search__X_unix_variable_value_for_user () {
{
  dylan_value T0;
  dylan_value T1;

  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:9
  T1 = KmakeVKdMdfmc_manglingM0I(&KLmanglerGVdfmc_mangling, &KPempty_vectorVKi);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:9
  TmanglerTVvariable_search = primitive_allocate_thread_variable(T1);
  // /home/bruce/Development/dylan/opendylan/sources/lib/variable-search/unix-variable-value.dylan:9
  T0 = &KPfalseVKi;
  goto I0;
}
I0:

  return;
}


/* eof */
