#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int setup_trace_log_file_ext(char **log_filename, FILE **logfp, char *filename,
                             int flags) {
  char *open_mode_str = "w";
  if ((*log_filename) && (*logfp)) {
    fflush(*logfp);
    fclose(*logfp);
  }
  *log_filename = filename;
  if (flags & 0x1) {
    open_mode_str = "a";
  }
  if ((*logfp = fopen(*log_filename, open_mode_str)) == NULL) {
    fprintf(stderr, "\nFailed to open log trace file %s\n", *log_filename);
    *log_filename = NULL;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_trace_log_file_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    0xea2(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1196 <setup_trace_log_file_ext+0x56>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1196 <setup_trace_log_file_ext+0x56>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <fflush@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11bb <setup_trace_log_file_ext+0x7b>
lea    0xe4b(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <fopen@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    120f <setup_trace_log_file_ext+0xcf>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe11(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1216 <setup_trace_log_file_ext+0xd6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_trace_log_file_ext>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
cmpq   $0x0,(%rdi)
je     116d <setup_trace_log_file_ext+0x2d>
mov    (%r15),%rdi
test   %rdi,%rdi
je     116d <setup_trace_log_file_ext+0x2d>
call   1050 <fflush@plt>
mov    (%r15),%rdi
call   1030 <fclose@plt>
mov    %rbx,(%r14)
test   $0x1,%bpl
lea    0xe85(%rip),%rax
lea    0xe80(%rip),%rsi
cmove  %rax,%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rax,(%r15)
xor    %eax,%eax
test   %rcx,%rcx
jne    11bf <setup_trace_log_file_ext+0x7f>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe56(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,(%r14)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"\0AFailed to open log trace file %s\0A\00"
define i32 @setup_trace_log_file_ext(i8** %0, i32** %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i8** %0, i8*** %6
store i32** %1, i32*** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = load i8**, i8*** %6
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %25
14: ; preds = %4
%15 = load i32**, i32*** %7
%16 = load i32*, i32** %15
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i32**, i32*** %7
%20 = load i32*, i32** %19
%21 = call i32 @fflush(i32* %20)
%22 = load i32**, i32*** %7
%23 = load i32*, i32** %22
%24 = call i32 @fclose(i32* %23)
br label %25
25: ; preds = %18, %14, %4
%26 = load i8*, i8** %8
%27 = load i8**, i8*** %6
store i8* %26, i8** %27
%28 = load i32, i32* %9
%29 = and i32 %28, 1
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %32
32: ; preds = %31, %25
%33 = load i8**, i8*** %6
%34 = load i8*, i8** %33
%35 = load i8*, i8** %10
%36 = call i32* @fopen(i8* %34, i8* %35)
%37 = load i32**, i32*** %7
store i32* %36, i32** %37
%38 = icmp eq i32* %36, null
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %6
%42 = load i8*, i8** %41
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i8**, i8*** %6
store i8* null, i8** %44
store i32 1, i32* %5
br label %46
45: ; preds = %32
store i32 0, i32* %5
br label %46
46: ; preds = %45, %39
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"\0AFailed to open log trace file %s\0A\00"
define i32 @setup_trace_log_file_ext(i8** nocapture %0, i32** nocapture %1, i8* %2, i32 %3) {
%5 = load i8*, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = load i32*, i32** %1
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = tail call i32 @fflush(i32* %8)
%12 = load i32*, i32** %1
%13 = tail call i32 @fclose(i32* %12)
br label %14
14: ; preds = %10, %7, %4
store i8* %2, i8** %0
%15 = and i32 %3, 1
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%18 = tail call i32* @fopen(i8* %2, i8* %17)
store i32* %18, i32** %1
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %0
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %22)
store i8* null, i8** %0
br label %24
24: ; preds = %14, %20
%25 = phi i32 [ 1, %20 ], [ 0, %14 ]
ret i32 %25
}