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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ strftime(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ vfprintf(int /*<<< orphan*/ *, char const *,
                            int /*<<< orphan*/);
void vlog2file(int prepend_time, const char *filename, const char *format,
               va_list args) {
  FILE *fp;
  time_t lt;
  char buf[40];
  lt = time(NULL);
  *buf = '\0';
  if ((fp = fopen(filename, "ab")) == NULL) {
    fprintf(stderr, "Unable to open %s!\n", filename);
    if (prepend_time)
      strftime(buf, 32, "%Y-%m-%dT%H:%M:%S", localtime(&lt));
    fprintf(stderr, "%.32s: ", buf);
    vfprintf(stderr, format, args);
  } else {
    if (prepend_time) {
      strftime(buf, 32, "%Y-%m-%dT%H:%M:%S", localtime(&lt));
      fprintf(fp, "%.32s: ", buf);
    }
    vfprintf(fp, format, args);
    fprintf(fp, "\n");
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<vlog2file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x2c(%rbp)
movb   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1239 <vlog2file+0xc9>
mov    0x2e25(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x4(%rbp)
je     1202 <vlog2file+0x92>
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <localtime@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%ecx
mov    $0x20,%esi
lea    0xe1a(%rip),%rdx
call   1070 <strftime@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%rdi
lea    -0x60(%rbp),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <vfprintf@plt>
jmp    12ac <vlog2file+0x13c>
cmpl   $0x0,-0x4(%rbp)
je     1281 <vlog2file+0x111>
lea    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <localtime@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%ecx
mov    $0x20,%esi
lea    0xdb1(%rip),%rdx
call   1070 <strftime@plt>
mov    -0x28(%rbp),%rdi
lea    -0x60(%rbp),%rdx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <vfprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vlog2file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r12d
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
movb   $0x0,0x10(%rsp)
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1222 <vlog2file+0xa2>
mov    %rax,%rbp
test   %r12d,%r12d
je     11fd <vlog2file+0x7d>
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xe42(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %eax,%ecx
call   1080 <strftime@plt>
lea    0xe39(%rip),%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   10a0 <vfprintf@plt>
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    128b <vlog2file+0x10b>
mov    0x2db7(%rip),%rbp
mov    0x0(%rbp),%rdi
lea    0xdcf(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r12d,%r12d
je     1265 <vlog2file+0xe5>
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xdc3(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x20,%esi
mov    %eax,%ecx
call   1080 <strftime@plt>
mov    0x0(%rbp),%rdi
lea    0xdb9(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   10a0 <vfprintf@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ab\00"
@stderr = global i32* null
@.str.1 = [20 x i8] c"Unable to open %s!\0A\00"
@.str.2 = [18 x i8] c"%Y-%m-%dT%H:%M:%S\00"
@.str.3 = [8 x i8] c"%.32s: \00"
@.str.4 = [2 x i8] c"\0A\00"
define void @vlog2file(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [40 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = call i32 @time(i32* null)
store i32 %12, i32* %10
%13 = getelementptr inbounds [40 x i8], [40 x i8]* %11, i64 0, i64 0
store i8 0, i8* %13
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %35
17: ; preds = %4
%18 = load i32*, i32** @stderr
%19 = load i8*, i8** %6
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = getelementptr inbounds [40 x i8], [40 x i8]* %11, i64 0, i64 0
%25 = call i32 @localtime(i32* %10)
%26 = call i32 @strftime(i8* %24, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %17
%28 = load i32*, i32** @stderr
%29 = getelementptr inbounds [40 x i8], [40 x i8]* %11, i64 0, i64 0
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** @stderr
%32 = load i8*, i8** %7
%33 = load i32, i32* %8
%34 = call i32 @vfprintf(i32* %31, i8* %32, i32 %33)
br label %54
35: ; preds = %4
%36 = load i32, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = getelementptr inbounds [40 x i8], [40 x i8]* %11, i64 0, i64 0
%40 = call i32 @localtime(i32* %10)
%41 = call i32 @strftime(i8* %39, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i32*, i32** %9
%43 = getelementptr inbounds [40 x i8], [40 x i8]* %11, i64 0, i64 0
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %38, %35
%46 = load i32*, i32** %9
%47 = load i8*, i8** %7
%48 = load i32, i32* %8
%49 = call i32 @vfprintf(i32* %46, i8* %47, i32 %48)
%50 = load i32*, i32** %9
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32*, i32** %9
%53 = call i32 @fclose(i32* %52)
br label %54
54: ; preds = %45, %27
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"ab\00"
@stderr = global i32* null
@.str.1 = [20 x i8] c"Unable to open %s!\0A\00"
@.str.2 = [18 x i8] c"%Y-%m-%dT%H:%M:%S\00"
@.str.3 = [8 x i8] c"%.32s: \00"
define void @vlog2file(i32 %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = alloca i32
%6 = alloca [40 x i8]
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%9 = tail call i32 @time(i32* null)
store i32 %9, i32* %5
store i8 0, i8* %8
%10 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %24
12: ; preds = %4
%13 = load i32*, i32** @stderr
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%15 = icmp eq i32 %0, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = call i32 @localtime(i32* %5)
%18 = call i32 @strftime(i8* %8, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %12
%20 = load i32*, i32** @stderr
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%22 = load i32*, i32** @stderr
%23 = call i32 @vfprintf(i32* %22, i8* %2, i32 %3)
br label %34
24: ; preds = %4
%25 = icmp eq i32 %0, 0
br i1 %25, label %30, label %26
26: ; preds = %24
%27 = call i32 @localtime(i32* %5)
%28 = call i32 @strftime(i8* %8, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %30
30: ; preds = %26, %24
%31 = call i32 @vfprintf(i32* %10, i8* %2, i32 %3)
%32 = call i32 @fputc(i32 10, i32* %10)
%33 = call i32 @fclose(i32* %10)
br label %34
34: ; preds = %30, %19
ret void
}