#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int copy_data(int /*<<< orphan*/, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write_file(const char *filename) {
  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd == -1) {
    fprintf(stderr,
            "Error: "
            "failed to create output file %s (%s)\n",
            filename, strerror(errno));
    return -1;
  }
  int rc = copy_data(STDIN_FILENO, fd);
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d6 <write_file+0x86>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <write_file+0xa8>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
call   1050 <copy_data@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%rbx
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     119f <write_file+0x4f>
mov    %eax,%ebp
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
call   1050 <copy_data@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
jmp    11cf <write_file+0x7f>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ebp
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe43(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Error: failed to create output file %s (%s)\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
define i32 @write_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11, i32 438)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %19)
store i32 -1, i32* %2
br label %28
21: ; preds = %1
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* %4
%24 = call i32 @copy_data(i32 %22, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %5
store i32 %27, i32* %2
br label %28
28: ; preds = %21, %15
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Error: failed to create output file %s (%s)\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
define i32 @write_file(i8* %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6, i32 438)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12)
br label %18
14: ; preds = %1
%15 = load i32, i32* @STDIN_FILENO
%16 = tail call i32 @copy_data(i32 %15, i32 %7)
%17 = tail call i32 @close(i32 %7)
br label %18
18: ; preds = %14, %9
%19 = phi i32 [ -1, %9 ], [ %16, %14 ]
ret i32 %19
}