#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ io_error(char *);
int open(char *, int, int);
int unlink(char *);
void newfile(char *name, int *fd, FILE **stream, int create) {
  if (create) {
    if (unlink(name) == -1 && *fd != -1)
      io_error("unlink");
    *fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0777);
  } else
    *fd = open(name, O_RDWR, 0777);
  if (*fd == -1)
    io_error(name);
  *stream = fdopen(*fd, "r+");
  if (*stream == NULL)
    io_error("fdopen");
}


x86 assembly (clang, -O0):
<newfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11c2 <newfile+0x82>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    118c <newfile+0x4c>
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
je     118c <newfile+0x4c>
lea    0xe79(%rip),%rdi
call   1060 <io_error@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e1 <newfile+0xa1>
mov    -0x8(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11f7 <newfile+0xb7>
mov    -0x8(%rbp),%rdi
call   1060 <io_error@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1040 <fdopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    122d <newfile+0xed>
lea    0xde2(%rip),%rdi
call   1060 <io_error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newfile>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
test   %ecx,%ecx
je     118e <newfile+0x4e>
mov    %rbx,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1171 <newfile+0x31>
cmpl   $0xffffffff,(%r15)
je     1171 <newfile+0x31>
lea    0xe94(%rip),%rdi
call   1060 <io_error@plt>
mov    0x2e68(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
jmp    1197 <newfile+0x57>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    %eax,(%r15)
cmp    $0xffffffff,%eax
jne    11b7 <newfile+0x77>
mov    %rbx,%rdi
call   1060 <io_error@plt>
mov    (%r15),%eax
lea    0xe49(%rip),%rsi
mov    %eax,%edi
call   1040 <fdopen@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11d3 <newfile+0x93>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe30(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <io_error@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"unlink\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [7 x i8] c"fdopen\00"
define void @newfile(i8* %0, i32* %1, i32** %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32**
%8 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32** %2, i32*** %7
store i32 %3, i32* %8
%9 = load i32, i32* %8
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %4
%12 = load i8*, i8** %5
%13 = call i32 @unlink(i8* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32*, i32** %6
%17 = load i32, i32* %16
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @io_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %15, %11
%22 = load i8*, i8** %5
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_EXCL
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %22, i32 %27, i32 511)
%29 = load i32*, i32** %6
store i32 %28, i32* %29
br label %35
30: ; preds = %4
%31 = load i8*, i8** %5
%32 = load i32, i32* @O_RDWR
%33 = call i32 @open(i8* %31, i32 %32, i32 511)
%34 = load i32*, i32** %6
store i32 %33, i32* %34
br label %35
35: ; preds = %30, %21
%36 = load i32*, i32** %6
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i8*, i8** %5
%41 = call i32 @io_error(i8* %40)
br label %42
42: ; preds = %39, %35
%43 = load i32*, i32** %6
%44 = load i32, i32* %43
%45 = call i32* @fdopen(i32 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%46 = load i32**, i32*** %7
store i32* %45, i32** %46
%47 = load i32**, i32*** %7
%48 = load i32*, i32** %47
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @io_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %42
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"unlink\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [7 x i8] c"fdopen\00"
define void @newfile(i8* %0, i32* nocapture %1, i32** nocapture writeonly %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %20, label %6
6: ; preds = %4
%7 = tail call i32 @unlink(i8* %0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, -1
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @io_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9, %6
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_EXCL
%19 = or i32 %17, %18
br label %22
20: ; preds = %4
%21 = load i32, i32* @O_RDWR
br label %22
22: ; preds = %20, %14
%23 = phi i32 [ %21, %20 ], [ %19, %14 ]
%24 = tail call i32 @open(i8* %0, i32 %23, i32 511)
store i32 %24, i32* %1
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = tail call i32 @io_error(i8* %0)
%28 = load i32, i32* %1
br label %29
29: ; preds = %26, %22
%30 = phi i32 [ %28, %26 ], [ %24, %22 ]
%31 = tail call i32* @fdopen(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %2
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = tail call i32 @io_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
ret void
}