#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*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, ...);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int log_msg(char *msg) {
  FILE *file = fopen("log/master.log", "a");
  if (file == NULL) {
    fprintf(stderr, "[ERROR] Failed to open log file. System said: %s\n",
            strerror(errno));
    return EXIT_FAILURE;
  }
  time_t current_time = 0;
  time(&current_time);
  struct tm *timeinfo = localtime(&current_time);
  char timestr[40];
  strftime(timestr, 40, "%Y-%m-%dT%H:%M:%S %Z", timeinfo);
  if (ferror(file) != 0) {
    fprintf(stderr, "[ERROR] Failed to write to log file. System said: %s\n",
            strerror(errno));
  }
  fprintf(file, "%s %s\n", timestr, msg);
  fclose(file);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<log_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6d(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <log_msg+0x70>
mov    0x2e2b(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <log_msg+0x119>
movl   $0x0,-0x1c(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <time@plt>
lea    -0x1c(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x28,%esi
lea    0xe22(%rip),%rdx
call   1080 <strftime@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     126a <log_msg+0xea>
mov    0x2da0(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rdx
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_msg>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%r14
lea    0xe6d(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1244 <log_msg+0xc4>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1070 <time@plt>
mov    %r15,%rdi
call   1030 <localtime@plt>
lea    0xe74(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x28,%esi
mov    %rax,%rcx
call   1080 <strftime@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     121a <log_msg+0x9a>
mov    0x2deb(%rip),%rax
movslq (%rax),%r15
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe4b(%rip),%rsi
mov    %r15,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xe6d(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d86(%rip),%rax
jmp    1277 <log_msg+0xf7>
mov    0x2d95(%rip),%rax
movslq (%rax),%rbx
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdae(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [15 x i8] c"log/master.log\00"
@.str.1 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.2 = [50 x i8] c"[ERROR] Failed to open log file. System said: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [21 x i8] c"%Y-%m-%dT%H:%M:%S %Z\00"
@.str.4 = [54 x i8] c"[ERROR] Failed to write to log file. System said: %s\0A\00"
@.str.5 = [7 x i8] c"%s %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @log_msg(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.tm*
%7 = alloca [40 x i8]
store i8* %0, i8** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %19
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
store i32 %18, i32* %2
br label %43
19: ; preds = %1
store i32 0, i32* %5
%20 = call i32 @time(i32* %5)
%21 = call %struct.tm* @localtime(i32* %5)
store %struct.tm* %21, %struct.tm** %6
%22 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%23 = load %struct.tm*, %struct.tm** %6
%24 = call i32 @strftime(i8* %22, i32 40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), %struct.tm* %23)
%25 = load i32*, i32** %4
%26 = call i64 @ferror(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i32*
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %28, %19
%36 = load i32*, i32** %4
%37 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%38 = load i8*, i8** %3
%39 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %37, i8* %38)
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* @EXIT_SUCCESS
store i32 %42, i32* %2
br label %43
43: ; preds = %35, %11
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [15 x i8] c"log/master.log\00"
@.str.1 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.2 = [50 x i8] c"[ERROR] Failed to open log file. System said: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [21 x i8] c"%Y-%m-%dT%H:%M:%S %Z\00"
@.str.4 = [54 x i8] c"[ERROR] Failed to write to log file. System said: %s\0A\00"
@.str.5 = [7 x i8] c"%s %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @log_msg(i8* %0) {
%2 = alloca i32
%3 = alloca [40 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
br label %33
14: ; preds = %1
%15 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%16 = call i32 @time(i32* %2)
%17 = call %struct.tm* @localtime(i32* %2)
%18 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%19 = call i32 @strftime(i8* %18, i32 40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), %struct.tm* %17)
%20 = call i64 @ferror(i32* %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i32*
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %22, %14
%30 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %18, i8* %0)
%31 = call i32 @fclose(i32* %4)
%32 = load i32, i32* @EXIT_SUCCESS
br label %33
33: ; preds = %29, %6
%34 = phi i32 [ %13, %6 ], [ %32, %29 ]
ret i32 %34
}