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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  unsigned char src;
  char *Buf, *a;
  int BufLen;
  int nbytes, fileLen;
  unsigned int checksum = 0;
  int i;
  if (argc != 4) {
    printf("Usage: add_file <source file> <destination file> <size-K> \n");
    return -1;
  }
  BufLen = atoi(argv[3]) * 1024;
  Buf = (char *)malloc(BufLen);
  memset(Buf, 0x00, BufLen);
  fp = fopen(argv[1], "rb");
  if (fp == NULL) {
    printf("source file open error\n");
    free(Buf);
    return -1;
  }
  fseek(fp, 0L, SEEK_END);
  fileLen = ftell(fp);
  fseek(fp, 0L, SEEK_SET);
  nbytes = fread(Buf, 1, fileLen, fp);
  if (nbytes != fileLen) {
    printf("source file read error\n");
    free(Buf);
    fclose(fp);
    return -1;
  }
  fclose(fp);
  fp = fopen(argv[2], "wb");
  if (fp == NULL) {
    printf("destination file open error\n");
    free(Buf);
    return -1;
  }
  a = Buf;
  nbytes = fwrite(a, 1, BufLen, fp);
  if (nbytes != BufLen) {
    printf("destination file write error\n");
    free(Buf);
    fclose(fp);
    return -1;
  }
  free(Buf);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11ef <main+0x3f>
lea    0xe22(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10c0 <atoi@plt>
shl    $0xa,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe11(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1260 <main+0xb0>
lea    0xdf9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x18(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x3c(%rbp),%eax
je     12e6 <main+0x136>
lea    0xd94(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd71(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1333 <main+0x183>
lea    0xd59(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
je     1389 <main+0x1d9>
lea    0xd29(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x4,%edi
jne    1297 <main+0xe7>
mov    %rsi,%r15
mov    0x18(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
shl    $0xa,%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <memset@plt>
mov    0x8(%r15),%rdi
lea    0xe0d(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a5 <main+0xf5>
mov    %rax,%rbx
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%r12d
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %r12d,%eax
jne    12ae <main+0xfe>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x10(%r15),%rdi
lea    0xda3(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b7 <main+0x107>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
cmp    %ebp,%eax
jne    12cd <main+0x11d>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    12ee <main+0x13e>
lea    0xdcf(%rip),%rdi
call   1040 <puts@plt>
jmp    12e9 <main+0x139>
lea    0xdaa(%rip),%rdi
jmp    12be <main+0x10e>
lea    0xd8a(%rip),%rdi
jmp    12d4 <main+0x124>
lea    0xd65(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12e9 <main+0x139>
lea    0xd32(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [60 x i8] c"Usage: add_file <source file> <destination file> <size-K> \0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"source file open error\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [24 x i8] c"source file read error\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [29 x i8] c"destination file open error\0A\00"
@.str.6 = [30 x i8] c"destination file write error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %13
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %96
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 3
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = mul nsw i32 %23, 1024
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = load i32, i32* %10
%30 = call i32 @memset(i8* %28, i32 0, i32 %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %41
37: ; preds = %19
%38 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%39 = load i8*, i8** %8
%40 = call i32 @free(i8* %39)
store i32 -1, i32* %3
br label %96
41: ; preds = %19
%42 = load i32*, i32** %6
%43 = load i32, i32* @SEEK_END
%44 = call i32 @fseek(i32* %42, i64 0, i32 %43)
%45 = load i32*, i32** %6
%46 = call i32 @ftell(i32* %45)
store i32 %46, i32* %12
%47 = load i32*, i32** %6
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @fseek(i32* %47, i64 0, i32 %48)
%50 = load i8*, i8** %8
%51 = load i32, i32* %12
%52 = load i32*, i32** %6
%53 = call i32 @fread(i8* %50, i32 1, i32 %51, i32* %52)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = load i32, i32* %12
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %63
57: ; preds = %41
%58 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%59 = load i8*, i8** %8
%60 = call i32 @free(i8* %59)
%61 = load i32*, i32** %6
%62 = call i32 @fclose(i32* %61)
store i32 -1, i32* %3
br label %96
63: ; preds = %41
%64 = load i32*, i32** %6
%65 = call i32 @fclose(i32* %64)
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
%69 = call i32* @fopen(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %69, i32** %6
%70 = load i32*, i32** %6
%71 = icmp eq i32* %70, null
br i1 %71, label %72, label %76
72: ; preds = %63
%73 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%74 = load i8*, i8** %8
%75 = call i32 @free(i8* %74)
store i32 -1, i32* %3
br label %96
76: ; preds = %63
%77 = load i8*, i8** %8
store i8* %77, i8** %9
%78 = load i8*, i8** %9
%79 = load i32, i32* %10
%80 = load i32*, i32** %6
%81 = call i32 @fwrite(i8* %78, i32 1, i32 %79, i32* %80)
store i32 %81, i32* %11
%82 = load i32, i32* %11
%83 = load i32, i32* %10
%84 = icmp ne i32 %82, %83
br i1 %84, label %85, label %91
85: ; preds = %76
%86 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%87 = load i8*, i8** %8
%88 = call i32 @free(i8* %87)
%89 = load i32*, i32** %6
%90 = call i32 @fclose(i32* %89)
store i32 -1, i32* %3
br label %96
91: ; preds = %76
%92 = load i8*, i8** %8
%93 = call i32 @free(i8* %92)
%94 = load i32*, i32** %6
%95 = call i32 @fclose(i32* %94)
store i32 0, i32* %3
br label %96
96: ; preds = %91, %85, %72, %57, %37, %17
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.4 = [3 x i8] c"wb\00"
@str = [29 x i8] c"destination file write error\00"
@str.7 = [28 x i8] c"destination file open error\00"
@str.8 = [23 x i8] c"source file read error\00"
@str.9 = [23 x i8] c"source file open error\00"
@str.10 = [59 x i8] c"Usage: add_file <source file> <destination file> <size-K> \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.10, i64 0, i64 0))
br label %52
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 3
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = shl nsw i32 %9, 10
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @memset(i8* %12, i32 0, i32 %10)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %6
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%20 = tail call i32 @free(i8* %12)
br label %52
21: ; preds = %6
%22 = load i32, i32* @SEEK_END
%23 = tail call i32 @fseek(i32* %16, i64 0, i32 %22)
%24 = tail call i32 @ftell(i32* %16)
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @fseek(i32* %16, i64 0, i32 %25)
%27 = tail call i32 @fread(i8* %12, i32 1, i32 %24, i32* %16)
%28 = icmp eq i32 %27, %24
br i1 %28, label %33, label %29
29: ; preds = %21
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%31 = tail call i32 @free(i8* %12)
%32 = tail call i32 @fclose(i32* %16)
br label %52
33: ; preds = %21
%34 = tail call i32 @fclose(i32* %16)
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32* @fopen(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%41 = tail call i32 @free(i8* %12)
br label %52
42: ; preds = %33
%43 = tail call i32 @fwrite(i8* %12, i32 1, i32 %10, i32* %37)
%44 = icmp eq i32 %43, %10
br i1 %44, label %49, label %45
45: ; preds = %42
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%47 = tail call i32 @free(i8* %12)
%48 = tail call i32 @fclose(i32* %37)
br label %52
49: ; preds = %42
%50 = tail call i32 @free(i8* %12)
%51 = tail call i32 @fclose(i32* %37)
br label %52
52: ; preds = %49, %45, %39, %29, %18, %4
%53 = phi i32 [ -1, %4 ], [ -1, %18 ], [ -1, %29 ], [ -1, %39 ], [ -1, %45 ], [ 0, %49 ]
ret i32 %53
}