#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 /*<<< orphan*/ F_SETFD;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ error(char *, char *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int open(char *, int, int);
int open_write_aux(char *file) {
  int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd < 0) {
    error("esh: cannot open \"%s\" for writing.", file);
    return -1;
  }
  fcntl(fd, F_SETFD, 1);
  return fd;
}


x86 assembly (clang, -O0):
<open_write_aux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rax
or     (%rax),%esi
mov    0x2e97(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118e <open_write_aux+0x5e>
mov    -0x10(%rbp),%rsi
lea    0xe83(%rip),%rdi
call   1040 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <open_write_aux+0x7a>
mov    -0x14(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_write_aux>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    0x2eb6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2ea0(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1179 <open_write_aux+0x49>
mov    %eax,%ebx
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <fcntl@plt>
jmp    118d <open_write_aux+0x5d>
lea    0xe80(%rip),%rdi
mov    %r14,%rsi
call   1040 <error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [35 x i8] c"esh: cannot open \22%s\22 for writing.\00"
@F_SETFD = global i32 0
define i32 @open_write_aux(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 420)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %22
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @F_SETFD
%20 = call i32 @fcntl(i32 %18, i32 %19, i32 1)
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %22
22: ; preds = %17, %14
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [35 x i8] c"esh: cannot open \22%s\22 for writing.\00"
@F_SETFD = global i32 0
define i32 @open_write_aux(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 420)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %14
11: ; preds = %1
%12 = load i32, i32* @F_SETFD
%13 = tail call i32 @fcntl(i32 %7, i32 %12, i32 1)
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ -1, %9 ], [ %7, %11 ]
ret i32 %15
}