#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 tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct stat {
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int FILENAME_MAX;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
FILE *file_logger_init(char *filename) {
  FILE *fd;
  time_t t = time(NULL);
  struct tm tm = *localtime(&t);
  char formatted_filename[FILENAME_MAX];
  char file_path[FILENAME_MAX];
  struct stat st = {0};
  const char *filter_path_base;
  filter_path_base = getenv("FILE_PATH");
  sprintf(file_path, "%s/logs", filter_path_base);
  if (stat(file_path, &st) == -1) {
    mkdir(file_path, 0700);
  }
  if (filename) {
    sprintf(formatted_filename, "%s/%s_%d_%d_%d.%d:%d:%d.txt", file_path,
            filename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
            tm.tm_min, tm.tm_sec);
  } else {
    sprintf(formatted_filename, "%s/%d_%d_%d.%d:%d:%d.txt", file_path,
            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
            tm.tm_sec);
  }
  fd = fopen(formatted_filename, "w");
  if (fd == NULL) {
    fprintf(stderr, "Failed to open file '%s'.  Does directory exist?\n",
            formatted_filename);
  }
  return fd;
}


x86 assembly (clang, -O0):
<file_logger_init>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x78,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <localtime@plt>
mov    0x10(%rax),%rcx
mov    %rcx,-0x30(%rbp)
movups (%rax),%xmm0
movaps %xmm0,-0x40(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x78(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x50(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xdde(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x70(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1265 <file_logger_init+0xd5>
mov    -0x70(%rbp),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
cmpq   $0x0,-0x10(%rbp)
je     12c9 <file_logger_init+0x139>
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%r8d
add    $0x76c,%r8d
mov    -0x3c(%rbp),%r9d
add    $0x1,%r9d
mov    -0x38(%rbp),%ebx
mov    -0x34(%rbp),%r11d
mov    -0x30(%rbp),%r10d
mov    -0x2c(%rbp),%eax
lea    0xd6e(%rip),%rsi
sub    $0x20,%rsp
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
mov    $0x0,%al
call   10b0 <sprintf@plt>
add    $0x20,%rsp
jmp    1314 <file_logger_init+0x184>
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rdi
mov    -0x40(%rbp),%ecx
add    $0x76c,%ecx
mov    -0x3c(%rbp),%r8d
add    $0x1,%r8d
mov    -0x38(%rbp),%r9d
mov    -0x34(%rbp),%r11d
mov    -0x30(%rbp),%r10d
mov    -0x2c(%rbp),%eax
lea    0xd36(%rip),%rsi
sub    $0x20,%rsp
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   10b0 <sprintf@plt>
add    $0x20,%rsp
mov    -0x78(%rbp),%rdi
lea    0xd28(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    134c <file_logger_init+0x1bc>
mov    -0x78(%rbp),%rdx
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rcx,%rsp
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_logger_init>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,-0x34(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <localtime@plt>
mov    %rsp,-0x60(%rbp)
mov    (%rax),%r13d
mov    0x4(%rax),%r12d
mov    0x8(%rax),%ecx
mov    %rcx,-0x50(%rbp)
mov    0xc(%rax),%ecx
mov    %rcx,-0x58(%rbp)
mov    0x10(%rax),%ecx
mov    %rcx,-0x48(%rbp)
mov    0x14(%rax),%eax
mov    %rax,-0x40(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%r14
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r14
mov    %r14,%rsp
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
movl   $0x0,-0x30(%rbp)
lea    0xdf7(%rip),%rdi
call   1030 <getenv@plt>
lea    0xdf5(%rip),%rsi
mov    %r15,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    -0x30(%rbp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    1240 <file_logger_init+0xc0>
mov    %r15,%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
add    $0x76c,%r13d
add    $0x1,%r12d
test   %rbx,%rbx
je     127b <file_logger_init+0xfb>
lea    0xdbb(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r13d,%r8d
mov    %r12d,%r9d
xor    %eax,%eax
push   -0x40(%rbp)
push   -0x48(%rbp)
push   -0x58(%rbp)
push   -0x50(%rbp)
call   10a0 <sprintf@plt>
jmp    12a6 <file_logger_init+0x126>
sub    $0x8,%rsp
lea    0xda8(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %r13d,%ecx
mov    %r12d,%r8d
mov    -0x50(%rbp),%r9
xor    %eax,%eax
push   -0x40(%rbp)
push   -0x48(%rbp)
push   -0x58(%rbp)
call   10a0 <sprintf@plt>
add    $0x20,%rsp
lea    0xd96(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12d9 <file_logger_init+0x159>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    -0x60(%rbp),%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
%struct.stat = type { i32 }
@FILENAME_MAX = global i32 0
@.str = [10 x i8] c"FILE_PATH\00"
@.str.1 = [8 x i8] c"%s/logs\00"
@.str.2 = [28 x i8] c"%s/%s_%d_%d_%d.%d:%d:%d.txt\00"
@.str.3 = [25 x i8] c"%s/%d_%d_%d.%d:%d:%d.txt\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [50 x i8] c"Failed to open file '%s'. Does directory exist?\0A\00"
define i32* @file_logger_init(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.tm
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.stat
%10 = alloca i8*
store i8* %0, i8** %2
%11 = call i32 @time(i32* null)
store i32 %11, i32* %4
%12 = call %struct.tm* @localtime(i32* %4)
%13 = bitcast %struct.tm* %5 to i8*
%14 = bitcast %struct.tm* %12 to i8*
%15 = load i32, i32* @FILENAME_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %6
%18 = alloca i8, i64 %16
store i64 %16, i64* %7
%19 = load i32, i32* @FILENAME_MAX
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
store i64 %20, i64* %8
%22 = bitcast %struct.stat* %9 to i8*
%23 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @stat(i8* %21, %struct.stat* %9)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %1
%29 = call i32 @mkdir(i8* %21, i32 448)
br label %30
30: ; preds = %28, %1
%31 = load i8*, i8** %2
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %50
33: ; preds = %30
%34 = load i8*, i8** %2
%35 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 0
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 1900
%38 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 1
%39 = load i32, i32* %38
%40 = add nsw i32 %39, 1
%41 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 2
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 3
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 4
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 5
%48 = load i32, i32* %47
%49 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %34, i32 %37, i32 %40, i32 %42, i32 %44, i32 %46, i32 %48)
br label %66
50: ; preds = %30
%51 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 0
%52 = load i32, i32* %51
%53 = add nsw i32 %52, 1900
%54 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 1
%55 = load i32, i32* %54
%56 = add nsw i32 %55, 1
%57 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 2
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 3
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 4
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 5
%64 = load i32, i32* %63
%65 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %21, i32 %53, i32 %56, i32 %58, i32 %60, i32 %62, i32 %64)
br label %66
66: ; preds = %50, %33
%67 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %67, i32** %3
%68 = load i32*, i32** %3
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i8* %18)
br label %73
73: ; preds = %70, %66
%74 = load i32*, i32** %3
%75 = load i8*, i8** %6
ret i32* %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@FILENAME_MAX = global i32 0
@.str = [10 x i8] c"FILE_PATH\00"
@.str.1 = [8 x i8] c"%s/logs\00"
@.str.2 = [28 x i8] c"%s/%s_%d_%d_%d.%d:%d:%d.txt\00"
@.str.3 = [25 x i8] c"%s/%d_%d_%d.%d:%d:%d.txt\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [50 x i8] c"Failed to open file '%s'. Does directory exist?\0A\00"
define noalias i32* @file_logger_init(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @time(i32* null)
store i32 %5, i32* %2
%6 = call %struct.tm* @localtime(i32* %2)
%7 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 2
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 3
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 4
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 5
%18 = load i32, i32* %17
%19 = load i32, i32* @FILENAME_MAX
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
%22 = alloca i8, i64 %20
%23 = load i32, i32* @FILENAME_MAX
%24 = zext i32 %23 to i64
%25 = alloca i8, i64 %24
%26 = bitcast %struct.stat* %3 to i8*
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
store i32 0, i32* %27
%28 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%29 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @stat(i8* %25, %struct.stat* %3)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %1
%33 = call i32 @mkdir(i8* %25, i32 448)
br label %34
34: ; preds = %32, %1
%35 = icmp eq i8* %0, null
%36 = add nsw i32 %8, 1900
%37 = add nsw i32 %10, 1
br i1 %35, label %40, label %38
38: ; preds = %34
%39 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %0, i32 %36, i32 %37, i32 %12, i32 %14, i32 %16, i32 %18)
br label %42
40: ; preds = %34
%41 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %25, i32 %36, i32 %37, i32 %12, i32 %14, i32 %16, i32 %18)
br label %42
42: ; preds = %40, %38
%43 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i8* %22)
br label %48
48: ; preds = %45, %42
ret i32* %43
}