#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ stderr;
void appendLineToCsvFile(char *fname, char *header_line, char *lineToAppend) {
  FILE *fd;
  if ((fd = fopen(fname, "r")) == NULL) {
    if ((fd = fopen(fname, "w")) == NULL) {
      fprintf(stderr,
              "RTL-Wx: Unable to open %s file for writing.  Exiting...\n\n",
              fname);
      exit(1);
    } else {
      fprintf(fd, header_line);
      fclose(fd);
    }
  } else
    fclose(fd);
  if ((fd = fopen(fname, "a")) == NULL) {
    fprintf(stderr,
            "RTL-Wx: Unable to open %s file for appending.  Exiting...\n\n",
            fname);
    exit(1);
  }
  fprintf(fd, lineToAppend);
  fclose(fd);
}


x86 assembly (clang, -O0):
<appendLineToCsvFile>:
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    0xea1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d3 <appendLineToCsvFile+0x93>
mov    -0x8(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b6 <appendLineToCsvFile+0x76>
mov    0x2e49(%rip),%rax
movslq (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11dc <appendLineToCsvFile+0x9c>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1220 <appendLineToCsvFile+0xe0>
mov    0x2ddf(%rip),%rax
movslq (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
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):
<appendLineToCsvFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1189 <appendLineToCsvFile+0x49>
lea    0xe96(%rip),%rsi
mov    %r12,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11db <appendLineToCsvFile+0x9b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xea6(%rip),%rsi
mov    %r12,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c8 <appendLineToCsvFile+0x88>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
mov    0x2e11(%rip),%rax
movslq (%rax),%rdi
lea    0xe67(%rip),%rsi
jmp    11ec <appendLineToCsvFile+0xac>
mov    0x2dfe(%rip),%rax
movslq (%rax),%rdi
lea    0xe18(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [58 x i8] c"RTL-Wx: Unable to open %s file for writing. Exiting...\0A\0A\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [60 x i8] c"RTL-Wx: Unable to open %s file for appending. Exiting...\0A\0A\00"
define void @appendLineToCsvFile(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 ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %29
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
%19 = load i8*, i8** %4
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = load i32*, i32** %7
%24 = load i8*, i8** %5
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* %24)
%26 = load i32*, i32** %7
%27 = call i32 @fclose(i32* %26)
br label %28
28: ; preds = %22
br label %32
29: ; preds = %3
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
br label %32
32: ; preds = %29, %28
%33 = load i8*, i8** %4
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %7
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = sext i32 %37 to i64
%39 = inttoptr i64 %38 to i32*
%40 = load i8*, i8** %4
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = load i32*, i32** %7
%45 = load i8*, i8** %6
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* %45)
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [58 x i8] c"RTL-Wx: Unable to open %s file for writing. Exiting...\0A\0A\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [60 x i8] c"RTL-Wx: Unable to open %s file for appending. Exiting...\0A\0A\00"
define void @appendLineToCsvFile(i8* %0, i8* nocapture %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %17
6: ; preds = %3
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %6
%16 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* %1)
br label %17
17: ; preds = %3, %15
%18 = phi i32* [ %7, %15 ], [ %4, %3 ]
%19 = tail call i32 @fclose(i32* %18)
%20 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i32*
%26 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* %2)
%30 = tail call i32 @fclose(i32* %20)
ret void
}