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

/* Variables and functions */
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, int, char *);
int getpid();
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int filelog(char *filename, char *text) {
  char timestr[40];
  time_t clk;
  clk = time(NULL);
  sprintf(timestr, "%s", ctime(&clk));
  timestr[strlen(timestr) - 1] = '\0';
  printf("Logging %s to %s\n", text, filename);
  FILE *f;
  if ((f = fopen(filename, "a")) != NULL) {
    fprintf(f, "%s - %d : %s\n", timestr, getpid(), text);
    fclose(f);
    return 1;
  }
  printf("Error logging %s to %s\n", text, filename);
  return 0;
}


x86 assembly (clang, -O0):
<filelog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0x44(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x44(%rbp),%rdi
call   1050 <ctime@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rdx
lea    0xe35(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x40(%rbp,%rax,1)
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe11(%rip),%rdi
call   1070 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     125b <filelog+0xcb>
mov    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
mov    -0x18(%rbp),%r8
lea    0xdd6(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x50(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <filelog+0xe6>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdbb(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filelog>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r15
mov    %rdi,%r12
xor    %r14d,%r14d
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1060 <ctime@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe21(%rip),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <printf@plt>
lea    0xe21(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1231 <filelog+0xa1>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe05(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %eax,%ecx
mov    %r15,%r8
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%r14d
jmp    1243 <filelog+0xb3>
lea    0xdea(%rip),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <printf@plt>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str.1 = [18 x i8] c"Logging %s to %s\0A\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [14 x i8] c"%s - %d : %s\0A\00"
@.str.4 = [24 x i8] c"Error logging %s to %s\0A\00"
define i32 @filelog(i8* %0, i8* %1) {
%3 = alloca [40 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 @time(i32* null)
store i32 %7, i32* %4
%8 = call i8* @ctime(i32* %4)
%9 = call i8* @strcpy(i8* %5, i8* dereferenceable(1) %8)
%10 = call i32 @strlen(i8* %5)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%15 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = call i32 (...) @getpid()
%19 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %5, i32 %18, i8* %1)
%20 = call i32 @fclose(i32* %15)
br label %23
21: ; preds = %2
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %0)
br label %23
23: ; preds = %21, %17
%24 = phi i32 [ 1, %17 ], [ 0, %21 ]
ret i32 %24
}