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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ logMessage(int, char *, char *, int /*<<< orphan*/);
int open(char *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write(int, char *, int);
int copyFileFd(int infd, char *dest) {
  int outfd;
  char buf[4096];
  int i;
  int rc = 0;
  outfd = open(dest, O_CREAT | O_RDWR, 0666);
  if (outfd < 0) {
    logMessage(40, "failed to open %s: %s", dest, strerror(errno));
    return 1;
  }
  while ((i = read(infd, buf, sizeof(buf))) > 0) {
    if (write(outfd, buf, i) != i) {
      rc = 1;
      break;
    }
  }
  close(outfd);
  return rc;
}


x86 assembly (clang, -O0):
<copyFileFd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1028(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e8 <copyFileFd+0x88>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1030(%rbp),%rdx
mov    %eax,%ecx
mov    $0x28,%edi
lea    0xe29(%rip),%rsi
call   1030 <logMessage@plt>
movl   $0x1,-0x4(%rbp)
jmp    1256 <copyFileFd+0xf6>
jmp    11ed <copyFileFd+0x8d>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jle    1245 <copyFileFd+0xe5>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1040 <write@plt>
cmp    -0x1024(%rbp),%eax
je     1240 <copyFileFd+0xe0>
movl   $0x1,-0x1028(%rbp)
jmp    1245 <copyFileFd+0xe5>
jmp    11ed <copyFileFd+0x8d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1028(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyFileFd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e7c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11ce <copyFileFd+0x6e>
mov    %eax,%r12d
mov    %rsp,%rbx
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11f9 <copyFileFd+0x99>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11a0 <copyFileFd+0x40>
mov    $0x1,%ebx
jmp    11fb <copyFileFd+0x9b>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1d(%rip),%rsi
mov    $0x28,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1030 <logMessage@plt>
mov    $0x1,%ebx
jmp    1203 <copyFileFd+0xa3>
xor    %ebx,%ebx
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"failed to open %s: %s\00"
@errno = global i32 0
define i32 @copyFileFd(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13, i32 438)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @logMessage(i32 40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %20)
store i32 1, i32* %3
br label %41
22: ; preds = %2
br label %23
23: ; preds = %36, %22
%24 = load i32, i32* %4
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 4096)
store i32 %26, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = load i32, i32* %6
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = load i32, i32* %8
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* %8
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 1, i32* %9
br label %37
36: ; preds = %28
br label %23
37: ; preds = %35, %23
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %9
store i32 %40, i32* %3
br label %41
41: ; preds = %37, %17
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"failed to open %s: %s\00"
@errno = global i32 0
define i32 @copyFileFd(i32 %0, i8* %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_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %1, i32 %7, i32 438)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @logMessage(i32 40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %12)
br label %23
14: ; preds = %2, %17
%15 = call i32 @read(i32 %0, i8* %4, i32 4096)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @write(i32 %8, i8* %4, i32 %15)
%19 = icmp eq i32 %18, %15
br i1 %19, label %14, label %20
20: ; preds = %17, %14
%21 = phi i32 [ 0, %14 ], [ 1, %17 ]
%22 = tail call i32 @close(i32 %8)
br label %23
23: ; preds = %20, %10
%24 = phi i32 [ 1, %10 ], [ %21, %20 ]
ret i32 %24
}