#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 */
struct stream {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ die(char *, char *);
int open(char *, int, int);
struct stream *sopen_fd(char *, int);
struct stream *sopen_write(char *name) {
  int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd < 0)
    die("Unable to open %s for writing: %m", name);
  return sopen_fd(name, fd);
}


x86 assembly (clang, -O0):
<sopen_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rax
or     (%rax),%esi
mov    0x2e97(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1182 <sopen_write+0x52>
mov    -0x8(%rbp),%rsi
lea    0xe83(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <sopen_fd@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sopen_write>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rax
mov    0x2eb7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%rbx
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1170 <sopen_write+0x40>
lea    0xe98(%rip),%rdi
mov    %rbx,%rsi
call   1050 <die@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <sopen_fd@plt>

LLVM IR (-O0):
%struct.stream = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [34 x i8] c"Unable to open %s for writing: %m\00"
define %struct.stream* @sopen_write(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %4, i32 %9, i32 438)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32 @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %1
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = call %struct.stream* @sopen_fd(i8* %17, i32 %18)
ret %struct.stream* %19
}

LLVM IR (-O2):
%struct.stream = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [34 x i8] c"Unable to open %s for writing: %m\00"
define %struct.stream* @sopen_write(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 slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %1
%12 = tail call %struct.stream* @sopen_fd(i8* %0, i32 %7)
ret %struct.stream* %12
}