#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 st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
scalar_t__ memcmp(char *, char const *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int strlen(char const *);
int main(int argc, char **argv) {
  char *path;
  int path_length;
  int imports_count;
  char **imports;
  int *imports_lengths;
  struct stat st;
  char *buffer;
  FILE *fp;
  int i, k;
  const char *prefix = "_lv_";
  int prefix_length = strlen(prefix);
  if (argc < 3) {
    printf("usage: %s <dll> <import>...", argv[0]);
    return 1;
  }
  path = argv[1];
  path_length = strlen(path);
  imports_count = argc - 2;
  imports = malloc(imports_count * sizeof(char *));
  imports_lengths = malloc(imports_count * sizeof(int));
  for (i = 2, k = 0; i < argc; ++i) {
    if (path_length >= strlen(argv[i]) &&
        strcmp(path + path_length - strlen(argv[i]), argv[i]) == 0) {
      --imports_count;
      continue;
    }
    imports[k] = argv[i];
    imports_lengths[k] = strlen(argv[i]);
    ++k;
  }
  if (stat(path, &st) < 0) {
    printf("error: could not stat '%s'\n", path);
    return 1;
  }
  buffer = malloc(st.st_size + 1024);
  fp = fopen(path, "rb");
  if (fp == NULL) {
    printf("error: could not open '%s' for reading\n", path);
    return 1;
  }
  if (fread(buffer, 1, st.st_size, fp) != st.st_size) {
    fclose(fp);
    free(buffer);
    printf("error: could not read from '%s'\n", path);
    return 1;
  }
  fclose(fp);
  for (i = 0; i < st.st_size - 100; ++i) {
    for (k = 0; k < imports_count; ++k) {
      if (memcmp(buffer + i, imports[k], imports_lengths[k]) == 0) {
        if (i > prefix_length &&
            memcmp(buffer + i - prefix_length, prefix, prefix_length) == 0) {
          printf("ignoring already rewritten '%s' at 0x%08x\n", imports[k], i);
          continue;
        }
        printf("rewriting '%s' at 0x%08x\n", imports[k], i);
        memcpy(buffer + i, prefix, prefix_length);
        i += prefix_length;
        memcpy(buffer + i, imports[k], imports_lengths[k]);
        i += imports_lengths[k];
        memcpy(buffer + i, "\0\0\0", 3);
        i += 3;
      }
    }
  }
  fp = fopen(path, "wb");
  if (fp == NULL) {
    free(buffer);
    printf("error: could not open '%s' for writing\n", path);
    return 1;
  }
  if (fwrite(buffer, 1, st.st_size, fp) != st.st_size) {
    fclose(fp);
    free(buffer);
    printf("error: could not write to '%s'\n", path);
    return 1;
  }
  fclose(fp);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe23(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1218 <main+0x58>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1641 <main+0x481>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x2,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1342 <main+0x182>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%eax
cmp    %ecx,%eax
jl     12f5 <main+0x135>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
mov    -0x68(%rbp),%rdi
movslq %eax,%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    12f5 <main+0x135>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    1334 <main+0x174>
mov    -0x10(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x28(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    126d <main+0xad>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jge    1377 <main+0x1b7>
mov    -0x18(%rbp),%rsi
lea    0xcbd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1641 <main+0x481>
mov    -0x38(%rbp),%edi
add    $0x400,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xca9(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    13c6 <main+0x206>
mov    -0x18(%rbp),%rsi
lea    0xc8d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1641 <main+0x481>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x38(%rbp),%eax
je     1414 <main+0x254>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rsi
lea    0xc67(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1641 <main+0x481>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    -0x38(%rbp),%ecx
sub    $0x64,%ecx
cmp    %ecx,%eax
jge    1594 <main+0x3d4>
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1581 <main+0x3c1>
mov    -0x40(%rbp),%rdi
movslq -0x4c(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x30(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    156e <main+0x3ae>
mov    -0x4c(%rbp),%eax
cmp    -0x5c(%rbp),%eax
jle    14d4 <main+0x314>
mov    -0x40(%rbp),%rdi
movslq -0x4c(%rbp),%rax
add    %rax,%rdi
movslq -0x5c(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x58(%rbp),%rsi
mov    -0x5c(%rbp),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    14d4 <main+0x314>
mov    -0x28(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x4c(%rbp),%edx
lea    0xbc1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1573 <main+0x3b3>
mov    -0x28(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x4c(%rbp),%edx
lea    0xbca(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
movslq -0x4c(%rbp),%rax
add    %rax,%rdi
mov    -0x58(%rbp),%rsi
mov    -0x5c(%rbp),%edx
call   10b0 <memcpy@plt>
mov    -0x5c(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rdi
movslq -0x4c(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x30(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
call   10b0 <memcpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rdi
movslq -0x4c(%rbp),%rax
add    %rax,%rdi
lea    0xbbe(%rip),%rsi
mov    $0x3,%edx
call   10b0 <memcpy@plt>
mov    -0x4c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x4c(%rbp)
jmp    1573 <main+0x3b3>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    143c <main+0x27c>
jmp    1586 <main+0x3c6>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1424 <main+0x264>
mov    -0x18(%rbp),%rdi
lea    0xb2f(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    15da <main+0x41a>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rsi
lea    0xb0a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1641 <main+0x481>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
cmp    -0x38(%rbp),%eax
je     1628 <main+0x468>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rsi
lea    0xae4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1641 <main+0x481>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r13
mov    %edi,%r15d
lea    0xe25(%rip),%rdi
call   1060 <strlen@plt>
cmp    $0x2,%r15d
jg     11f6 <main+0x36>
mov    0x0(%r13),%rsi
lea    0xe14(%rip),%rdi
jmp    1510 <main+0x350>
mov    %eax,0x14(%rsp)
mov    0x8(%r13),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
lea    -0x2(%r15),%eax
mov    %eax,0x10(%rsp)
lea    -0x10(,%r15,8),%edi
ff
call   10c0 <malloc@plt>
mov    %rax,0x30(%rsp)
lea    -0x8(,%r15,4),%edi
ff
call   10c0 <malloc@plt>
mov    %rax,0x28(%rsp)
movslq %r14d,%r12
mov    %rbx,0x20(%rsp)
add    %rbx,%r12
mov    %r15d,%r15d
xor    %ebp,%ebp
mov    $0x2,%ebx
jmp    127a <main+0xba>
nopl   0x0(%rax)
mov    0x0(%r13,%rbx,8),%rdi
movslq %ebp,%rbp
mov    0x30(%rsp),%rax
mov    %rdi,(%rax,%rbp,8)
call   1060 <strlen@plt>
mov    0x28(%rsp),%rcx
mov    %eax,(%rcx,%rbp,4)
add    $0x1,%ebp
add    $0x1,%rbx
cmp    %rbx,%r15
je     12b1 <main+0xf1>
mov    0x0(%r13,%rbx,8),%rdi
call   1060 <strlen@plt>
cmp    %eax,%r14d
jl     1250 <main+0x90>
mov    0x0(%r13,%rbx,8),%rdi
call   1060 <strlen@plt>
cltq
mov    %r12,%rdi
sub    %rax,%rdi
mov    0x0(%r13,%rbx,8),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1250 <main+0x90>
addl   $0xffffffff,0x10(%rsp)
jmp    1271 <main+0xb1>
lea    0x8(%rsp),%rsi
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <stat@plt>
test   %rax,%rax
js     146a <main+0x2aa>
mov    $0x400,%edi
add    0x8(%rsp),%edi
call   10c0 <malloc@plt>
mov    %rax,%r13
lea    0xd59(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1479 <main+0x2b9>
mov    %rax,%r14
mov    0x8(%rsp),%edx
mov    $0x1,%ebp
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
mov    0x8(%rsp),%r12d
mov    %r14,%rdi
call   1050 <fclose@plt>
cmp    %r12d,%r15d
jne    1488 <main+0x2c8>
mov    0x8(%rsp),%eax
cmp    $0x65,%eax
mov    0x14(%rsp),%ebx
mov    %r13,%r15
jl     14a3 <main+0x2e3>
movslq %ebx,%rcx
neg    %rcx
mov    %rcx,0x38(%rsp)
mov    0x10(%rsp),%ecx
mov    %rcx,0x40(%rsp)
xor    %r12d,%r12d
mov    %r15,0x18(%rsp)
jmp    1374 <main+0x1b4>
nopl   0x0(%rax)
mov    0x8(%rsp),%eax
add    $0x1,%r12d
lea    -0x64(%rax),%ecx
cmp    %ecx,%r12d
jge    14a3 <main+0x2e3>
cmpl   $0x0,0x10(%rsp)
jle    1364 <main+0x1a4>
xor    %ebp,%ebp
jmp    13b0 <main+0x1f0>
lea    0xd03(%rip),%rdi
mov    %r15,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rsp),%r15
add    $0x1,%rbp
cmp    %rbp,0x40(%rsp)
je     1360 <main+0x1a0>
movslq %r12d,%r13
add    %r15,%r13
mov    0x30(%rsp),%rax
mov    (%rax,%rbp,8),%r15
mov    0x28(%rsp),%rax
mov    (%rax,%rbp,4),%r14d
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    13a0 <main+0x1e0>
cmp    %ebx,%r12d
jle    13fc <main+0x23c>
mov    0x38(%rsp),%rax
lea    (%rax,%r13,1),%rdi
lea    0xc10(%rip),%rsi
mov    %ebx,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     137f <main+0x1bf>
lea    0xcb1(%rip),%rdi
mov    %r15,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
lea    0xbe6(%rip),%rsi
mov    %ebx,%edx
call   10b0 <memcpy@plt>
add    %ebx,%r12d
movslq %r12d,%rbx
mov    0x18(%rsp),%rax
lea    (%rax,%rbx,1),%rdi
mov    %r15,%rsi
mov    %rax,%r15
mov    %r14d,%edx
call   10b0 <memcpy@plt>
lea    (%r14,%rbx,1),%eax
movslq %eax,%rdi
add    %r15,%rdi
lea    0xcca(%rip),%rsi
mov    $0x3,%edx
call   10b0 <memcpy@plt>
lea    (%r14,%rbx,1),%r12d
add    $0x3,%r12d
mov    0x14(%rsp),%ebx
jmp    13a5 <main+0x1e5>
lea    0xbb0(%rip),%rdi
mov    %rbx,%rsi
jmp    1510 <main+0x350>
lea    0xbc0(%rip),%rdi
mov    %rbx,%rsi
jmp    1510 <main+0x350>
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xbd1(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    151c <main+0x35c>
lea    0xc24(%rip),%rsi
mov    0x20(%rsp),%r12
mov    %r12,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     14fe <main+0x33e>
mov    %rax,%rbp
mov    0x8(%rsp),%edx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %eax,%r14d
mov    %r15,%rbx
mov    0x8(%rsp),%r15d
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
cmp    %r15d,%r14d
je     151c <main+0x35c>
lea    0xbfd(%rip),%rdi
jmp    150d <main+0x34d>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xbc4(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"_lv_\00"
@.str.1 = [28 x i8] c"usage: %s <dll> <import>...\00"
@.str.2 = [28 x i8] c"error: could not stat '%s'\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [40 x i8] c"error: could not open '%s' for reading\0A\00"
@.str.5 = [33 x i8] c"error: could not read from '%s'\0A\00"
@.str.6 = [43 x i8] c"ignoring already rewritten '%s' at 0x%08x\0A\00"
@.str.7 = [26 x i8] c"rewriting '%s' at 0x%08x\0A\00"
@.str.8 = [4 x i8] zeroinitializer
@.str.9 = [3 x i8] c"wb\00"
@.str.10 = [40 x i8] c"error: could not open '%s' for writing\0A\00"
@.str.11 = [32 x i8] c"error: could not write to '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32*
%11 = alloca %struct.stat
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %16
%18 = load i8*, i8** %16
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %17
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %27
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %286
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = call i32 @strlen(i8* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %4
%34 = sub nsw i32 %33, 2
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 8
%38 = trunc i64 %37 to i32
%39 = call i8* @malloc(i32 %38)
%40 = bitcast i8* %39 to i8**
store i8** %40, i8*** %9
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 4
%44 = trunc i64 %43 to i32
%45 = call i8* @malloc(i32 %44)
%46 = bitcast i8* %45 to i32*
store i32* %46, i32** %10
store i32 2, i32* %14
store i32 0, i32* %15
br label %47
47: ; preds = %106, %27
%48 = load i32, i32* %14
%49 = load i32, i32* %4
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %109
51: ; preds = %47
%52 = load i32, i32* %7
%53 = load i8**, i8*** %5
%54 = load i32, i32* %14
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @strlen(i8* %57)
%59 = icmp sge i32 %52, %58
br i1 %59, label %60, label %84
60: ; preds = %51
%61 = load i8*, i8** %6
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8**, i8*** %5
%66 = load i32, i32* %14
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8*, i8** %65, i64 %67
%69 = load i8*, i8** %68
%70 = call i32 @strlen(i8* %69)
%71 = sext i32 %70 to i64
%72 = sub i64 0, %71
%73 = getelementptr inbounds i8, i8* %64, i64 %72
%74 = load i8**, i8*** %5
%75 = load i32, i32* %14
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8*, i8** %74, i64 %76
%78 = load i8*, i8** %77
%79 = call i64 @strcmp(i8* %73, i8* %78)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %60
%82 = load i32, i32* %8
%83 = add nsw i32 %82, -1
store i32 %83, i32* %8
br label %106
84: ; preds = %60, %51
%85 = load i8**, i8*** %5
%86 = load i32, i32* %14
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8*, i8** %85, i64 %87
%89 = load i8*, i8** %88
%90 = load i8**, i8*** %9
%91 = load i32, i32* %15
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8*, i8** %90, i64 %92
store i8* %89, i8** %93
%94 = load i8**, i8*** %5
%95 = load i32, i32* %14
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8*, i8** %94, i64 %96
%98 = load i8*, i8** %97
%99 = call i32 @strlen(i8* %98)
%100 = load i32*, i32** %10
%101 = load i32, i32* %15
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i32, i32* %100, i64 %102
store i32 %99, i32* %103
%104 = load i32, i32* %15
%105 = add nsw i32 %104, 1
store i32 %105, i32* %15
br label %106
106: ; preds = %84, %81
%107 = load i32, i32* %14
%108 = add nsw i32 %107, 1
store i32 %108, i32* %14
br label %47
109: ; preds = %47
%110 = load i8*, i8** %6
%111 = call i64 @stat(i8* %110, %struct.stat* %11)
%112 = icmp slt i64 %111, 0
br i1 %112, label %113, label %116
113: ; preds = %109
%114 = load i8*, i8** %6
%115 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %114)
store i32 1, i32* %3
br label %286
116: ; preds = %109
%117 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%118 = load i32, i32* %117
%119 = add nsw i32 %118, 1024
%120 = call i8* @malloc(i32 %119)
store i8* %120, i8** %12
%121 = load i8*, i8** %6
%122 = call i32* @fopen(i8* %121, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %122, i32** %13
%123 = load i32*, i32** %13
%124 = icmp eq i32* %123, null
br i1 %124, label %125, label %128
125: ; preds = %116
%126 = load i8*, i8** %6
%127 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %126)
store i32 1, i32* %3
br label %286
128: ; preds = %116
%129 = load i8*, i8** %12
%130 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%131 = load i32, i32* %130
%132 = load i32*, i32** %13
%133 = call i32 @fread(i8* %129, i32 1, i32 %131, i32* %132)
%134 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%135 = load i32, i32* %134
%136 = icmp ne i32 %133, %135
br i1 %136, label %137, label %144
137: ; preds = %128
%138 = load i32*, i32** %13
%139 = call i32 @fclose(i32* %138)
%140 = load i8*, i8** %12
%141 = call i32 @free(i8* %140)
%142 = load i8*, i8** %6
%143 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %142)
store i32 1, i32* %3
br label %286
144: ; preds = %128
%145 = load i32*, i32** %13
%146 = call i32 @fclose(i32* %145)
store i32 0, i32* %14
br label %147
147: ; preds = %252, %144
%148 = load i32, i32* %14
%149 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%150 = load i32, i32* %149
%151 = sub nsw i32 %150, 100
%152 = icmp slt i32 %148, %151
br i1 %152, label %153, label %255
153: ; preds = %147
store i32 0, i32* %15
br label %154
154: ; preds = %248, %153
%155 = load i32, i32* %15
%156 = load i32, i32* %8
%157 = icmp slt i32 %155, %156
br i1 %157, label %158, label %251
158: ; preds = %154
%159 = load i8*, i8** %12
%160 = load i32, i32* %14
%161 = sext i32 %160 to i64
%162 = getelementptr inbounds i8, i8* %159, i64 %161
%163 = load i8**, i8*** %9
%164 = load i32, i32* %15
%165 = sext i32 %164 to i64
%166 = getelementptr inbounds i8*, i8** %163, i64 %165
%167 = load i8*, i8** %166
%168 = load i32*, i32** %10
%169 = load i32, i32* %15
%170 = sext i32 %169 to i64
%171 = getelementptr inbounds i32, i32* %168, i64 %170
%172 = load i32, i32* %171
%173 = call i64 @memcmp(i8* %162, i8* %167, i32 %172)
%174 = icmp eq i64 %173, 0
br i1 %174, label %175, label %247
175: ; preds = %158
%176 = load i32, i32* %14
%177 = load i32, i32* %17
%178 = icmp sgt i32 %176, %177
br i1 %178, label %179, label %200
179: ; preds = %175
%180 = load i8*, i8** %12
%181 = load i32, i32* %14
%182 = sext i32 %181 to i64
%183 = getelementptr inbounds i8, i8* %180, i64 %182
%184 = load i32, i32* %17
%185 = sext i32 %184 to i64
%186 = sub i64 0, %185
%187 = getelementptr inbounds i8, i8* %183, i64 %186
%188 = load i8*, i8** %16
%189 = load i32, i32* %17
%190 = call i64 @memcmp(i8* %187, i8* %188, i32 %189)
%191 = icmp eq i64 %190, 0
br i1 %191, label %192, label %200
192: ; preds = %179
%193 = load i8**, i8*** %9
%194 = load i32, i32* %15
%195 = sext i32 %194 to i64
%196 = getelementptr inbounds i8*, i8** %193, i64 %195
%197 = load i8*, i8** %196
%198 = load i32, i32* %14
%199 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %197, i32 %198)
br label %248
200: ; preds = %179, %175
%201 = load i8**, i8*** %9
%202 = load i32, i32* %15
%203 = sext i32 %202 to i64
%204 = getelementptr inbounds i8*, i8** %201, i64 %203
%205 = load i8*, i8** %204
%206 = load i32, i32* %14
%207 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %205, i32 %206)
%208 = load i8*, i8** %12
%209 = load i32, i32* %14
%210 = sext i32 %209 to i64
%211 = getelementptr inbounds i8, i8* %208, i64 %210
%212 = load i8*, i8** %16
%213 = load i32, i32* %17
%214 = call i32 @memcpy(i8* %211, i8* %212, i32 %213)
%215 = load i32, i32* %17
%216 = load i32, i32* %14
%217 = add nsw i32 %216, %215
store i32 %217, i32* %14
%218 = load i8*, i8** %12
%219 = load i32, i32* %14
%220 = sext i32 %219 to i64
%221 = getelementptr inbounds i8, i8* %218, i64 %220
%222 = load i8**, i8*** %9
%223 = load i32, i32* %15
%224 = sext i32 %223 to i64
%225 = getelementptr inbounds i8*, i8** %222, i64 %224
%226 = load i8*, i8** %225
%227 = load i32*, i32** %10
%228 = load i32, i32* %15
%229 = sext i32 %228 to i64
%230 = getelementptr inbounds i32, i32* %227, i64 %229
%231 = load i32, i32* %230
%232 = call i32 @memcpy(i8* %221, i8* %226, i32 %231)
%233 = load i32*, i32** %10
%234 = load i32, i32* %15
%235 = sext i32 %234 to i64
%236 = getelementptr inbounds i32, i32* %233, i64 %235
%237 = load i32, i32* %236
%238 = load i32, i32* %14
%239 = add nsw i32 %238, %237
store i32 %239, i32* %14
%240 = load i8*, i8** %12
%241 = load i32, i32* %14
%242 = sext i32 %241 to i64
%243 = getelementptr inbounds i8, i8* %240, i64 %242
%244 = call i32 @memcpy(i8* %243, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 3)
%245 = load i32, i32* %14
%246 = add nsw i32 %245, 3
store i32 %246, i32* %14
br label %247
247: ; preds = %200, %158
br label %248
248: ; preds = %247, %192
%249 = load i32, i32* %15
%250 = add nsw i32 %249, 1
store i32 %250, i32* %15
br label %154
251: ; preds = %154
br label %252
252: ; preds = %251
%253 = load i32, i32* %14
%254 = add nsw i32 %253, 1
store i32 %254, i32* %14
br label %147
255: ; preds = %147
%256 = load i8*, i8** %6
%257 = call i32* @fopen(i8* %256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0))
store i32* %257, i32** %13
%258 = load i32*, i32** %13
%259 = icmp eq i32* %258, null
br i1 %259, label %260, label %265
260: ; preds = %255
%261 = load i8*, i8** %12
%262 = call i32 @free(i8* %261)
%263 = load i8*, i8** %6
%264 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.10, i64 0, i64 0), i8* %263)
store i32 1, i32* %3
br label %286
265: ; preds = %255
%266 = load i8*, i8** %12
%267 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%268 = load i32, i32* %267
%269 = load i32*, i32** %13
%270 = call i32 @fwrite(i8* %266, i32 1, i32 %268, i32* %269)
%271 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%272 = load i32, i32* %271
%273 = icmp ne i32 %270, %272
br i1 %273, label %274, label %281
274: ; preds = %265
%275 = load i32*, i32** %13
%276 = call i32 @fclose(i32* %275)
%277 = load i8*, i8** %12
%278 = call i32 @free(i8* %277)
%279 = load i8*, i8** %6
%280 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i8* %279)
store i32 1, i32* %3
br label %286
281: ; preds = %265
%282 = load i32*, i32** %13
%283 = call i32 @fclose(i32* %282)
%284 = load i8*, i8** %12
%285 = call i32 @free(i8* %284)
store i32 0, i32* %3
br label %286
286: ; preds = %281, %274, %260, %137, %125, %113, %22
%287 = load i32, i32* %3
ret i32 %287
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"_lv_\00"
@.str.1 = [28 x i8] c"usage: %s <dll> <import>...\00"
@.str.2 = [28 x i8] c"error: could not stat '%s'\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [40 x i8] c"error: could not open '%s' for reading\0A\00"
@.str.5 = [33 x i8] c"error: could not read from '%s'\0A\00"
@.str.6 = [43 x i8] c"ignoring already rewritten '%s' at 0x%08x\0A\00"
@.str.7 = [26 x i8] c"rewriting '%s' at 0x%08x\0A\00"
@.str.8 = [4 x i8] zeroinitializer
@.str.9 = [3 x i8] c"wb\00"
@.str.10 = [40 x i8] c"error: could not open '%s' for writing\0A\00"
@.str.11 = [32 x i8] c"error: could not write to '%s'\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %147
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @strlen(i8* %12)
%14 = add nsw i32 %0, -2
%15 = shl i32 %14, 3
%16 = tail call i8* @malloc(i32 %15)
%17 = bitcast i8* %16 to i8**
%18 = shl i32 %14, 2
%19 = tail call i8* @malloc(i32 %18)
%20 = bitcast i8* %19 to i32*
%21 = sext i32 %13 to i64
%22 = getelementptr inbounds i8, i8* %12, i64 %21
%23 = zext i32 %0 to i64
br label %24
24: ; preds = %10, %50
%25 = phi i64 [ 2, %10 ], [ %53, %50 ]
%26 = phi i32 [ %14, %10 ], [ %52, %50 ]
%27 = phi i32 [ 0, %10 ], [ %51, %50 ]
%28 = getelementptr inbounds i8*, i8** %1, i64 %25
%29 = load i8*, i8** %28
%30 = tail call i32 @strlen(i8* %29)
%31 = icmp slt i32 %13, %30
br i1 %31, label %43, label %32
32: ; preds = %24
%33 = load i8*, i8** %28
%34 = tail call i32 @strlen(i8* %33)
%35 = sext i32 %34 to i64
%36 = sub nsw i64 0, %35
%37 = getelementptr inbounds i8, i8* %22, i64 %36
%38 = load i8*, i8** %28
%39 = tail call i64 @strcmp(i8* %37, i8* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = add nsw i32 %26, -1
br label %50
43: ; preds = %32, %24
%44 = load i8*, i8** %28
%45 = sext i32 %27 to i64
%46 = getelementptr inbounds i8*, i8** %17, i64 %45
store i8* %44, i8** %46
%47 = tail call i32 @strlen(i8* %44)
%48 = getelementptr inbounds i32, i32* %20, i64 %45
store i32 %47, i32* %48
%49 = add nsw i32 %27, 1
br label %50
50: ; preds = %43, %41
%51 = phi i32 [ %27, %41 ], [ %49, %43 ]
%52 = phi i32 [ %42, %41 ], [ %26, %43 ]
%53 = add nuw nsw i64 %25, 1
%54 = icmp eq i64 %53, %23
br i1 %54, label %55, label %24
55: ; preds = %50
%56 = call i64 @stat(i8* %12, %struct.stat* %3)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %147
60: ; preds = %55
%61 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%62 = load i32, i32* %61
%63 = add nsw i32 %62, 1024
%64 = call i8* @malloc(i32 %63)
%65 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%66 = icmp eq i32* %65, null
br i1 %66, label %67, label %69
67: ; preds = %60
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %12)
br label %147
69: ; preds = %60
%70 = load i32, i32* %61
%71 = call i32 @fread(i8* %64, i32 1, i32 %70, i32* %65)
%72 = load i32, i32* %61
%73 = icmp eq i32 %71, %72
%74 = call i32 @fclose(i32* %65)
br i1 %73, label %78, label %75
75: ; preds = %69
%76 = call i32 @free(i8* %64)
%77 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %12)
br label %147
78: ; preds = %69
%79 = load i32, i32* %61
%80 = icmp sgt i32 %79, 100
br i1 %80, label %81, label %132
81: ; preds = %78
%82 = icmp sgt i32 %52, 0
%83 = sext i32 %5 to i64
%84 = sub nsw i64 0, %83
%85 = zext i32 %52 to i64
br label %86
86: ; preds = %81, %126
%87 = phi i32 [ %79, %81 ], [ %127, %126 ]
%88 = phi i32 [ 0, %81 ], [ %129, %126 ]
br i1 %82, label %89, label %126
89: ; preds = %86, %120
%90 = phi i64 [ %122, %120 ], [ 0, %86 ]
%91 = phi i32 [ %121, %120 ], [ %88, %86 ]
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %64, i64 %92
%94 = getelementptr inbounds i8*, i8** %17, i64 %90
%95 = load i8*, i8** %94
%96 = getelementptr inbounds i32, i32* %20, i64 %90
%97 = load i32, i32* %96
%98 = call i64 @memcmp(i8* %93, i8* %95, i32 %97)
%99 = icmp eq i64 %98, 0
br i1 %99, label %100, label %120
100: ; preds = %89
%101 = icmp sgt i32 %91, %5
br i1 %101, label %102, label %108
102: ; preds = %100
%103 = getelementptr inbounds i8, i8* %93, i64 %84
%104 = call i64 @memcmp(i8* %103, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %5)
%105 = icmp eq i64 %104, 0
br i1 %105, label %106, label %108
106: ; preds = %102
%107 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %95, i32 %91)
br label %120
108: ; preds = %102, %100
%109 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %95, i32 %91)
%110 = call i32 @memcpy(i8* %93, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %5)
%111 = add nsw i32 %91, %5
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds i8, i8* %64, i64 %112
%114 = call i32 @memcpy(i8* %113, i8* %95, i32 %97)
%115 = add nsw i32 %97, %111
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds i8, i8* %64, i64 %116
%118 = call i32 @memcpy(i8* %117, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 3)
%119 = add nsw i32 %115, 3
br label %120
120: ; preds = %89, %108, %106
%121 = phi i32 [ %91, %106 ], [ %119, %108 ], [ %91, %89 ]
%122 = add nuw nsw i64 %90, 1
%123 = icmp eq i64 %122, %85
br i1 %123, label %124, label %89
124: ; preds = %120
%125 = load i32, i32* %61
br label %126
126: ; preds = %124, %86
%127 = phi i32 [ %87, %86 ], [ %125, %124 ]
%128 = phi i32 [ %88, %86 ], [ %121, %124 ]
%129 = add nsw i32 %128, 1
%130 = add nsw i32 %127, -100
%131 = icmp slt i32 %129, %130
br i1 %131, label %86, label %132
132: ; preds = %126, %78
%133 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0))
%134 = icmp eq i32* %133, null
br i1 %134, label %135, label %138
135: ; preds = %132
%136 = call i32 @free(i8* %64)
%137 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.10, i64 0, i64 0), i8* %12)
br label %147
138: ; preds = %132
%139 = load i32, i32* %61
%140 = call i32 @fwrite(i8* %64, i32 1, i32 %139, i32* %133)
%141 = load i32, i32* %61
%142 = icmp eq i32 %140, %141
%143 = call i32 @fclose(i32* %133)
%144 = call i32 @free(i8* %64)
br i1 %142, label %147, label %145
145: ; preds = %138
%146 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i8* %12)
br label %147
147: ; preds = %138, %145, %135, %75, %67, %58, %7
%148 = phi i32 [ 1, %7 ], [ 1, %58 ], [ 1, %67 ], [ 1, %75 ], [ 1, %135 ], [ 1, %145 ], [ 0, %138 ]
ret i32 %148
}