#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_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fchmod(int, int);
scalar_t__ fchown(int, int, int);
int /*<<< orphan*/ *fdopen(int, char *);
int open(char *, int, int);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ unlink(char *);
FILE *lha_arch_fopen(char *filename, int unix_uid, int unix_gid,
                     int unix_perms) {
  FILE *fstream;
  int fileno;
  unlink(filename);
  fileno = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
  if (fileno < 0) {
    return NULL;
  }
  if (unix_uid >= 0) {
    if (fchown(fileno, unix_uid, unix_gid) != 0) {
    }
  }
  if (unix_perms >= 0) {
    if (fchmod(fileno, unix_perms) != 0) {
      close(fileno);
      remove(filename);
      return NULL;
    }
  }
  fstream = fdopen(fileno, "wb");
  if (fstream == NULL) {
    close(fileno);
    remove(filename);
    return NULL;
  }
  return fstream;
}


x86 assembly (clang, -O0):
<lha_arch_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d1 <lha_arch_fopen+0x61>
movq   $0x0,-0x8(%rbp)
jmp    1283 <lha_arch_fopen+0x113>
cmpl   $0x0,-0x14(%rbp)
jl     11fd <lha_arch_fopen+0x8d>
mov    -0x2c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1090 <fchown@plt>
cmp    $0x0,%rax
je     11f8 <lha_arch_fopen+0x88>
jmp    11f8 <lha_arch_fopen+0x88>
jmp    11fd <lha_arch_fopen+0x8d>
cmpl   $0x0,-0x1c(%rbp)
jl     123f <lha_arch_fopen+0xcf>
mov    -0x2c(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1070 <fchmod@plt>
cmp    $0x0,%rax
je     123a <lha_arch_fopen+0xca>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
movq   $0x0,-0x8(%rbp)
jmp    1283 <lha_arch_fopen+0x113>
jmp    123f <lha_arch_fopen+0xcf>
mov    -0x2c(%rbp),%edi
lea    0xdb7(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127b <lha_arch_fopen+0x10b>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
movq   $0x0,-0x8(%rbp)
jmp    1283 <lha_arch_fopen+0x113>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lha_arch_fopen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%rbx
call   1030 <unlink@plt>
mov    0x2e68(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11ff <lha_arch_fopen+0x8f>
mov    %eax,%ebp
test   %r12d,%r12d
js     11c9 <lha_arch_fopen+0x59>
mov    %ebp,%edi
mov    %r12d,%esi
mov    %r15d,%edx
call   1090 <fchown@plt>
test   %r14d,%r14d
js     11dd <lha_arch_fopen+0x6d>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <fchmod@plt>
test   %rax,%rax
jne    11f0 <lha_arch_fopen+0x80>
lea    0xe1c(%rip),%rsi
mov    %ebp,%edi
call   1060 <fdopen@plt>
test   %rax,%rax
jne    1201 <lha_arch_fopen+0x91>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1040 <remove@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@.str = [3 x i8] c"wb\00"
define i32* @lha_arch_fopen(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = call i32 @unlink(i8* %12)
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %15, %16
%18 = load i32, i32* @O_EXCL
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19, i32 384)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %4
store i32* null, i32** %5
br label %61
24: ; preds = %4
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = load i32, i32* %11
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = call i64 @fchown(i32 %28, i32 %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %34
34: ; preds = %33, %27
br label %35
35: ; preds = %34, %24
%36 = load i32, i32* %9
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i32, i32* %11
%40 = load i32, i32* %9
%41 = call i64 @fchmod(i32 %39, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i32, i32* %11
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %6
%47 = call i32 @remove(i8* %46)
store i32* null, i32** %5
br label %61
48: ; preds = %38
br label %49
49: ; preds = %48, %35
%50 = load i32, i32* %11
%51 = call i32* @fdopen(i32 %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %51, i32** %10
%52 = load i32*, i32** %10
%53 = icmp eq i32* %52, null
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = load i32, i32* %11
%56 = call i32 @close(i32 %55)
%57 = load i8*, i8** %6
%58 = call i32 @remove(i8* %57)
store i32* null, i32** %5
br label %61
59: ; preds = %49
%60 = load i32*, i32** %10
store i32* %60, i32** %5
br label %61
61: ; preds = %59, %54, %43, %23
%62 = load i32*, i32** %5
ret i32* %62
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@.str = [3 x i8] c"wb\00"
define noalias i32* @lha_arch_fopen(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @unlink(i8* %0)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_WRONLY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10, i32 384)
%12 = icmp slt i32 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %4
%14 = icmp sgt i32 %1, -1
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = tail call i64 @fchown(i32 %11, i32 %1, i32 %2)
br label %17
17: ; preds = %15, %13
%18 = icmp sgt i32 %3, -1
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = tail call i64 @fchmod(i32 %11, i32 %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19, %17
%23 = tail call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22, %19
%26 = tail call i32 @close(i32 %11)
%27 = tail call i32 @remove(i8* %0)
br label %28
28: ; preds = %25, %22, %4
%29 = phi i32* [ null, %4 ], [ %23, %22 ], [ null, %25 ]
ret i32* %29
}