#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int O_CREAT;
int O_NOFOLLOW;
int O_TRUNC;
int O_WRONLY;
scalar_t__ lstat(char const *, struct stat *);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int open_log(const char *filename, mode_t mode) {
  int fd;
  struct stat st;
  if (lstat(filename, &st) >= 0)
    unlink(filename);
  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, mode);
  return fd;
}


x86 assembly (clang, -O0):
<open_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
jl     115f <open_log+0x2f>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
or     (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
mov    0x2e6b(%rip),%rax
or     (%rax),%esi
mov    -0xc(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_log>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %rax,%rax
js     114d <open_log+0x1d>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e6c(%rip),%rax
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e82(%rip),%rax
or     (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <open@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
define i32 @open_log(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i64 @lstat(i8* %7, %struct.stat* %6)
%9 = icmp sge i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = call i32 @unlink(i8* %11)
br label %13
13: ; preds = %10, %2
%14 = load i8*, i8** %3
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_NOFOLLOW
%21 = or i32 %19, %20
%22 = load i32, i32* %4
%23 = call i32 @open(i8* %14, i32 %21, i32 %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
define i32 @open_log(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp sgt i64 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @unlink(i8* %0)
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = load i32, i32* @O_NOFOLLOW
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %0, i32 %16, i32 %1)
ret i32 %17
}