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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ EX_CANTCREAT;
int /*<<< orphan*/ EX_NOINPUT;
int /*<<< orphan*/ EX_USAGE;
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int src, dst, in, out;
  char buf[4194304];
  int bufsize;
  if (argc != 4) {
    fprintf(stderr, "Usage: %s <buffer size> <src> <dest>\n", argv[0]);
    exit(EX_USAGE);
  }
  bufsize = atoi(argv[1]);
  if (bufsize > 4194304) {
    fprintf(stderr, "Error: %s: max. buffer size is %d\n", argv[0], 4194304);
    exit(EX_USAGE);
  }
  src = open(argv[2], O_RDONLY);
  if (src < 0)
    exit(EX_NOINPUT);
  dst = creat(argv[3], 0666);
  if (dst < 0)
    exit(EX_CANTCREAT);
  while (1) {
    in = read(src, buf, bufsize);
    if (in <= 0)
      break;
    out = write(dst, buf, in);
    if (out <= 0)
      break;
  }
  close(src);
  close(dst);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11cf <main+0x4f>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x400024(%rbp)
cmpl   $0x400000,-0x400024(%rbp)
jle    1223 <main+0xa3>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1d(%rip),%rsi
mov    $0x400000,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1254 <main+0xd4>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    $0x1b6,%esi
call   1090 <creat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1281 <main+0x101>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1286 <main+0x106>
mov    -0x14(%rbp),%edi
lea    -0x400020(%rbp),%rsi
mov    -0x400024(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     12ad <main+0x12d>
jmp    12d6 <main+0x156>
mov    -0x18(%rbp),%edi
lea    -0x400020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     12d1 <main+0x151>
jmp    12d6 <main+0x156>
jmp    1286 <main+0x106>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x400008,%rsp
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    11c6 <main+0x46>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
cmp    $0x400001,%eax
jl     11ee <main+0x6e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe6e(%rip),%rsi
mov    $0x400000,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11e0 <main+0x60>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe27(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    1215 <main+0x95>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %eax,%r15d
mov    0x18(%rbx),%rdi
mov    $0x1b6,%esi
call   1090 <creat@plt>
test   %eax,%eax
jns    1238 <main+0xb8>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %eax,%ebx
mov    %rsp,%rbp
nopl   (%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1262 <main+0xe2>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jg     1240 <main+0xc0>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <buffer size> <src> <dest>\0A\00"
@EX_USAGE = global i32 0
@.str.1 = [35 x i8] c"Error: %s: max. buffer size is %d\0A\00"
@O_RDONLY = global i32 0
@EX_NOINPUT = global i32 0
@EX_CANTCREAT = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4194304 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 %22
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)
%20 = load i32, i32* @EX_USAGE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp sgt i32 %27, 4194304
br i1 %28, label %29, label %37
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 4194304)
%35 = load i32, i32* @EX_USAGE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %22
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* @EX_NOINPUT
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %37
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 3
%51 = load i8*, i8** %50
%52 = call i32 @creat(i8* %51, i32 438)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = load i32, i32* @EX_CANTCREAT
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %48
br label %59
59: ; preds = %58, %75
%60 = load i32, i32* %6
%61 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %10, i64 0, i64 0
%62 = load i32, i32* %11
%63 = call i32 @read(i32 %60, i8* %61, i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp sle i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %59
br label %76
67: ; preds = %59
%68 = load i32, i32* %7
%69 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %10, i64 0, i64 0
%70 = load i32, i32* %8
%71 = call i32 @write(i32 %68, i8* %69, i32 %70)
store i32 %71, i32* %9
%72 = load i32, i32* %9
%73 = icmp sle i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %67
br label %76
75: ; preds = %67
br label %59
76: ; preds = %74, %66
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %7
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* @EXIT_SUCCESS
%82 = call i32 @exit(i32 %81)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <buffer size> <src> <dest>\0A\00"
@EX_USAGE = global i32 0
@.str.1 = [35 x i8] c"Error: %s: max. buffer size is %d\0A\00"
@O_RDONLY = global i32 0
@EX_NOINPUT = global i32 0
@EX_CANTCREAT = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4194304 x i8]
%4 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 4
br i1 %5, label %12, 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)
%10 = load i32, i32* @EX_USAGE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = icmp sgt i32 %15, 4194304
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %1
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 4194304)
%21 = load i32, i32* @EX_USAGE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %12
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = tail call i32 @open(i8* %25, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @EX_NOINPUT
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = getelementptr inbounds i8*, i8** %1, i64 3
%34 = load i8*, i8** %33
%35 = tail call i32 @creat(i8* %34, i32 438)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @EX_CANTCREAT
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32, %43
%41 = call i32 @read(i32 %27, i8* %4, i32 %15)
%42 = icmp slt i32 %41, 1
br i1 %42, label %46, label %43
43: ; preds = %40
%44 = call i32 @write(i32 %35, i8* %4, i32 %41)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %40
46: ; preds = %43, %40
%47 = tail call i32 @close(i32 %27)
%48 = tail call i32 @close(i32 %35)
%49 = load i32, i32* @EXIT_SUCCESS
%50 = tail call i32 @exit(i32 %49)
unreachable
}