#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_FAILURE;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int in_fd, out_fd, n_chars;
  char buf[4096];
  if (argc != 3) {
    fprintf(stderr, "Usage: %s <src> <dest>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((in_fd = open(argv[1], O_RDONLY)) == -1) {
    fprintf(stderr, "Cannot open %s: %s\n", argv[1], strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((out_fd = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644)) == -1) {
    fprintf(stderr, "Cannot create %s: %s\n", argv[2], strerror(errno));
    exit(EXIT_FAILURE);
  }
  while ((n_chars = read(in_fd, buf, 4096)) > 0) {
    if (write(out_fd, buf, n_chars) != n_chars) {
      perror("write() failed");
      exit(EXIT_FAILURE);
    }
  }
  if (n_chars == -1) {
    perror("read() failed");
    exit(EXIT_FAILURE);
  }
  if (close(in_fd) == -1 || close(out_fd) == -1)
    perror("close() failed");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-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    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124b <main+0xcb>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x102c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x1028(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x102c(%rbp),%edi
mov    -0x1028(%rbp),%rdx
mov    %rax,%rcx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d45(%rip),%rax
or     (%rax),%esi
mov    0x2d84(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12de <main+0x15e>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x1038(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rdx
mov    %rax,%rcx
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12e3 <main+0x163>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    133d <main+0x1bd>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     1338 <main+0x1b8>
lea    0xd1d(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12e3 <main+0x163>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1361 <main+0x1e1>
lea    0xd03(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1383 <main+0x203>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    138f <main+0x20f>
lea    0xcd5(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    1284 <main+0x104>
mov    0x8(%r14),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12a0 <main+0x120>
mov    %eax,%r15d
mov    0x2e36(%rip),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x144>
mov    %eax,%r12d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1241 <main+0xc1>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1200 <main+0x80>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmp    $0xffffffff,%ebp
je     12fa <main+0x17a>
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1276 <main+0xf6>
mov    %r12d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1276 <main+0xf6>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
jmp    1264 <main+0xe4>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd69(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1233 <main+0xb3>
mov    0x2d21(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r14),%rbp
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd56(%rip),%rsi
jmp    12e6 <main+0x166>
mov    0x2cfd(%rip),%rax
mov    (%rax),%ebx
mov    0x10(%r14),%rbp
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd46(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1233 <main+0xb3>
lea    0xd50(%rip),%rdi
jmp    122e <main+0xae>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <src> <dest>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [22 x i8] c"Cannot create %s: %s\0A\00"
@.str.3 = [15 x i8] c"write() failed\00"
@.str.4 = [14 x i8] c"read() failed\00"
@.str.5 = [15 x i8] c"close() failed\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 i32
%9 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %37
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %20
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_CREAT
%42 = load i32, i32* @O_WRONLY
%43 = or i32 %41, %42
%44 = load i32, i32* @O_TRUNC
%45 = or i32 %43, %44
%46 = call i32 (i8*, i32, ...) @open(i8* %40, i32 %45, i32 420)
store i32 %46, i32* %7
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %58
48: ; preds = %37
%49 = load i32, i32* @stderr
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %52, i8* %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %37
br label %59
59: ; preds = %75, %58
%60 = load i32, i32* %6
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%62 = call i32 @read(i32 %60, i8* %61, i32 4096)
store i32 %62, i32* %8
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %76
64: ; preds = %59
%65 = load i32, i32* %7
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%67 = load i32, i32* %8
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
%69 = load i32, i32* %8
%70 = icmp ne i32 %68, %69
br i1 %70, label %71, label %75
71: ; preds = %64
%72 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %64
br label %59
76: ; preds = %59
%77 = load i32, i32* %8
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %83
79: ; preds = %76
%80 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%81 = load i32, i32* @EXIT_FAILURE
%82 = call i32 @exit(i32 %81)
unreachable
83: ; preds = %76
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = icmp eq i32 %85, -1
br i1 %86, label %91, label %87
87: ; preds = %83
%88 = load i32, i32* %7
%89 = call i32 @close(i32 %88)
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %93
91: ; preds = %87, %83
%92 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %93
93: ; preds = %91, %87
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <src> <dest>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [22 x i8] c"Cannot create %s: %s\0A\00"
@.str.3 = [15 x i8] c"write() failed\00"
@.str.4 = [14 x i8] c"read() failed\00"
@.str.5 = [15 x i8] c"close() failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
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 ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%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 = load i32, i32* @O_RDONLY
%16 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %13
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %12
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %30, %29
%32 = load i32, i32* @O_TRUNC
%33 = or i32 %31, %32
%34 = tail call i32 (i8*, i32, ...) @open(i8* %28, i32 %33, i32 420)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %44
36: ; preds = %26
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %27
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %38, i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %26, %47
%45 = call i32 @read(i32 %16, i8* %4, i32 4096)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %44
%48 = call i32 @write(i32 %34, i8* %4, i32 %45)
%49 = icmp eq i32 %48, %45
br i1 %49, label %44, label %50
50: ; preds = %47
%51 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = tail call i32 @exit(i32 %52)
unreachable
54: ; preds = %44
%55 = icmp eq i32 %45, -1
br i1 %55, label %56, label %60
56: ; preds = %54
%57 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = tail call i32 @exit(i32 %58)
unreachable
60: ; preds = %54
%61 = tail call i32 @close(i32 %16)
%62 = icmp eq i32 %61, -1
br i1 %62, label %66, label %63
63: ; preds = %60
%64 = tail call i32 @close(i32 %34)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %63, %60
%67 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %68
68: ; preds = %66, %63
ret i32 0
}