#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_EXCL;
int O_RDWR;
int O_TRUNC;
int open(char const *, int, int);
int /*<<< orphan*/ unlink(char const *);
int mc_temp_file(const char *filename) {
  int fd = -1;
  if (!filename) {
    return -1;
  }
  fd = open(filename, O_CREAT | O_EXCL | O_TRUNC | O_RDWR | 0 | 0, 0755);
  if (fd > 0) {
    unlink(filename);
  }
  return fd;
}


x86 assembly (clang, -O0):
<mc_temp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114a <mc_temp_file+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <mc_temp_file+0x7e>
mov    -0x10(%rbp),%rdi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
or     $0x0,%esi
mov    $0x1ed,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1198 <mc_temp_file+0x78>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mc_temp_file>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1169 <mc_temp_file+0x49>
mov    %rdi,%rbx
mov    0x2ec6(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2eac(%rip),%rax
or     (%rax),%esi
mov    0x2e8b(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   1040 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    116e <mc_temp_file+0x4e>
mov    %rbx,%rdi
call   1030 <unlink@plt>
jmp    116e <mc_temp_file+0x4e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
define i32 @mc_temp_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i32 -1, i32* %2
br label %27
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_EXCL
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = load i32, i32* @O_RDWR
%16 = or i32 %14, %15
%17 = or i32 %16, 0
%18 = or i32 %17, 0
%19 = call i32 @open(i8* %9, i32 %18, i32 493)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %8
%23 = load i8*, i8** %3
%24 = call i32 @unlink(i8* %23)
br label %25
25: ; preds = %22, %8
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %7
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
define i32 @mc_temp_file(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_EXCL
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10, i32 493)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 @unlink(i8* %0)
br label %15
15: ; preds = %3, %13, %1
%16 = phi i32 [ -1, %1 ], [ %11, %13 ], [ %11, %3 ]
ret i32 %16
}