#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 */

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
int /*<<< orphan*/ print_usage();
int /*<<< orphan*/ printf(char *, ...);
char *realpath(char *, int /*<<< orphan*/ *);
scalar_t__ setenv(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unsetenv(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int cid;
  int status;
  char *env;
  char *cwd;
  char *args[2] = {"bash", NULL};
  char *sourcepath;
  char *destpath;
  if (argc != 3) {
    print_usage();
    return 0;
  }
  sourcepath = realpath(argv[1], NULL);
  destpath = realpath(argv[2], NULL);
  if (!sourcepath || !destpath) {
    fprintf(stderr, "Error reading dir paths\n");
    return 1;
  }
  printf("Source: %s, Dest: %s\n", sourcepath, destpath);
  cwd = get_current_dir_name();
  if (!cwd) {
    return 1;
  }
  asprintf(&env, "%s/libpurelibc.so:%s/libdirewrite.so", cwd, cwd);
  if (setenv("LD_PRELOAD", env, 1) < 0) {
    fprintf(stderr, "setenv failed: %s\n", strerror(errno));
    ;
    free(cwd);
    free(env);
    return 1;
  }
  setenv("LDRW_SOURCE", sourcepath, 1);
  setenv("LDRW_DEST", destpath, 1);
  cid = fork();
  if (!cid) {
    execvp("bash", args);
    return 0;
  } else {
    wait(&status);
  }
  printf("All over now.\n");
  free(sourcepath);
  free(destpath);
  free(cwd);
  free(env);
  unsetenv("LD_PRELOAD");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c13(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c10(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1219 <main+0x49>
mov    $0x0,%al
call   10c0 <print_usage@plt>
movl   $0x0,-0x4(%rbp)
jmp    13df <main+0x20f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1259 <main+0x89>
cmpq   $0x0,-0x50(%rbp)
jne    127c <main+0xac>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13df <main+0x20f>
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <get_current_dir_name@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b4 <main+0xe4>
movl   $0x1,-0x4(%rbp)
jmp    13df <main+0x20f>
mov    -0x28(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xd6d(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x20(%rbp),%rsi
lea    0xd82(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0x0,%rax
jge    1337 <main+0x167>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    13df <main+0x20f>
mov    -0x48(%rbp),%rsi
lea    0xd35(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x50(%rbp),%rsi
lea    0xd2c(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1391 <main+0x1c1>
lea    -0x40(%rbp),%rsi
lea    0xc80(%rip),%rdi
call   10b0 <execvp@plt>
movl   $0x0,-0x4(%rbp)
jmp    13df <main+0x20f>
lea    -0x18(%rbp),%rdi
call   10e0 <wait@plt>
lea    0xcec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xc86(%rip),%rdi
call   10a0 <unsetenv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
movaps 0x2c0d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x3,%edi
jne    130f <main+0x12f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
xor    %esi,%esi
call   1080 <realpath@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
xor    %esi,%esi
call   1080 <realpath@plt>
test   %r14,%r14
je     131a <main+0x13a>
mov    %rax,%r12
test   %rax,%rax
je     131a <main+0x13a>
lea    0xde5(%rip),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <get_current_dir_name@plt>
test   %rax,%rax
je     1331 <main+0x151>
mov    %rax,%r15
lea    0xdd4(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
mov    %rax,%rcx
call   10a0 <asprintf@plt>
mov    (%rsp),%rsi
lea    0xde0(%rip),%rdi
mov    $0x1,%ebx
mov    $0x1,%edx
call   1050 <setenv@plt>
test   %rax,%rax
js     1345 <main+0x165>
lea    0xddf(%rip),%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xdd7(%rip),%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1382 <main+0x1a2>
lea    0xc(%rsp),%rdi
call   10f0 <wait@plt>
lea    0xdb2(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
lea    0xd51(%rip),%rdi
call   10b0 <unsetenv@plt>
jmp    1336 <main+0x156>
xor    %ebx,%ebx
xor    %eax,%eax
call   10d0 <print_usage@plt>
jmp    1336 <main+0x156>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    0xcdb(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c94(%rip),%rax
mov    (%rax),%ebp
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd01(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    1336 <main+0x156>
lea    0xc77(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10c0 <execvp@plt>
jmp    1336 <main+0x156>

LLVM IR (-O0):
@.str = [5 x i8] c"bash\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error reading dir paths\0A\00"
@.str.2 = [22 x i8] c"Source: %s, Dest: %s\0A\00"
@.str.3 = [37 x i8] c"%s/libpurelibc.so:%s/libdirewrite.so\00"
@.str.4 = [11 x i8] c"LD_PRELOAD\00"
@.str.5 = [19 x i8] c"setenv failed: %s\0A\00"
@errno = global i32 0
@.str.6 = [12 x i8] c"LDRW_SOURCE\00"
@.str.7 = [10 x i8] c"LDRW_DEST\00"
@.str.8 = [15 x i8] c"All over now.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [2 x i8*]
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [2 x i8*]* %10 to i8*
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @print_usage()
store i32 0, i32* %3
br label %83
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i8* @realpath(i8* %21, i32* null)
store i8* %22, i8** %11
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i8* @realpath(i8* %25, i32* null)
store i8* %26, i8** %12
%27 = load i8*, i8** %11
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = load i8*, i8** %12
%31 = icmp ne i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %29, %18
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %83
35: ; preds = %29
%36 = load i8*, i8** %11
%37 = load i8*, i8** %12
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %37)
%39 = call i8* (...) @get_current_dir_name()
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = icmp ne i8* %40, null
br i1 %41, label %43, label %42
42: ; preds = %35
store i32 1, i32* %3
br label %83
43: ; preds = %35
%44 = load i8*, i8** %9
%45 = load i8*, i8** %9
%46 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %45)
%47 = load i8*, i8** %8
%48 = call i64 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %47, i32 1)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %43
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %53)
%55 = load i8*, i8** %9
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %8
%58 = call i32 @free(i8* %57)
store i32 1, i32* %3
br label %83
59: ; preds = %43
%60 = load i8*, i8** %11
%61 = call i64 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %60, i32 1)
%62 = load i8*, i8** %12
%63 = call i64 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %62, i32 1)
%64 = call i32 (...) @fork()
store i32 %64, i32* %6
%65 = load i32, i32* %6
%66 = icmp ne i32 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %59
%68 = getelementptr inbounds [2 x i8*], [2 x i8*]* %10, i64 0, i64 0
%69 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %68)
store i32 0, i32* %3
br label %83
70: ; preds = %59
%71 = call i32 @wait(i32* %7)
br label %72
72: ; preds = %70
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%74 = load i8*, i8** %11
%75 = call i32 @free(i8* %74)
%76 = load i8*, i8** %12
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %9
%79 = call i32 @free(i8* %78)
%80 = load i8*, i8** %8
%81 = call i32 @free(i8* %80)
%82 = call i32 @unsetenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %83
83: ; preds = %72, %67, %50, %42, %32, %16
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@.str = [5 x i8] c"bash\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error reading dir paths\0A\00"
@.str.2 = [22 x i8] c"Source: %s, Dest: %s\0A\00"
@.str.3 = [37 x i8] c"%s/libpurelibc.so:%s/libdirewrite.so\00"
@.str.4 = [11 x i8] c"LD_PRELOAD\00"
@.str.5 = [19 x i8] c"setenv failed: %s\0A\00"
@errno = global i32 0
@.str.6 = [12 x i8] c"LDRW_SOURCE\00"
@.str.7 = [10 x i8] c"LDRW_DEST\00"
@str = [14 x i8] c"All over now.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [2 x i8*]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = bitcast [2 x i8*]* %5 to i8*
%9 = icmp eq i32 %0, 3
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 (...) @print_usage()
br label %59
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i8* @realpath(i8* %14, i32* null)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i8* @realpath(i8* %17, i32* null)
%19 = icmp ne i8* %15, null
%20 = icmp ne i8* %18, null
%21 = and i1 %19, %20
br i1 %21, label %25, label %22
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %59
25: ; preds = %12
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %18)
%27 = tail call i8* (...) @get_current_dir_name()
%28 = icmp eq i8* %27, null
br i1 %28, label %59, label %29
29: ; preds = %25
%30 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %27, i8* %27)
%31 = load i8*, i8** %4
%32 = call i64 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %31, i32 1)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = call i32 @free(i8* %27)
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
br label %59
42: ; preds = %29
%43 = call i64 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %15, i32 1)
%44 = call i64 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %18, i32 1)
%45 = call i32 (...) @fork()
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%49 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %48)
br label %59
50: ; preds = %42
%51 = call i32 @wait(i32* %3)
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%53 = call i32 @free(i8* %15)
%54 = call i32 @free(i8* %18)
%55 = call i32 @free(i8* %27)
%56 = load i8*, i8** %4
%57 = call i32 @free(i8* %56)
%58 = call i32 @unsetenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %25, %50, %47, %34, %22, %10
%60 = phi i32 [ 0, %10 ], [ 1, %34 ], [ 0, %50 ], [ 0, %47 ], [ 1, %22 ], [ 1, %25 ]
ret i32 %60
}