#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ checkForOpenErrors(int);
int /*<<< orphan*/ closeFile(int);
int /*<<< orphan*/ errExit(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Usage: %s file1 file2\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  int fd1 = open(argv[1], O_RDONLY);
  checkForOpenErrors(fd1);
  int fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
  checkForOpenErrors(fd2);
  char buf[8];
  ssize_t numRead = read(fd1, buf, 8);
  while (numRead != -1) {
    if (numRead == 0) {
      break;
    } else {
      if (write(fd2, buf, numRead) < numRead)
        errExit("Not enough written");
    }
    numRead = read(fd1, buf, 8);
  }
  if (numRead == -1)
    errExit("numRead");
  closeFile(fd1);
  closeFile(fd2);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <checkForOpenErrors@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
or     (%rax),%edx
mov    0x2d98(%rip),%rax
or     (%rax),%edx
mov    0x2da7(%rip),%rax
or     (%rax),%edx
mov    0x2d7e(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <checkForOpenErrors@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     12b5 <main+0x135>
cmpl   $0x0,-0x24(%rbp)
jne    1273 <main+0xf3>
jmp    12b5 <main+0x135>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
jge    1297 <main+0x117>
lea    0xd85(%rip),%rdi
call   1040 <errExit@plt>
jmp    129c <main+0x11c>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
jmp    125a <main+0xda>
cmpl   $0xffffffff,-0x24(%rbp)
jne    12cb <main+0x14b>
lea    0xd64(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x14(%rbp),%edi
call   1080 <closeFile@plt>
mov    -0x18(%rbp),%edi
call   1080 <closeFile@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    125e <main+0xde>
mov    0x8(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   1070 <checkForOpenErrors@plt>
mov    0x2dcb(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2dc8(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dce(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
mov    0x10(%rbp),%rdi
mov    0x2db0(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   1070 <checkForOpenErrors@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,%ebp
add    $0x1,%eax
cmp    $0x2,%eax
jae    127c <main+0xfc>
cmp    $0xffffffff,%ebp
jne    1240 <main+0xc0>
lea    0xdef(%rip),%rdi
call   1040 <errExit@plt>
mov    %r14d,%edi
call   1080 <closeFile@plt>
mov    %r12d,%edi
call   1080 <closeFile@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x0(%rbp),%rsi
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0x8(%rsp),%rbx
lea    0xd8f(%rip),%r15
jmp    12aa <main+0x12a>
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,%ebp
add    $0x1,%eax
cmp    $0x1,%eax
jbe    122f <main+0xaf>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jge    1290 <main+0x110>
mov    %r15,%rdi
call   1040 <errExit@plt>
jmp    1290 <main+0x110>

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: %s file1 file2\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [19 x i8] c"Not enough written\00"
@.str.2 = [8 x i8] c"numRead\00"
@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 [8 x i8]
%9 = alloca i32
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 %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = call i32 @checkForOpenErrors(i32 %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IRGRP
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IWGRP
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IROTH
%43 = or i32 %41, %42
%44 = call i32 (i8*, i32, ...) @open(i8* %29, i32 %34, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = call i32 @checkForOpenErrors(i32 %45)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 8)
store i32 %49, i32* %9
br label %50
50: ; preds = %67, %19
%51 = load i32, i32* %9
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %71
53: ; preds = %50
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %71
57: ; preds = %53
%58 = load i32, i32* %7
%59 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%60 = load i32, i32* %9
%61 = call i32 @write(i32 %58, i8* %59, i32 %60)
%62 = load i32, i32* %9
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %57
%65 = call i32 @errExit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %66
66: ; preds = %64, %57
br label %67
67: ; preds = %66
%68 = load i32, i32* %6
%69 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 8)
store i32 %70, i32* %9
br label %50
71: ; preds = %56, %50
%72 = load i32, i32* %9
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 @errExit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %76
76: ; preds = %74, %71
%77 = load i32, i32* %6
%78 = call i32 @closeFile(i32 %77)
%79 = load i32, i32* %7
%80 = call i32 @closeFile(i32 %79)
%81 = load i32, i32* @EXIT_SUCCESS
%82 = call i32 @exit(i32 %81)
unreachable
}

LLVM IR (-O2):
@.str = [23 x i8] c"Usage: %s file1 file2\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [19 x i8] c"Not enough written\00"
@.str.2 = [8 x i8] c"numRead\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i8]
%4 = icmp eq i32 %0, 3
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = tail call i32 @checkForOpenErrors(i32 %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %24, %23
%26 = load i32, i32* @S_IRGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IWGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = tail call i32 (i8*, i32, ...) @open(i8* %17, i32 %22, i32 %31)
%33 = tail call i32 @checkForOpenErrors(i32 %32)
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%35 = call i32 @read(i32 %14, i8* %34, i32 8)
%36 = add i32 %35, 1
%37 = icmp ult i32 %36, 2
br i1 %37, label %48, label %38
38: ; preds = %10, %44
%39 = phi i32 [ %45, %44 ], [ %35, %10 ]
%40 = call i32 @write(i32 %32, i8* %34, i32 %39)
%41 = icmp slt i32 %40, %39
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = tail call i32 @errExit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %38, %42
%45 = call i32 @read(i32 %14, i8* %34, i32 8)
%46 = add i32 %45, 1
%47 = icmp ult i32 %46, 2
br i1 %47, label %48, label %38
48: ; preds = %44, %10
%49 = phi i32 [ %35, %10 ], [ %45, %44 ]
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = tail call i32 @errExit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = tail call i32 @closeFile(i32 %14)
%55 = tail call i32 @closeFile(i32 %32)
%56 = load i32, i32* @EXIT_SUCCESS
%57 = tail call i32 @exit(i32 %56)
unreachable
}