#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *src, *dst;
  size_t in, out;
  char buf[65536];
  int bufsize;
  if (argc != 4) {
    fprintf(stderr, "Usage: %s <buffer size> <src> <dest>\n", argv[0]);
    return (1);
  }
  bufsize = atoi(argv[1]);
  if (bufsize > 65536) {
    fprintf(stderr, "Error: %s: max. buffer size is %d\n", argv[0], 65536);
    return (1);
  }
  src = fopen(argv[2], "r");
  if (NULL == src)
    return (2);
  dst = fopen(argv[3], "w");
  if (dst < 0)
    return (3);
  while (1) {
    in = fread(buf, 1, bufsize, src);
    if (0 == in)
      break;
    out = fwrite(buf, 1, in, dst);
    if (0 == out)
      break;
  }
  fclose(src);
  fclose(dst);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11ad <main+0x4d>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12df <main+0x17f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x10034(%rbp)
cmpl   $0x10000,-0x10034(%rbp)
jle    11ff <main+0x9f>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3f(%rip),%rsi
mov    $0x10000,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12df <main+0x17f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    122f <main+0xcf>
movl   $0x2,-0x4(%rbp)
jmp    12df <main+0x17f>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe0d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    125e <main+0xfe>
movl   $0x3,-0x4(%rbp)
jmp    12df <main+0x17f>
jmp    1263 <main+0x103>
lea    -0x10030(%rbp),%rdi
mov    -0x10034(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1293 <main+0x133>
jmp    12c6 <main+0x166>
lea    -0x10030(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    12c1 <main+0x161>
jmp    12c6 <main+0x166>
jmp    1263 <main+0x103>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x10008,%rsp
mov    %rsi,%r14
cmp    $0x4,%edi
jne    11ae <main+0x4e>
mov    0x8(%r14),%rdi
call   1070 <atoi@plt>
cmp    $0x10001,%eax
jl     11cf <main+0x6f>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe8e(%rip),%rsi
mov    $0x10000,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1249 <main+0xe9>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe3f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1249 <main+0xe9>
mov    %eax,%r15d
mov    0x10(%r14),%rdi
lea    0xe6c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1244 <main+0xe4>
mov    %rax,%rbx
mov    0x18(%r14),%rdi
lea    0xe56(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1230 <main+0xd0>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1080 <fwrite@plt>
test   %rax,%rax
jne    1200 <main+0xa0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1249 <main+0xe9>
mov    $0x2,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <buffer size> <src> <dest>\0A\00"
@.str.1 = [35 x i8] c"Error: %s: max. buffer size is %d\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\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 i64
%9 = alloca i64
%10 = alloca [65536 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %72
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp sgt i32 %25, 65536
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 0
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 65536)
store i32 1, i32* %3
br label %72
33: ; preds = %20
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %6
%38 = load i32*, i32** %6
%39 = icmp eq i32* null, %38
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 2, i32* %3
br label %72
41: ; preds = %33
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 3
%44 = load i8*, i8** %43
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %45, i32** %7
%46 = load i32*, i32** %7
%47 = icmp ult i32* %46, null
br i1 %47, label %48, label %49
48: ; preds = %41
store i32 3, i32* %3
br label %72
49: ; preds = %41
br label %50
50: ; preds = %49, %66
%51 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%52 = load i32, i32* %11
%53 = load i32*, i32** %6
%54 = call i64 @fread(i8* %51, i32 1, i32 %52, i32* %53)
store i64 %54, i64* %8
%55 = load i64, i64* %8
%56 = icmp eq i64 0, %55
br i1 %56, label %57, label %58
57: ; preds = %50
br label %67
58: ; preds = %50
%59 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%60 = load i64, i64* %8
%61 = load i32*, i32** %7
%62 = call i64 @fwrite(i8* %59, i32 1, i64 %60, i32* %61)
store i64 %62, i64* %9
%63 = load i64, i64* %9
%64 = icmp eq i64 0, %63
br i1 %64, label %65, label %66
65: ; preds = %58
br label %67
66: ; preds = %58
br label %50
67: ; preds = %65, %57
%68 = load i32*, i32** %6
%69 = call i32 @fclose(i32* %68)
%70 = load i32*, i32** %7
%71 = call i32 @fclose(i32* %70)
store i32 0, i32* %3
br label %72
72: ; preds = %67, %48, %40, %27, %14
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <buffer size> <src> <dest>\0A\00"
@.str.1 = [35 x i8] c"Error: %s: max. buffer size is %d\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 4
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %37
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = icmp sgt i32 %13, 65536
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 65536)
br label %37
19: ; preds = %10
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %37, label %24
24: ; preds = %19
%25 = getelementptr inbounds i8*, i8** %1, i64 3
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %24, %31
%29 = call i64 @fread(i8* %4, i32 1, i32 %13, i32* %22)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call i64 @fwrite(i8* %4, i32 1, i64 %29, i32* %27)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %28
34: ; preds = %31, %28
%35 = tail call i32 @fclose(i32* %22)
%36 = tail call i32 @fclose(i32* %27)
br label %37
37: ; preds = %19, %34, %15, %6
%38 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 0, %34 ], [ 2, %19 ]
ret i32 %38
}