#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void write(char *filename, char *txt, char *var) {
  FILE *f = fopen(filename, "a+");
  if (f != NULL) {
    if (var == NULL) {
      fprintf(f, txt);
    } else {
      fprintf(f, txt, var);
    }
    fclose(f);
  }
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119e <write+0x6e>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <write+0x52>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1195 <write+0x65>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1166 <write+0x36>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
test   %r15,%r15
je     116c <write+0x3c>
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1173 <write+0x43>
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"a+\00"
define void @write(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %27
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32*, i32** %7
%17 = load i8*, i8** %5
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* %17)
br label %24
19: ; preds = %12
%20 = load i32*, i32** %7
%21 = load i8*, i8** %5
%22 = load i8*, i8** %6
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* %21, i8* %22)
br label %24
24: ; preds = %19, %15
%25 = load i32*, i32** %7
%26 = call i32 @fclose(i32* %25)
br label %27
27: ; preds = %24, %3
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"a+\00"
define void @write(i8* nocapture %0, i8* nocapture %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = icmp eq i8* %2, null
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* %1)
br label %12
10: ; preds = %6
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* %1, i8* %2)
br label %12
12: ; preds = %10, %8
%13 = tail call i32 @fclose(i32* %4)
br label %14
14: ; preds = %12, %3
ret void
}