#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IROTH;
int S_IRUSR;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int read(int, char *, int);
int write(int, char *, int);
int copy_to_file_from_fd(int fd_in, char *filename) {
  int data_size = 0;
  char buff[1024];
  int bread = 0, bwrite = 0;
  int fd_out;
  fd_out = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
                S_IROTH | S_IWOTH | S_IRUSR | S_IWUSR);
  if (fd_out <= 0)
    return 0;
  while ((bread = read(fd_in, buff, 1024)) > 0) {
    bwrite = write(fd_out, buff, bread);
    data_size += bwrite;
  }
  close(fd_out);
  return data_size;
}


x86 assembly (clang, -O0):
<copy_to_file_from_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x424(%rbp)
movl   $0x0,-0x428(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    0x2e0c(%rip),%rax
or     (%rax),%edx
mov    0x2e1b(%rip),%rax
or     (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jg     11d4 <copy_to_file_from_fd+0x94>
movl   $0x0,-0x4(%rbp)
jmp    123c <copy_to_file_from_fd+0xfc>
jmp    11d9 <copy_to_file_from_fd+0x99>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    122b <copy_to_file_from_fd+0xeb>
mov    -0x42c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11d9 <copy_to_file_from_fd+0x99>
mov    -0x42c(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_to_file_from_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    0x2e5c(%rip),%rcx
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e8a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e59(%rip),%rcx
mov    0x2e32(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e3f(%rip),%rcx
or     (%rcx),%edx
mov    %edi,%r14d
mov    0x2e4b(%rip),%rcx
or     (%rcx),%edx
mov    %rsi,%rdi
mov    %eax,%esi
call   1060 <open@plt>
test   %eax,%eax
jle    11ed <copy_to_file_from_fd+0xad>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11e3 <copy_to_file_from_fd+0xa3>
mov    %rsp,%rbp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    %eax,%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11c0 <copy_to_file_from_fd+0x80>
mov    %r15d,%edi
call   1040 <close@plt>
jmp    11ef <copy_to_file_from_fd+0xaf>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @copy_to_file_from_fd(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = load i32, i32* @S_IWOTH
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %11, i32 %16, i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 0, i32* %3
br label %46
28: ; preds = %2
br label %29
29: ; preds = %34, %28
%30 = load i32, i32* %4
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 1024)
store i32 %32, i32* %8
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = load i32, i32* %10
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %8
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = load i32, i32* %6
%41 = add nsw i32 %40, %39
store i32 %41, i32* %6
br label %29
42: ; preds = %29
%43 = load i32, i32* %10
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %6
store i32 %45, i32* %3
br label %46
46: ; preds = %42, %27
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @copy_to_file_from_fd(i32 %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = load i32, i32* @S_IWOTH
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %1, i32 %9, i32 %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %32, label %19
19: ; preds = %2
%20 = call i32 @read(i32 %0, i8* %4, i32 1024)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19, %22
%23 = phi i32 [ %27, %22 ], [ %20, %19 ]
%24 = phi i32 [ %26, %22 ], [ 0, %19 ]
%25 = call i32 @write(i32 %17, i8* %4, i32 %23)
%26 = add nsw i32 %25, %24
%27 = call i32 @read(i32 %0, i8* %4, i32 1024)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %22, label %29
29: ; preds = %22, %19
%30 = phi i32 [ 0, %19 ], [ %26, %22 ]
%31 = tail call i32 @close(i32 %17)
br label %32
32: ; preds = %2, %29
%33 = phi i32 [ %30, %29 ], [ 0, %2 ]
ret i32 %33
}