#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
char *getenv(char *);
int help(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
int main(int argc, char **argv) {
  char buf[8192];
  int first_arg = 1;
  char *orig_env = getenv("LD_PRELOAD");
  if (argc < 2) {
    return help(argv[0]);
  }
  buf[0] = 0;
  for (int i = 1; i < argc; i++) {
    if (argv[i][0] != '-') {
      first_arg = i;
      break;
    } else if ((strcmp(argv[i], "-r") == 0 ||
                strcmp(argv[i], "--random") == 0) &&
               argv[++i] != NULL) {
      size_t buflen = strlen(buf);
      snprintf(buf + buflen, sizeof(buf) - buflen, "%s ", argv[i]);
    }
  }
  setenv("FREEBIND_RANDOM", buf, 1);
  snprintf(buf, sizeof(buf),
           "/usr/local/lib"
           "/"
           "freebind.so"
           "%s%s",
           orig_env ? " " : "", orig_env ? orig_env : "");
  setenv("LD_PRELOAD", buf, 1);
  execvp(argv[first_arg], &argv[first_arg]);
  snprintf(buf, sizeof(buf), "Failed to open program \"%s\"", argv[1]);
  perror(buf);
  return help(argv[0]);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x2014(%rbp)
lea    0xe56(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x2020(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d4 <main+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <help@plt>
mov    %eax,-0x4(%rbp)
jmp    1416 <main+0x296>
movb   $0x0,-0x2010(%rbp)
movl   $0x1,-0x2024(%rbp)
mov    -0x2024(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12f5 <main+0x175>
mov    -0x10(%rbp),%rax
movslq -0x2024(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     1220 <main+0xa0>
mov    -0x2024(%rbp),%eax
mov    %eax,-0x2014(%rbp)
jmp    12f5 <main+0x175>
mov    -0x10(%rbp),%rax
movslq -0x2024(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdd5(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     126a <main+0xea>
mov    -0x10(%rbp),%rax
movslq -0x2024(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdb3(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12d7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    -0x2024(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x2024(%rbp)
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     12d7 <main+0x157>
lea    -0x2010(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x2030(%rbp)
lea    -0x2010(%rbp),%rdi
add    -0x2030(%rbp),%rdi
mov    $0x2000,%eax
sub    -0x2030(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
movslq -0x2024(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xd47(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    12dc <main+0x15c>
jmp    12e1 <main+0x161>
mov    -0x2024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2024(%rbp)
jmp    11e5 <main+0x65>
lea    -0x2010(%rbp),%rsi
lea    0xd18(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2040(%rbp)
mov    -0x2020(%rbp),%rdx
lea    0xcf1(%rip),%rax
lea    0xce9(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2020(%rbp)
je     1360 <main+0x1e0>
mov    -0x2020(%rbp),%rax
mov    %rax,-0x2048(%rbp)
jmp    1373 <main+0x1f3>
lea    0xcb3(%rip),%rax
mov    %rax,-0x2048(%rbp)
jmp    1373 <main+0x1f3>
mov    -0x2038(%rbp),%rcx
mov    -0x2040(%rbp),%rdi
mov    -0x2048(%rbp),%r8
mov    $0x2000,%esi
lea    0xc97(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x2010(%rbp),%rsi
lea    0xc57(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x10(%rbp),%rax
movslq -0x2014(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x2014(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   10a0 <execvp@plt>
lea    -0x2010(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x2000,%esi
lea    0xc56(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x2010(%rbp),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <help@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe62(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x1,%r14d
jg     11c2 <main+0x42>
mov    (%rbx),%rdi
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <help@plt>
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
mov    $0x1,%r13d
lea    0xe32(%rip),%r15
jmp    11e9 <main+0x69>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
cmp    %r14d,%r13d
jge    1251 <main+0xd1>
movslq %r13d,%rbp
mov    (%rbx,%rbp,8),%rdi
cmpb   $0x2d,(%rdi)
jne    1257 <main+0xd7>
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1217 <main+0x97>
mov    (%rbx,%rbp,8),%rdi
lea    0xe01(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11e0 <main+0x60>
lea    0x1(%rbp),%r13d
mov    0x8(%rbx,%rbp,8),%r12
test   %r12,%r12
je     11e0 <main+0x60>
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    $0x2000,%esi
sub    %eax,%esi
lea    0xdd2(%rip),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    11e0 <main+0x60>
mov    $0x1,%r13d
lea    0xdbd(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    0x8(%rsp),%r8
test   %r8,%r8
lea    0xd9b(%rip),%rax
lea    0xd93(%rip),%rcx
cmove  %rax,%rcx
cmove  %rax,%r8
lea    0xd96(%rip),%rdx
mov    %rbp,%rdi
mov    $0x2000,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0xd55(%rip),%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
movslq %r13d,%rax
lea    (%rbx,%rax,8),%rsi
mov    (%rbx,%rax,8),%rdi
call   10a0 <execvp@plt>
mov    0x8(%rbx),%rcx
lea    0xd77(%rip),%rdx
mov    %rbp,%rdi
mov    $0x2000,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1080 <perror@plt>
mov    (%rbx),%rdi
call   1090 <help@plt>
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"LD_PRELOAD\00"
@.str.1 = [3 x i8] c"-r\00"
@.str.2 = [9 x i8] c"--random\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [16 x i8] c"FREEBIND_RANDOM\00"
@.str.5 = [31 x i8] c"/usr/local/lib/freebind.so%s%s\00"
@.str.6 = [2 x i8] c" \00"
@.str.7 = [1 x i8] zeroinitializer
@.str.8 = [28 x i8] c"Failed to open program \22%s\22\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [8192 x i8]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
%11 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @help(i8* %17)
store i32 %18, i32* %3
br label %120
19: ; preds = %2
%20 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
store i8 0, i8* %20
store i32 1, i32* %9
br label %21
21: ; preds = %78, %19
%22 = load i32, i32* %9
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %81
25: ; preds = %21
%26 = load i8**, i8*** %5
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 45
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = load i32, i32* %9
store i32 %36, i32* %7
br label %81
37: ; preds = %25
%38 = load i8**, i8*** %5
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %53, label %45
45: ; preds = %37
%46 = load i8**, i8*** %5
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %76
53: ; preds = %45, %37
%54 = load i8**, i8*** %5
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
store i32 %56, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8*, i8** %54, i64 %57
%59 = load i8*, i8** %58
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %76
61: ; preds = %53
%62 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%63 = call i64 @strlen(i8* %62)
store i64 %63, i64* %10
%64 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%65 = load i64, i64* %10
%66 = getelementptr inbounds i8, i8* %64, i64 %65
%67 = load i64, i64* %10
%68 = sub i64 8192, %67
%69 = trunc i64 %68 to i32
%70 = load i8**, i8*** %5
%71 = load i32, i32* %9
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
%74 = load i8*, i8** %73
%75 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %66, i32 %69, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %61, %53, %45
br label %77
77: ; preds = %76
br label %78
78: ; preds = %77
%79 = load i32, i32* %9
%80 = add nsw i32 %79, 1
store i32 %80, i32* %9
br label %21
81: ; preds = %35, %21
%82 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%83 = call i32 @setenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %82, i32 1)
%84 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%85 = load i8*, i8** %8
%86 = icmp ne i8* %85, null
%87 = zext i1 %86 to i64
%88 = select i1 %86, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0)
%89 = load i8*, i8** %8
%90 = icmp ne i8* %89, null
br i1 %90, label %91, label %93
91: ; preds = %81
%92 = load i8*, i8** %8
br label %94
93: ; preds = %81
br label %94
94: ; preds = %93, %91
%95 = phi i8* [ %92, %91 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), %93 ]
%96 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %84, i32 8192, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %88, i8* %95)
%97 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%98 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %97, i32 1)
%99 = load i8**, i8*** %5
%100 = load i32, i32* %7
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8*, i8** %99, i64 %101
%103 = load i8*, i8** %102
%104 = load i8**, i8*** %5
%105 = load i32, i32* %7
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds i8*, i8** %104, i64 %106
%108 = call i32 @execvp(i8* %103, i8** %107)
%109 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%110 = load i8**, i8*** %5
%111 = getelementptr inbounds i8*, i8** %110, i64 1
%112 = load i8*, i8** %111
%113 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %109, i32 8192, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %112)
%114 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%115 = call i32 @perror(i8* %114)
%116 = load i8**, i8*** %5
%117 = getelementptr inbounds i8*, i8** %116, i64 0
%118 = load i8*, i8** %117
%119 = call i32 @help(i8* %118)
store i32 %119, i32* %3
br label %120
120: ; preds = %94, %14
%121 = load i32, i32* %3
ret i32 %121
}

LLVM IR (-O2):
@.str = [11 x i8] c"LD_PRELOAD\00"
@.str.1 = [3 x i8] c"-r\00"
@.str.2 = [9 x i8] c"--random\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [16 x i8] c"FREEBIND_RANDOM\00"
@.str.5 = [31 x i8] c"/usr/local/lib/freebind.so%s%s\00"
@.str.6 = [2 x i8] c" \00"
@.str.7 = [1 x i8] zeroinitializer
@.str.8 = [28 x i8] c"Failed to open program \22%s\22\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 @help(i8* %8)
br label %59
10: ; preds = %2
store i8 0, i8* %4
br label %11
11: ; preds = %10, %37
%12 = phi i32 [ %39, %37 ], [ 1, %10 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 45
br i1 %17, label %18, label %41
18: ; preds = %11
%19 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i8*, i8** %14
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %21, %18
%26 = add nsw i32 %12, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %1, i64 %27
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = call i64 @strlen(i8* %4)
%33 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 %32
%34 = trunc i64 %32 to i32
%35 = sub i32 8192, %34
%36 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %33, i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %37
37: ; preds = %31, %25, %21
%38 = phi i32 [ %26, %31 ], [ %26, %25 ], [ %12, %21 ]
%39 = add nsw i32 %38, 1
%40 = icmp slt i32 %39, %0
br i1 %40, label %11, label %41
41: ; preds = %37, %11
%42 = phi i32 [ 1, %37 ], [ %12, %11 ]
%43 = call i32 @setenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32 1)
%44 = icmp eq i8* %5, null
%45 = select i1 %44, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0)
%46 = select i1 %44, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i8* %5
%47 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %4, i32 8192, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %45, i8* %46)
%48 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %4, i32 1)
%49 = sext i32 %42 to i64
%50 = getelementptr inbounds i8*, i8** %1, i64 %49
%51 = load i8*, i8** %50
%52 = call i32 @execvp(i8* %51, i8** %50)
%53 = getelementptr inbounds i8*, i8** %1, i64 1
%54 = load i8*, i8** %53
%55 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %4, i32 8192, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %54)
%56 = call i32 @perror(i8* %4)
%57 = load i8*, i8** %1
%58 = call i32 @help(i8* %57)
br label %59
59: ; preds = %41, %7
%60 = phi i32 [ %9, %7 ], [ %58, %41 ]
ret i32 %60
}