#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(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  unsigned char *Buf;
  int BufLen;
  int nbytes, fileLen, toBeReadLen;
  int i;
  if (argc != 4) {
    printf("Usage: size_adjust <source file> <destination file> <size> \n");
    return -1;
  }
  BufLen = atoi(argv[3]);
  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);
  memset(Buf, 0x00, BufLen);
  if (BufLen <= fileLen)
    toBeReadLen = BufLen;
  else
    toBeReadLen = fileLen;
  nbytes = fread(Buf, 1, toBeReadLen, fp);
  if (nbytes != toBeReadLen) {
    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;
  }
  nbytes = fwrite(Buf, 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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11e8 <main+0x38>
lea    0xe29(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main+0x20b>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1256 <main+0xa6>
lea    0xe04(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main+0x20b>
mov    -0x18(%rbp),%rdi
mov    0x2d87(%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,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jg     12b3 <main+0x103>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    12b9 <main+0x109>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     1307 <main+0x157>
lea    0xd74(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main+0x20b>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1354 <main+0x1a4>
lea    0xd39(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main+0x20b>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     13a2 <main+0x1f2>
lea    0xd11(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main+0x20b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x4,%edi
jne    12a9 <main+0xf9>
mov    %rsi,%r15
mov    0x18(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1070 <memset@plt>
mov    0x8(%r15),%rdi
lea    0xe0e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b7 <main+0x107>
mov    %rax,%rbx
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1070 <memset@plt>
cmp    %ebp,%r12d
cmovle %r12d,%ebp
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %ebp,%eax
jne    12c0 <main+0x110>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x10(%r15),%rdi
lea    0xd93(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12c9 <main+0x119>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
cmp    %r12d,%eax
jne    12df <main+0x12f>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    1300 <main+0x150>
lea    0xdbd(%rip),%rdi
call   1040 <puts@plt>
jmp    12fb <main+0x14b>
lea    0xd98(%rip),%rdi
jmp    12d0 <main+0x120>
lea    0xd78(%rip),%rdi
jmp    12e6 <main+0x136>
lea    0xd53(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12fb <main+0x14b>
lea    0xd20(%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 = [61 x i8] c"Usage: size_adjust <source file> <destination file> <size> \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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %103
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 3
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = call i32 @memset(i8* %25, i32 0, i32 %26)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %6
%32 = load i32*, i32** %6
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %38
34: ; preds = %17
%35 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%36 = load i8*, i8** %7
%37 = call i32 @free(i8* %36)
store i32 -1, i32* %3
br label %103
38: ; preds = %17
%39 = load i32*, i32** %6
%40 = load i32, i32* @SEEK_END
%41 = call i32 @fseek(i32* %39, i64 0, i32 %40)
%42 = load i32*, i32** %6
%43 = call i32 @ftell(i32* %42)
store i32 %43, i32* %10
%44 = load i32*, i32** %6
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %44, i64 0, i32 %45)
%47 = load i8*, i8** %7
%48 = load i32, i32* %8
%49 = call i32 @memset(i8* %47, i32 0, i32 %48)
%50 = load i32, i32* %8
%51 = load i32, i32* %10
%52 = icmp sle i32 %50, %51
br i1 %52, label %53, label %55
53: ; preds = %38
%54 = load i32, i32* %8
store i32 %54, i32* %11
br label %57
55: ; preds = %38
%56 = load i32, i32* %10
store i32 %56, i32* %11
br label %57
57: ; preds = %55, %53
%58 = load i8*, i8** %7
%59 = load i32, i32* %11
%60 = load i32*, i32** %6
%61 = call i32 @fread(i8* %58, i32 1, i32 %59, i32* %60)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = load i32, i32* %11
%64 = icmp ne i32 %62, %63
br i1 %64, label %65, label %71
65: ; preds = %57
%66 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%67 = load i8*, i8** %7
%68 = call i32 @free(i8* %67)
%69 = load i32*, i32** %6
%70 = call i32 @fclose(i32* %69)
store i32 -1, i32* %3
br label %103
71: ; preds = %57
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 2
%76 = load i8*, i8** %75
%77 = call i32* @fopen(i8* %76, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %77, i32** %6
%78 = load i32*, i32** %6
%79 = icmp eq i32* %78, null
br i1 %79, label %80, label %84
80: ; preds = %71
%81 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%82 = load i8*, i8** %7
%83 = call i32 @free(i8* %82)
store i32 -1, i32* %3
br label %103
84: ; preds = %71
%85 = load i8*, i8** %7
%86 = load i32, i32* %8
%87 = load i32*, i32** %6
%88 = call i32 @fwrite(i8* %85, i32 1, i32 %86, i32* %87)
store i32 %88, i32* %9
%89 = load i32, i32* %9
%90 = load i32, i32* %8
%91 = icmp ne i32 %89, %90
br i1 %91, label %92, label %98
92: ; preds = %84
%93 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%94 = load i8*, i8** %7
%95 = call i32 @free(i8* %94)
%96 = load i32*, i32** %6
%97 = call i32 @fclose(i32* %96)
store i32 -1, i32* %3
br label %103
98: ; preds = %84
%99 = load i8*, i8** %7
%100 = call i32 @free(i8* %99)
%101 = load i32*, i32** %6
%102 = call i32 @fclose(i32* %101)
store i32 0, i32* %3
br label %103
103: ; preds = %98, %92, %80, %65, %34, %15
%104 = load i32, i32* %3
ret i32 %104
}

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 = [60 x i8] c"Usage: size_adjust <source file> <destination file> <size> \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 ([60 x i8], [60 x i8]* @str.10, i64 0, i64 0))
br label %54
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 3
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @memset(i8* %11, i32 0, i32 %9)
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %6
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%19 = tail call i32 @free(i8* %11)
br label %54
20: ; preds = %6
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @fseek(i32* %15, i64 0, i32 %21)
%23 = tail call i32 @ftell(i32* %15)
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %15, i64 0, i32 %24)
%26 = tail call i32 @memset(i8* %11, i32 0, i32 %9)
%27 = icmp sgt i32 %9, %23
%28 = select i1 %27, i32 %23, i32 %9
%29 = tail call i32 @fread(i8* %11, i32 1, i32 %28, i32* %15)
%30 = icmp eq i32 %29, %28
br i1 %30, label %35, label %31
31: ; preds = %20
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%33 = tail call i32 @free(i8* %11)
%34 = tail call i32 @fclose(i32* %15)
br label %54
35: ; preds = %20
%36 = tail call i32 @fclose(i32* %15)
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = tail call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%43 = tail call i32 @free(i8* %11)
br label %54
44: ; preds = %35
%45 = tail call i32 @fwrite(i8* %11, i32 1, i32 %9, i32* %39)
%46 = icmp eq i32 %45, %9
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%49 = tail call i32 @free(i8* %11)
%50 = tail call i32 @fclose(i32* %39)
br label %54
51: ; preds = %44
%52 = tail call i32 @free(i8* %11)
%53 = tail call i32 @fclose(i32* %39)
br label %54
54: ; preds = %51, %47, %41, %31, %17, %4
%55 = phi i32 [ -1, %4 ], [ -1, %17 ], [ -1, %31 ], [ -1, %41 ], [ -1, %47 ], [ 0, %51 ]
ret i32 %55
}