#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_CLOEXEC;
int O_CREAT;
int O_WRONLY;
int check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copy_fd(int, int);
int /*<<< orphan*/ open(char const *const, int, int);
void copy_fd_to_file(int in, const char *const out) {
  int out_fd = check(open(out, O_WRONLY | O_CREAT | O_CLOEXEC, 0700),
                     "open(copy_fd_file)");
  copy_fd(in, out_fd);
  check(close(out_fd), "close(copy_fd_file)");
}


x86 assembly (clang, -O0):
<copy_fd_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e95(%rip),%rax
or     (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
mov    $0x1c0,%edx
call   1040 <open@plt>
mov    %eax,%edi
lea    0xe7f(%rip),%rsi
call   1050 <check@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <copy_fd@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    %eax,%edi
lea    0xe6e(%rip),%rsi
call   1050 <check@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd_to_file>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e86(%rip),%rcx
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edi,%ebx
mov    0x2e6a(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
mov    $0x1c0,%edx
call   1040 <open@plt>
lea    0xe8a(%rip),%rsi
mov    %eax,%edi
call   1050 <check@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <copy_fd@plt>
mov    %ebp,%edi
call   1030 <close@plt>
lea    0xe7d(%rip),%rsi
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <check@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_CLOEXEC = global i32 0
@.str = [19 x i8] c"open(copy_fd_file)\00"
@.str.1 = [20 x i8] c"close(copy_fd_file)\00"
define void @copy_fd_to_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_CLOEXEC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11, i32 448)
%13 = call i32 @check(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 %13, i32* %5
%14 = load i32, i32* %3
%15 = load i32, i32* %5
%16 = call i32 @copy_fd(i32 %14, i32 %15)
%17 = load i32, i32* %5
%18 = call i32 @close(i32 %17)
%19 = call i32 @check(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_CLOEXEC = global i32 0
@.str = [19 x i8] c"open(copy_fd_file)\00"
@.str.1 = [20 x i8] c"close(copy_fd_file)\00"
define void @copy_fd_to_file(i32 %0, i8* nocapture %1) {
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_CLOEXEC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %1, i32 %7, i32 448)
%9 = tail call i32 @check(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @copy_fd(i32 %0, i32 %9)
%11 = tail call i32 @close(i32 %9)
%12 = tail call i32 @check(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
ret void
}