#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int open(char const *, int, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sync();
scalar_t__ write(int, char *, scalar_t__);
void copy(const char *from, const char *to) {
  int fd1, fd2;
  char buf[0x1000];
  ssize_t r = 0;
  if ((fd1 = open(from, O_RDONLY)) < 0)
    die("[-] open");
  if ((fd2 = open(to, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
    die("[-] open");
  for (;;) {
    r = read(fd1, buf, sizeof(buf));
    if (r < 0)
      die("[-] read");
    if (r == 0)
      break;
    if (write(fd2, buf, r) != r)
      die("[-] write");
  }
  close(fd1);
  close(fd2);
  sync();
  sync();
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x1028(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11aa <copy+0x4a>
lea    0xe5b(%rip),%rdi
call   1080 <die@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ed <copy+0x8d>
lea    0xe18(%rip),%rdi
call   1080 <die@plt>
jmp    11f2 <copy+0x92>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jge    1227 <copy+0xc7>
lea    0xde7(%rip),%rdi
call   1080 <die@plt>
cmpq   $0x0,-0x1028(%rbp)
jne    123a <copy+0xda>
jmp    126e <copy+0x10e>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x1028(%rbp),%rax
je     1269 <copy+0x109>
lea    0xdae(%rip),%rdi
call   1080 <die@plt>
jmp    11f2 <copy+0x92>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1060 <sync@plt>
mov    $0x0,%al
call   1060 <sync@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbp
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1197 <copy+0x37>
lea    0xe6e(%rip),%rdi
call   1080 <die@plt>
mov    0x2e3a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r13d
lea    0xe35(%rip),%r14
test   %eax,%eax
jns    11d7 <copy+0x77>
mov    %r14,%rdi
call   1080 <die@plt>
mov    %rsp,%rbx
lea    0xe28(%rip),%r12
lea    0xe2a(%rip),%r14
jmp    120b <copy+0xab>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1080 <die@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
jne    11cf <copy+0x6f>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
js     11f0 <copy+0x90>
jne    11f8 <copy+0x98>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r13d,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1060 <sync@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sync@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"[-] open\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [9 x i8] c"[-] read\00"
@.str.2 = [10 x i8] c"[-] write\00"
define void @copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 0, i64* %8
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = call i32 (i8*, i32, ...) @open(i8* %16, i32 %21, i32 384)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %15
br label %27
27: ; preds = %48, %26
%28 = load i32, i32* %5
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%30 = call i64 @read(i32 %28, i8* %29, i32 4096)
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = load i64, i64* %8
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
br label %49
39: ; preds = %35
%40 = load i32, i32* %6
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%42 = load i64, i64* %8
%43 = call i64 @write(i32 %40, i8* %41, i64 %42)
%44 = load i64, i64* %8
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %39
br label %27
49: ; preds = %38
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = call i32 (...) @sync()
%55 = call i32 (...) @sync()
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"[-] open\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [9 x i8] c"[-] read\00"
@.str.2 = [10 x i8] c"[-] write\00"
define void @copy(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %15, i32 384)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %29, %10
%19 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %29 ]
%20 = tail call i32 @die(i8* %19)
br label %21
21: ; preds = %10, %18
br label %22
22: ; preds = %21, %29
%23 = call i64 @read(i32 %6, i8* %4, i32 4096)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %29
27: ; preds = %22
%28 = icmp eq i64 %23, 0
br i1 %28, label %32, label %29
29: ; preds = %25, %27
%30 = call i64 @write(i32 %16, i8* %4, i64 %23)
%31 = icmp eq i64 %30, %23
br i1 %31, label %22, label %18
32: ; preds = %27
%33 = tail call i32 @close(i32 %6)
%34 = tail call i32 @close(i32 %16)
%35 = tail call i32 (...) @sync()
%36 = tail call i32 (...) @sync()
ret void
}