#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(char **log_filename, FILE **logfp, char *filename) {
  if ((*log_filename) && (*logfp)) {
    fflush(*logfp);
    fclose(*logfp);
  }
  *log_filename = filename;
  if ((*logfp = fopen(*log_filename, "a")) == 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>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1188 <setup_trace_log_file+0x48>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1188 <setup_trace_log_file+0x48>
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    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe5f(%rip),%rsi
call   1060 <fopen@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11ea <setup_trace_log_file+0xaa>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe34(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11f1 <setup_trace_log_file+0xb1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_trace_log_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
cmpq   $0x0,(%rdi)
je     1169 <setup_trace_log_file+0x29>
mov    (%r15),%rdi
test   %rdi,%rdi
je     1169 <setup_trace_log_file+0x29>
call   1050 <fflush@plt>
mov    (%r15),%rdi
call   1030 <fclose@plt>
mov    %rbx,(%r14)
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rax,(%r15)
xor    %eax,%eax
test   %rcx,%rcx
jne    11ac <setup_trace_log_file+0x6c>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,(%r14)
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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