#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*/ va_list;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ vfprintf(int /*<<< orphan*/ *, char const *,
                            int /*<<< orphan*/);
void logtofile(const char *format, va_list ap) {
  FILE *fpo = fopen("output.log", "a");
  if (fpo != NULL) {
    vfprintf(fpo, format, ap);
    fclose(fpo);
  } else {
    printf("Error %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<logtofile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119f <logtofile+0x4f>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <vfprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11bc <logtofile+0x6c>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logtofile>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xe9f(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     118f <logtofile+0x3f>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <vfprintf@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe69(%rip),%rdi
mov    %rax,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"output.log\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [10 x i8] c"Error %s\0A\00"
@errno = global i32 0
define void @logtofile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32*, i32** %5
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = call i32 @vfprintf(i32* %10, i8* %11, i32 %12)
%14 = load i32*, i32** %5
%15 = call i32 @fclose(i32* %14)
br label %20
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %16, %9
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"output.log\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [10 x i8] c"Error %s\0A\00"
@errno = global i32 0
define void @logtofile(i8* nocapture %0, i32 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @vfprintf(i32* %3, i8* %0, i32 %1)
%7 = tail call i32 @fclose(i32* %3)
br label %12
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %8, %5
ret void
}