#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct stat {
  int dummy;
};
struct dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ soPath;
typedef int /*<<< orphan*/ soDepPath;
typedef int /*<<< orphan*/ depHandles;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ ModelicaError(char *);
int /*<<< orphan*/ ModelicaFormatError(char *, char const *, ...);
int /*<<< orphan*/ RTLD_NOW;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char *, char const *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char const *, char const *);
char *strdup(char *);
char *strtok(char *, char *);
void *LoadLibrary(const char *lpFileName) {
  char *pathFMU;
  int len;
  char *soDirs = NULL;
  char *dirName;
  void *handle = NULL;
  void *depHandles[128];
  int nrOfDephandles = 0;
  pathFMU = getenv("LD_LIBRARY_PATH_FMU");
  if (pathFMU == NULL) {
    ModelicaError("Failed to get environment variable LD_LIBRARY_PATH_FMU");
    goto done;
  }
  soDirs = strdup(pathFMU);
  if (soDirs == NULL) {
    ModelicaFormatError("Out of memory when loading %s", lpFileName);
    goto done;
  }
  dirName = strtok(soDirs, ":");
  while (dirName != NULL) {
    char soPath[1024];
    struct stat sb;
    if (snprintf(soPath, sizeof(soPath), "%s/%s", dirName, lpFileName) ==
        sizeof(soPath)) {
      ModelicaFormatError("Internal buffer soPath too small when loading %s",
                          lpFileName);
      goto done;
    }
    if (stat(soPath, &sb) == 0) {
      DIR *dir;
      struct dirent *ent;
      if ((dir = opendir(dirName)) != NULL) {
        while ((ent = readdir(dir)) != NULL) {
          if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0 &&
              strcmp(ent->d_name, lpFileName) != 0) {
            char soDepPath[1024];
            if (snprintf(soDepPath, sizeof(soDepPath), "%s/%s", dirName,
                         ent->d_name) == sizeof(soDepPath)) {
              ModelicaFormatError(
                  "Internal buffer soPath too small when loading %s",
                  lpFileName);
              goto done;
            }
            depHandles[nrOfDephandles] = dlopen(soDepPath, RTLD_NOW);
            if (depHandles[nrOfDephandles] == NULL) {
              ModelicaError(dlerror());
            } else {
              if (++nrOfDephandles >= sizeof(depHandles)) {
                ModelicaFormatError("Too many files in %s", dirName);
                dlclose(depHandles[--nrOfDephandles]);
              }
            }
          }
        }
        closedir(dir);
        handle = dlopen(soPath, RTLD_NOW);
        if (handle != NULL) {
          int i;
          for (i = 0; i < nrOfDephandles; ++i) {
            dlclose(depHandles[i]);
          }
          goto done;
        }
        ModelicaError(dlerror());
      } else {
        ModelicaFormatError("Failed to open directory %s", dirName);
      }
    }
    dirName = strtok(NULL, ":");
  }
  ModelicaFormatError("Did not find %s in the paths in \"%s\"", lpFileName,
                      soDirs);
done:
  free(soDirs);
  return handle;
}


x86 assembly (clang, -O0):
<LoadLibrary>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc70,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x434(%rbp)
lea    0xe03(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1245 <LoadLibrary+0x55>
lea    0xdc5(%rip),%rdi
call   1080 <ModelicaError@plt>
jmp    1559 <LoadLibrary+0x369>
mov    -0x10(%rbp),%rdi
call   1100 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1274 <LoadLibrary+0x84>
mov    -0x8(%rbp),%rsi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   10e0 <ModelicaFormatError@plt>
jmp    1559 <LoadLibrary+0x369>
mov    -0x20(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1543 <LoadLibrary+0x353>
lea    -0x840(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    $0x400,%esi
lea    0xda9(%rip),%rdx
call   1070 <snprintf@plt>
cltq
cmp    $0x400,%rax
jne    12d8 <LoadLibrary+0xe8>
mov    -0x8(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   10e0 <ModelicaFormatError@plt>
jmp    1559 <LoadLibrary+0x369>
lea    -0x840(%rbp),%rdi
lea    -0x848(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0x0,%rax
jne    152a <LoadLibrary+0x33a>
mov    -0x28(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x850(%rbp)
cmp    $0x0,%rax
je     1513 <LoadLibrary+0x323>
jmp    1314 <LoadLibrary+0x124>
mov    -0x850(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x858(%rbp)
cmp    $0x0,%rax
je     1486 <LoadLibrary+0x296>
mov    -0x858(%rbp),%rax
mov    (%rax),%rdi
lea    0xd4d(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1481 <LoadLibrary+0x291>
mov    -0x858(%rbp),%rax
mov    (%rax),%rdi
lea    0xd2c(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1481 <LoadLibrary+0x291>
mov    -0x858(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1481 <LoadLibrary+0x291>
lea    -0xc60(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    -0x858(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xca8(%rip),%rdx
call   1070 <snprintf@plt>
cltq
cmp    $0x400,%rax
jne    13d9 <LoadLibrary+0x1e9>
mov    -0x8(%rbp),%rsi
lea    0xc90(%rip),%rdi
mov    $0x0,%al
call   10e0 <ModelicaFormatError@plt>
jmp    1559 <LoadLibrary+0x369>
lea    -0xc60(%rbp),%rdi
mov    0x2c01(%rip),%rax
mov    (%rax),%esi
call   10b0 <dlopen@plt>
mov    %rax,%rcx
movslq -0x434(%rbp),%rax
mov    %rcx,-0x430(%rbp,%rax,8)
ff
movslq -0x434(%rbp),%rax
cmpq   $0x0,-0x430(%rbp,%rax,8)
jne    142a <LoadLibrary+0x23a>
mov    $0x0,%al
call   1040 <dlerror@plt>
mov    %rax,%rdi
call   1080 <ModelicaError@plt>
jmp    147c <LoadLibrary+0x28c>
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
cltq
cmp    $0x400,%rax
jb     1477 <LoadLibrary+0x287>
mov    -0x28(%rbp),%rsi
lea    0xc3f(%rip),%rdi
mov    $0x0,%al
call   10e0 <ModelicaFormatError@plt>
mov    -0x434(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x434(%rbp)
cltq
mov    -0x430(%rbp,%rax,8),%rdi
ff
call   1110 <dlclose@plt>
jmp    147c <LoadLibrary+0x28c>
jmp    1481 <LoadLibrary+0x291>
jmp    1314 <LoadLibrary+0x124>
mov    -0x850(%rbp),%rdi
call   1090 <closedir@plt>
lea    -0x840(%rbp),%rdi
mov    0x2b48(%rip),%rax
mov    (%rax),%esi
call   10b0 <dlopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     14ff <LoadLibrary+0x30f>
movl   $0x0,-0xc64(%rbp)
mov    -0xc64(%rbp),%eax
cmp    -0x434(%rbp),%eax
jge    14fa <LoadLibrary+0x30a>
movslq -0xc64(%rbp),%rax
mov    -0x430(%rbp,%rax,8),%rdi
ff
call   1110 <dlclose@plt>
mov    -0xc64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc64(%rbp)
jmp    14c0 <LoadLibrary+0x2d0>
jmp    1559 <LoadLibrary+0x369>
mov    $0x0,%al
call   1040 <dlerror@plt>
mov    %rax,%rdi
call   1080 <ModelicaError@plt>
jmp    1525 <LoadLibrary+0x335>
mov    -0x28(%rbp),%rsi
lea    0xb88(%rip),%rdi
mov    $0x0,%al
call   10e0 <ModelicaFormatError@plt>
jmp    152a <LoadLibrary+0x33a>
xor    %eax,%eax
mov    %eax,%edi
lea    0xb20(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1288 <LoadLibrary+0x98>
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xb70(%rip),%rdi
mov    $0x0,%al
call   10e0 <ModelicaFormatError@plt>
mov    -0x20(%rbp),%rdi
call   1050 <free@plt>
mov    -0x30(%rbp),%rax
add    $0xc70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LoadLibrary>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc28,%rsp
mov    %rdi,%rbx
lea    0xe18(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1264 <LoadLibrary+0x74>
mov    %rax,%rdi
call   1100 <strdup@plt>
test   %rax,%rax
je     1428 <LoadLibrary+0x238>
mov    %rbx,0x8(%rsp)
lea    0xe23(%rip),%rsi
mov    %rax,0x18(%rsp)
mov    %rax,%rdi
call   10f0 <strtok@plt>
test   %rax,%rax
je     143f <LoadLibrary+0x24f>
mov    %rax,%r14
lea    0x820(%rsp),%rbx
lea    0x10(%rsp),%r12
lea    0xdf6(%rip),%rbp
xor    %r13d,%r13d
jmp    12a7 <LoadLibrary+0xb7>
lea    0xd95(%rip),%rdi
call   1080 <ModelicaError@plt>
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    1486 <LoadLibrary+0x296>
lea    0xe26(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10e0 <ModelicaFormatError@plt>
lea    0xdc4(%rip),%rbp
xor    %edi,%edi
mov    %rbp,%rsi
call   10f0 <strtok@plt>
mov    %rax,%r14
test   %rax,%rax
je     143f <LoadLibrary+0x24f>
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xda1(%rip),%rdx
mov    %r14,%rcx
mov    0x8(%rsp),%r8
call   1070 <snprintf@plt>
cmp    $0x400,%eax
je     145e <LoadLibrary+0x26e>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10c0 <stat@plt>
test   %rax,%rax
jne    1291 <LoadLibrary+0xa1>
mov    %r14,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1279 <LoadLibrary+0x89>
mov    %rax,%r15
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     13ed <LoadLibrary+0x1fd>
mov    %rax,%rbp
mov    0x8(%rsp),%r12
jmp    1332 <LoadLibrary+0x142>
xor    %eax,%eax
call   1040 <dlerror@plt>
mov    %rax,%rdi
call   1080 <ModelicaError@plt>
mov    %r13d,%eax
mov    %eax,%r13d
mov    %r15,%rdi
call   10d0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     13ed <LoadLibrary+0x1fd>
mov    0x0(%rbp),%rdi
lea    0xd52(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     131e <LoadLibrary+0x12e>
mov    0x0(%rbp),%rdi
lea    0xd3c(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     131e <LoadLibrary+0x12e>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     131e <LoadLibrary+0x12e>
mov    0x0(%rbp),%r8
lea    0x420(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xccf(%rip),%rdx
mov    %r14,%rcx
call   1070 <snprintf@plt>
cmp    $0x400,%eax
je     146e <LoadLibrary+0x27e>
mov    0x2c46(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10b0 <dlopen@plt>
mov    %rax,%rbp
movslq %r13d,%rax
mov    %rbp,0x20(%rsp,%rax,8)
test   %rbp,%rbp
je     1309 <LoadLibrary+0x119>
lea    0x1(%r13),%eax
cmp    $0x400,%eax
jb     131b <LoadLibrary+0x12b>
lea    0xcbb(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10e0 <ModelicaFormatError@plt>
mov    %rbp,%rdi
call   1110 <dlclose@plt>
jmp    1318 <LoadLibrary+0x128>
mov    %r15,%rdi
call   1090 <closedir@plt>
mov    0x2bec(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <dlopen@plt>
test   %rax,%rax
jne    14a3 <LoadLibrary+0x2b3>
xor    %eax,%eax
call   1040 <dlerror@plt>
mov    %rax,%rdi
call   1080 <ModelicaError@plt>
lea    0x10(%rsp),%r12
jmp    128a <LoadLibrary+0x9a>
lea    0xc08(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   10e0 <ModelicaFormatError@plt>
xor    %ebx,%ebx
jmp    1486 <LoadLibrary+0x296>
lea    0xc7c(%rip),%rdi
xor    %ebp,%ebp
mov    0x8(%rsp),%rsi
mov    0x18(%rsp),%rbx
mov    %rbx,%rdx
xor    %eax,%eax
call   10e0 <ModelicaFormatError@plt>
jmp    1486 <LoadLibrary+0x296>
lea    0xbf8(%rip),%rdi
xor    %ebp,%ebp
mov    0x8(%rsp),%rsi
jmp    147a <LoadLibrary+0x28a>
lea    0xbe8(%rip),%rdi
xor    %ebp,%ebp
mov    %r12,%rsi
xor    %eax,%eax
call   10e0 <ModelicaFormatError@plt>
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <free@plt>
mov    %rbp,%rax
add    $0xc28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rbp
test   %r13d,%r13d
jle    1481 <LoadLibrary+0x291>
mov    %r13d,%r14d
xor    %ebx,%ebx
mov    0x20(%rsp,%rbx,8),%rdi
call   1110 <dlclose@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    14b0 <LoadLibrary+0x2c0>
jmp    1481 <LoadLibrary+0x291>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [20 x i8] c"LD_LIBRARY_PATH_FMU\00"
@.str.1 = [55 x i8] c"Failed to get environment variable LD_LIBRARY_PATH_FMU\00"
@.str.2 = [30 x i8] c"Out of memory when loading %s\00"
@.str.3 = [2 x i8] c":\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [49 x i8] c"Internal buffer soPath too small when loading %s\00"
@.str.6 = [2 x i8] c".\00"
@.str.7 = [3 x i8] c"..\00"
@RTLD_NOW = global i32 0
@.str.8 = [21 x i8] c"Too many files in %s\00"
@.str.9 = [28 x i8] c"Failed to open directory %s\00"
@.str.10 = [37 x i8] c"Did not find %s in the paths in \22%s\22\00"
define i8* @LoadLibrary(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [128 x i8*]
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca %struct.stat
%12 = alloca i32*
%13 = alloca %struct.dirent*
%14 = alloca [1024 x i8]
%15 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %5
store i8* null, i8** %7
store i32 0, i32* %9
%16 = call i8* @getenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @ModelicaError(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
br label %157
21: ; preds = %1
%22 = load i8*, i8** %3
%23 = call i8* @strdup(i8* %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %2
%28 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %157
29: ; preds = %21
%30 = load i8*, i8** %5
%31 = call i8* @strtok(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %31, i8** %6
br label %32
32: ; preds = %151, %29
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %153
35: ; preds = %32
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%37 = load i8*, i8** %6
%38 = load i8*, i8** %2
%39 = call i32 @snprintf(i8* %36, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %37, i8* %38)
%40 = sext i32 %39 to i64
%41 = icmp eq i64 %40, 1024
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i8*, i8** %2
%44 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8* %43)
br label %157
45: ; preds = %35
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%47 = call i64 @stat(i8* %46, %struct.stat* %11)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %151
49: ; preds = %45
%50 = load i8*, i8** %6
%51 = call i32* @opendir(i8* %50)
store i32* %51, i32** %12
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %147
53: ; preds = %49
br label %54
54: ; preds = %120, %53
%55 = load i32*, i32** %12
%56 = call %struct.dirent* @readdir(i32* %55)
store %struct.dirent* %56, %struct.dirent** %13
%57 = icmp ne %struct.dirent* %56, null
br i1 %57, label %58, label %121
58: ; preds = %54
%59 = load %struct.dirent*, %struct.dirent** %13
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i64 @strcmp(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %120
64: ; preds = %58
%65 = load %struct.dirent*, %struct.dirent** %13
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i64 @strcmp(i8* %67, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %120
70: ; preds = %64
%71 = load %struct.dirent*, %struct.dirent** %13
%72 = getelementptr inbounds %struct.dirent, %struct.dirent* %71, i32 0, i32 0
%73 = load i8*, i8** %72
%74 = load i8*, i8** %2
%75 = call i64 @strcmp(i8* %73, i8* %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %120
77: ; preds = %70
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%79 = load i8*, i8** %6
%80 = load %struct.dirent*, %struct.dirent** %13
%81 = getelementptr inbounds %struct.dirent, %struct.dirent* %80, i32 0, i32 0
%82 = load i8*, i8** %81
%83 = call i32 @snprintf(i8* %78, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %79, i8* %82)
%84 = sext i32 %83 to i64
%85 = icmp eq i64 %84, 1024
br i1 %85, label %86, label %89
86: ; preds = %77
%87 = load i8*, i8** %2
%88 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8* %87)
br label %157
89: ; preds = %77
%90 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%91 = load i32, i32* @RTLD_NOW
%92 = call i8* @dlopen(i8* %90, i32 %91)
%93 = load i32, i32* %9
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds [128 x i8*], [128 x i8*]* %8, i64 0, i64 %94
store i8* %92, i8** %95
%96 = load i32, i32* %9
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds [128 x i8*], [128 x i8*]* %8, i64 0, i64 %97
%99 = load i8*, i8** %98
%100 = icmp eq i8* %99, null
br i1 %100, label %101, label %104
101: ; preds = %89
%102 = call i8* (...) @dlerror()
%103 = call i32 @ModelicaError(i8* %102)
br label %119
104: ; preds = %89
%105 = load i32, i32* %9
%106 = add nsw i32 %105, 1
store i32 %106, i32* %9
%107 = sext i32 %106 to i64
%108 = icmp uge i64 %107, 1024
br i1 %108, label %109, label %118
109: ; preds = %104
%110 = load i8*, i8** %6
%111 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %110)
%112 = load i32, i32* %9
%113 = add nsw i32 %112, -1
store i32 %113, i32* %9
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds [128 x i8*], [128 x i8*]* %8, i64 0, i64 %114
%116 = load i8*, i8** %115
%117 = call i32 @dlclose(i8* %116)
br label %118
118: ; preds = %109, %104
br label %119
119: ; preds = %118, %101
br label %120
120: ; preds = %119, %70, %64, %58
br label %54
121: ; preds = %54
%122 = load i32*, i32** %12
%123 = call i32 @closedir(i32* %122)
%124 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%125 = load i32, i32* @RTLD_NOW
%126 = call i8* @dlopen(i8* %124, i32 %125)
store i8* %126, i8** %7
%127 = load i8*, i8** %7
%128 = icmp ne i8* %127, null
br i1 %128, label %129, label %144
129: ; preds = %121
store i32 0, i32* %15
br label %130
130: ; preds = %140, %129
%131 = load i32, i32* %15
%132 = load i32, i32* %9
%133 = icmp slt i32 %131, %132
br i1 %133, label %134, label %143
134: ; preds = %130
%135 = load i32, i32* %15
%136 = sext i32 %135 to i64
%137 = getelementptr inbounds [128 x i8*], [128 x i8*]* %8, i64 0, i64 %136
%138 = load i8*, i8** %137
%139 = call i32 @dlclose(i8* %138)
br label %140
140: ; preds = %134
%141 = load i32, i32* %15
%142 = add nsw i32 %141, 1
store i32 %142, i32* %15
br label %130
143: ; preds = %130
br label %157
144: ; preds = %121
%145 = call i8* (...) @dlerror()
%146 = call i32 @ModelicaError(i8* %145)
br label %150
147: ; preds = %49
%148 = load i8*, i8** %6
%149 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %148)
br label %150
150: ; preds = %147, %144
br label %151
151: ; preds = %150, %45
%152 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %152, i8** %6
br label %32
153: ; preds = %32
%154 = load i8*, i8** %2
%155 = load i8*, i8** %5
%156 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0), i8* %154, i8* %155)
br label %157
157: ; preds = %153, %143, %86, %42, %26, %19
%158 = load i8*, i8** %5
%159 = call i32 @free(i8* %158)
%160 = load i8*, i8** %7
ret i8* %160
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [20 x i8] c"LD_LIBRARY_PATH_FMU\00"
@.str.1 = [55 x i8] c"Failed to get environment variable LD_LIBRARY_PATH_FMU\00"
@.str.2 = [30 x i8] c"Out of memory when loading %s\00"
@.str.3 = [2 x i8] c":\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [49 x i8] c"Internal buffer soPath too small when loading %s\00"
@.str.6 = [2 x i8] c".\00"
@.str.7 = [3 x i8] c"..\00"
@RTLD_NOW = global i32 0
@.str.8 = [21 x i8] c"Too many files in %s\00"
@.str.9 = [28 x i8] c"Failed to open directory %s\00"
@.str.10 = [37 x i8] c"Did not find %s in the paths in \22%s\22\00"
define i8* @LoadLibrary(i8* %0) {
%2 = alloca [128 x i8*]
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = bitcast [128 x i8*]* %2 to i8*
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @ModelicaError(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
br label %111
11: ; preds = %1
%12 = tail call i8* @strdup(i8* %7)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %111
16: ; preds = %11
%17 = tail call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %109, label %19
19: ; preds = %16
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = bitcast %struct.stat* %4 to i8*
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %23
23: ; preds = %19, %105
%24 = phi i32 [ 0, %19 ], [ %106, %105 ]
%25 = phi i8* [ %17, %19 ], [ %107, %105 ]
%26 = call i32 @snprintf(i8* %20, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %25, i8* %0)
%27 = icmp eq i32 %26, 1024
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %103
30: ; preds = %23
%31 = call i64 @stat(i8* %20, %struct.stat* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %105
33: ; preds = %30
%34 = call i32* @opendir(i8* %25)
%35 = icmp eq i32* %34, null
br i1 %35, label %101, label %36
36: ; preds = %33
%37 = call %struct.dirent* @readdir(i32* %34)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %81, label %39
39: ; preds = %36, %77
%40 = phi %struct.dirent* [ %79, %77 ], [ %37, %36 ]
%41 = phi i32 [ %78, %77 ], [ %24, %36 ]
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %77, label %46
46: ; preds = %39
%47 = load i8*, i8** %42
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %77, label %50
50: ; preds = %46
%51 = load i8*, i8** %42
%52 = call i64 @strcmp(i8* %51, i8* %0)
%53 = icmp eq i64 %52, 0
br i1 %53, label %77, label %54
54: ; preds = %50
%55 = load i8*, i8** %42
%56 = call i32 @snprintf(i8* %22, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %25, i8* %55)
%57 = icmp eq i32 %56, 1024
br i1 %57, label %75, label %58
58: ; preds = %54
%59 = load i32, i32* @RTLD_NOW
%60 = call i8* @dlopen(i8* %22, i32 %59)
%61 = sext i32 %41 to i64
%62 = getelementptr inbounds [128 x i8*], [128 x i8*]* %2, i64 0, i64 %61
store i8* %60, i8** %62
%63 = icmp eq i8* %60, null
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = call i8* (...) @dlerror()
%66 = call i32 @ModelicaError(i8* %65)
br label %73
67: ; preds = %58
%68 = add nsw i32 %41, 1
%69 = icmp ugt i32 %68, 1023
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %25)
%72 = call i32 @dlclose(i8* %60)
br label %73
73: ; preds = %64, %70, %67
%74 = phi i32 [ %68, %67 ], [ %41, %70 ], [ %41, %64 ]
br label %77
75: ; preds = %54
%76 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %103
77: ; preds = %73, %50, %46, %39
%78 = phi i32 [ %41, %50 ], [ %41, %46 ], [ %41, %39 ], [ %74, %73 ]
%79 = call %struct.dirent* @readdir(i32* %34)
%80 = icmp eq %struct.dirent* %79, null
br i1 %80, label %81, label %39
81: ; preds = %77, %36
%82 = phi i32 [ %24, %36 ], [ %78, %77 ]
%83 = call i32 @closedir(i32* %34)
%84 = load i32, i32* @RTLD_NOW
%85 = call i8* @dlopen(i8* %20, i32 %84)
%86 = icmp eq i8* %85, null
br i1 %86, label %98, label %87
87: ; preds = %81
%88 = icmp sgt i32 %82, 0
br i1 %88, label %89, label %103
89: ; preds = %87
%90 = zext i32 %82 to i64
br label %91
91: ; preds = %89, %91
%92 = phi i64 [ 0, %89 ], [ %96, %91 ]
%93 = getelementptr inbounds [128 x i8*], [128 x i8*]* %2, i64 0, i64 %92
%94 = load i8*, i8** %93
%95 = call i32 @dlclose(i8* %94)
%96 = add nuw nsw i64 %92, 1
%97 = icmp eq i64 %96, %90
br i1 %97, label %103, label %91
98: ; preds = %81
%99 = call i8* (...) @dlerror()
%100 = call i32 @ModelicaError(i8* %99)
br label %105
101: ; preds = %33
%102 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %25)
br label %105
103: ; preds = %91, %87, %28, %75
%104 = phi i8* [ null, %75 ], [ null, %28 ], [ %85, %87 ], [ %85, %91 ]
br label %111
105: ; preds = %98, %101, %30
%106 = phi i32 [ %24, %30 ], [ %24, %101 ], [ %82, %98 ]
%107 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%108 = icmp eq i8* %107, null
br i1 %108, label %109, label %23
109: ; preds = %105, %16
%110 = call i32 (i8*, i8*, ...) @ModelicaFormatError(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0), i8* %0, i8* %12)
br label %111
111: ; preds = %9, %14, %109, %103
%112 = phi i8* [ null, %9 ], [ null, %14 ], [ null, %109 ], [ %104, %103 ]
%113 = phi i8* [ null, %9 ], [ null, %14 ], [ %12, %109 ], [ %12, %103 ]
%114 = call i32 @free(i8* %113)
ret i8* %112
}