#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;
struct tm {
  int dummy;
};
typedef int /*<<< orphan*/ newmsg;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int logging(char *filename, char *message) {
  FILE *fp;
  if ((fp = fopen(filename, "a+")) < 0) {
    fprintf(stderr, "openlogging: Failed to open file: %s\n", filename);
    return -1;
  }
  char buf[80];
  time_t now = time(0);
  struct tm tstruct;
  tstruct = *localtime(&now);
  strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
  char newmsg[256];
  snprintf(newmsg, sizeof(newmsg), "[%s]: %s", buf, message);
  fwrite(newmsg, 1, strlen(newmsg), fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<logging>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jae    11e6 <logging+0x56>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <logging+0xe8>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,-0x74(%rbp)
lea    -0x74(%rbp),%rdi
call   1030 <localtime@plt>
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0x50,%esi
lea    0xe1b(%rip),%rdx
lea    -0x78(%rbp),%rcx
call   1090 <strftime@plt>
lea    -0x180(%rbp),%rdi
lea    -0x70(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x100,%esi
lea    0xe03(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x180(%rbp),%rax
mov    %rax,-0x188(%rbp)
lea    -0x180(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x188(%rbp),%rdi
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logging>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x168,%rsp
mov    %rsi,%r14
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe41(%rip),%rdx
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    $0x50,%esi
call   1080 <strftime@plt>
lea    0xe2f(%rip),%rdx
lea    0x60(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x168,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [3 x i8] c"a+\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"openlogging: Failed to open file: %s\0A\00"
@.str.2 = [12 x i8] c"%Y-%m-%d %X\00"
@.str.3 = [9 x i8] c"[%s]: %s\00"
define i32 @logging(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [80 x i8]
%8 = alloca i32
%9 = alloca %struct.tm
%10 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = icmp ult i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %36
18: ; preds = %2
%19 = call i32 @time(i32 0)
store i32 %19, i32* %8
%20 = call %struct.tm* @localtime(i32* %8)
%21 = bitcast %struct.tm* %9 to i8*
%22 = bitcast %struct.tm* %20 to i8*
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%24 = call i32 @strftime(i8* %23, i32 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %9)
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%27 = load i8*, i8** %5
%28 = call i32 @snprintf(i8* %25, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* %27)
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = load i32*, i32** %6
%33 = call i32 @fwrite(i8* %29, i32 1, i32 %31, i32* %32)
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %3
br label %36
36: ; preds = %18, %14
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [3 x i8] c"a+\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"%Y-%m-%d %X\00"
@.str.3 = [9 x i8] c"[%s]: %s\00"
define i32 @logging(i8* nocapture %0, i8* %1) {
%3 = alloca [80 x i8]
%4 = alloca i32
%5 = alloca %struct.tm
%6 = alloca [256 x i8]
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
%10 = tail call i32 @time(i32 0)
store i32 %10, i32* %4
%11 = bitcast %struct.tm* %5 to i8*
%12 = call %struct.tm* @localtime(i32* %4)
%13 = getelementptr %struct.tm, %struct.tm* %12, i64 0, i32 0
%14 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 0
%15 = load i32, i32* %13
store i32 %15, i32* %14
%16 = call i32 @strftime(i8* %8, i32 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %5)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%18 = call i32 @snprintf(i8* %17, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* %1)
%19 = call i32 @strlen(i8* %17)
%20 = call i32 @fwrite(i8* %17, i32 1, i32 %19, i32* %7)
%21 = call i32 @fclose(i32* %7)
ret i32 0
}