#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 */
struct timeval {
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ctime_r(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdout;
int strlen(char *);
void addlogline(char *line, char *logfile) {
  FILE *lf = NULL;
  struct timeval tv;
  char date[64];
  gettimeofday(&tv, NULL);
  ctime_r(&tv.tv_sec, date);
  date[strlen(date) - 1] = '\0';
  if (logfile) {
    lf = fopen(logfile, "a");
  }
  if (lf) {
    fprintf(lf, "%s: %s\n", date, line);
    fclose(lf);
  } else {
    fprintf(stdout, "%s: %s\n", date, line);
  }
}


x86 assembly (clang, -O0):
<addlogline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1070 <ctime_r@plt>
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x60(%rbp,%rax,1)
cmpq   $0x0,-0x10(%rbp)
je     11c4 <addlogline+0x64>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f5 <addlogline+0x95>
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1213 <addlogline+0xb3>
mov    0x2de4(%rip),%rax
mov    (%rax),%rdi
lea    -0x60(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addlogline>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x10(%rsp),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <ctime_r@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
test   %r12,%r12
je     11dc <addlogline+0x7c>
lea    0xe55(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11dc <addlogline+0x7c>
mov    %rax,%rbx
lea    0xe40(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
mov    %r14,%rcx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11fa <addlogline+0x9a>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rdi
lea    0xe15(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %r14,%rcx
call   1060 <fprintf@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [2 x i8] c"a\00"
@.str.1 = [8 x i8] c"%s: %s\0A\00"
@stdout = global i32* null
define void @addlogline(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.timeval
%7 = alloca [64 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32* null, i32** %5
%8 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%11 = call i32 @ctime_r(i32* %9, i8* %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 %15
store i8 0, i8* %16
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %5
br label %22
22: ; preds = %19, %2
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32*, i32** %5
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%28 = load i8*, i8** %3
%29 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28)
%30 = load i32*, i32** %5
%31 = call i32 @fclose(i32* %30)
br label %37
32: ; preds = %22
%33 = load i32*, i32** @stdout
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%35 = load i8*, i8** %3
%36 = call i32 @fprintf(i32* %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %35)
br label %37
37: ; preds = %32, %25
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [2 x i8] c"a\00"
@.str.1 = [8 x i8] c"%s: %s\0A\00"
@stdout = global i32* null
define void @addlogline(i8* %0, i8* %1) {
%3 = alloca %struct.timeval
%4 = alloca [64 x i8]
%5 = bitcast %struct.timeval* %3 to i8*
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%9 = call i32 @ctime_r(i32* %8, i8* %6)
%10 = call i32 @strlen(i8* %6)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %12
store i8 0, i8* %13
%14 = icmp eq i8* %1, null
br i1 %14, label %21, label %15
15: ; preds = %2
%16 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %0)
%20 = call i32 @fclose(i32* %16)
br label %24
21: ; preds = %2, %15
%22 = load i32*, i32** @stdout
%23 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %0)
br label %24
24: ; preds = %21, %18
ret void
}