#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 */
typedef int mode_t;

/* Variables and functions */
int ENOENT;
int O_CREAT;
int O_EXCL;
int O_NOFOLLOW;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int errno;
int open(char *, int, int);
scalar_t__ strcmp(char *, char *);
int unlink(char *);
int mx_open_newfile(char *fname) {
  int fh;
  int res;
  int flags = 0;
  mode_t mode = 0;
  flags |= O_CREAT | O_WRONLY | O_TRUNC;
  flags |= O_NOFOLLOW;
  mode |= S_IRUSR | S_IWUSR;
  mode |= S_IRGRP | S_IWGRP;
  mode |= S_IROTH | S_IWOTH;
  if (!fname) {
    fname = "/dev/null";
  } else if (strcmp(fname, "/dev/null") != 0) {
    res = unlink(fname);
    if (res == -1 && errno != ENOENT)
      return -errno;
    flags |= O_EXCL;
  }
  fh = open(fname, flags, mode);
  if (fh == -1)
    return -errno;
  return fh;
}


x86 assembly (clang, -O0):
<mx_open_newfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    0x2e26(%rip),%rcx
or     (%rcx),%eax
mov    0x2e85(%rip),%rcx
or     (%rcx),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    0x2e3e(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    0x2e2e(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    0x2dbe(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11dd <mx_open_newfile+0xad>
lea    0xe2c(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    124c <mx_open_newfile+0x11c>
mov    -0x10(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     1247 <mx_open_newfile+0x117>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1238 <mx_open_newfile+0x108>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%eax
je     1238 <mx_open_newfile+0x108>
xor    %eax,%eax
mov    0x2d82(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1281 <mx_open_newfile+0x151>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    124c <mx_open_newfile+0x11c>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    127b <mx_open_newfile+0x14b>
xor    %eax,%eax
mov    0x2d3f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1281 <mx_open_newfile+0x151>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mx_open_newfile>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ebd(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e9b(%rip),%rax
or     (%rax),%ebp
mov    0x2e9a(%rip),%rax
or     (%rax),%ebp
mov    0x2e39(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%ebx
mov    0x2e39(%rip),%rcx
mov    0x2e5a(%rip),%rdx
mov    0x2e23(%rip),%rsi
or     (%rax),%ebx
or     (%rcx),%ebx
or     (%rdx),%ebx
or     (%rsi),%ebx
mov    0x2dec(%rip),%rax
or     (%rax),%ebx
test   %rdi,%rdi
je     11d3 <mx_open_newfile+0xa3>
mov    %rdi,%r14
lea    0xe63(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11da <mx_open_newfile+0xaa>
mov    %r14,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11c8 <mx_open_newfile+0x98>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%eax
jne    11fb <mx_open_newfile+0xcb>
mov    0x2de9(%rip),%rax
or     (%rax),%ebp
jmp    11da <mx_open_newfile+0xaa>
lea    0xe26(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    11f6 <mx_open_newfile+0xc6>
mov    0x2dbe(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
neg    %eax
jmp    11f6 <mx_open_newfile+0xc6>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@errno = global i32 0
@ENOENT = global i32 0
@O_EXCL = global i32 0
define i32 @mx_open_newfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* %6
%14 = or i32 %13, %12
store i32 %14, i32* %6
%15 = load i32, i32* @O_NOFOLLOW
%16 = load i32, i32* %6
%17 = or i32 %16, %15
store i32 %17, i32* %6
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = load i32, i32* %7
%22 = or i32 %21, %20
store i32 %22, i32* %7
%23 = load i32, i32* @S_IRGRP
%24 = load i32, i32* @S_IWGRP
%25 = or i32 %23, %24
%26 = load i32, i32* %7
%27 = or i32 %26, %25
store i32 %27, i32* %7
%28 = load i32, i32* @S_IROTH
%29 = load i32, i32* @S_IWOTH
%30 = or i32 %28, %29
%31 = load i32, i32* %7
%32 = or i32 %31, %30
store i32 %32, i32* %7
%33 = load i8*, i8** %3
%34 = icmp ne i8* %33, null
br i1 %34, label %36, label %35
35: ; preds = %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %3
br label %57
36: ; preds = %1
%37 = load i8*, i8** %3
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %56
40: ; preds = %36
%41 = load i8*, i8** %3
%42 = call i32 @unlink(i8* %41)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %52
45: ; preds = %40
%46 = load i32, i32* @errno
%47 = load i32, i32* @ENOENT
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* @errno
%51 = sub nsw i32 0, %50
store i32 %51, i32* %2
br label %69
52: ; preds = %45, %40
%53 = load i32, i32* @O_EXCL
%54 = load i32, i32* %6
%55 = or i32 %54, %53
store i32 %55, i32* %6
br label %56
56: ; preds = %52, %36
br label %57
57: ; preds = %56, %35
%58 = load i8*, i8** %3
%59 = load i32, i32* %6
%60 = load i32, i32* %7
%61 = call i32 @open(i8* %58, i32 %59, i32 %60)
store i32 %61, i32* %4
%62 = load i32, i32* %4
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %57
%65 = load i32, i32* @errno
%66 = sub nsw i32 0, %65
store i32 %66, i32* %2
br label %69
67: ; preds = %57
%68 = load i32, i32* %4
store i32 %68, i32* %2
br label %69
69: ; preds = %67, %64, %49
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@errno = global i32 0
@ENOENT = global i32 0
@O_EXCL = global i32 0
define i32 @mx_open_newfile(i8* %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_WRONLY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = load i32, i32* @O_NOFOLLOW
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = load i32, i32* @S_IRGRP
%12 = load i32, i32* @S_IWGRP
%13 = load i32, i32* @S_IROTH
%14 = load i32, i32* @S_IWOTH
%15 = or i32 %10, %9
%16 = or i32 %15, %11
%17 = or i32 %16, %12
%18 = or i32 %17, %13
%19 = or i32 %18, %14
%20 = icmp eq i8* %0, null
br i1 %20, label %36, label %21
21: ; preds = %1
%22 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %21
%25 = tail call i32 @unlink(i8* %0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = load i32, i32* @ENOENT
%30 = icmp eq i32 %28, %29
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = sub nsw i32 0, %28
br label %44
33: ; preds = %27, %24
%34 = load i32, i32* @O_EXCL
%35 = or i32 %34, %8
br label %36
36: ; preds = %1, %21, %33
%37 = phi i8* [ %0, %33 ], [ %0, %21 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %1 ]
%38 = phi i32 [ %35, %33 ], [ %8, %21 ], [ %8, %1 ]
%39 = tail call i32 @open(i8* %37, i32 %38, i32 %19)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
br label %44
44: ; preds = %36, %41, %31
%45 = phi i32 [ %32, %31 ], [ %43, %41 ], [ %39, %36 ]
ret i32 %45
}