#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_mday;
  int tm_mon;
  int tm_year;
  int tm_hour;
  int /*<<< orphan*/ tm_sec;
  int /*<<< orphan*/ tm_min;
};

/* Variables and functions */
int FILENAME_MAX;
int /*<<< orphan*/ LOG_NOTICE;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int, int, int, int,
                            int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
scalar_t__ write(int, void *, size_t);
void log2file(char *fname, void *content, size_t content_size) {
  int fHandle;
  char fName[FILENAME_MAX];
  time_t t;
  struct tm local;
  if (content && content_size) {
    t = time(NULL);
    localtime_r(&t, &local);
    memset(fName, 0, FILENAME_MAX);
    snprintf(fName, FILENAME_MAX, "%s_%02d%02d%02d_%02d%02d%02d", fname,
             local.tm_mday, local.tm_mon + 1, local.tm_year - 100,
             local.tm_hour, local.tm_min, local.tm_sec);
    if ((fHandle = open(fName, O_APPEND | O_CREAT | O_WRONLY, 0644)) != -1) {
      if (write(fHandle, content, content_size) < 0) {
        syslog(LOG_NOTICE, "%s write error %d: %s\n", fName, errno,
               strerror(errno));
      }
      close(fHandle);
    }
  }
}


x86 assembly (clang, -O0):
<log2file>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x68,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1302 <log2file+0x172>
cmpq   $0x0,-0x20(%rbp)
je     1302 <log2file+0x172>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x3c(%rbp)
lea    -0x3c(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x60(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x60(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x58(%rbp),%r8d
mov    -0x54(%rbp),%r9d
add    $0x1,%r9d
mov    -0x50(%rbp),%ebx
sub    $0x64,%ebx
mov    -0x4c(%rbp),%r11d
mov    -0x44(%rbp),%r10d
mov    -0x48(%rbp),%eax
lea    0xdb7(%rip),%rdx
sub    $0x20,%rsp
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rdi
add    $0x20,%rsp
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    0x2d34(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     12fd <log2file+0x16d>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12f5 <log2file+0x165>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x64(%rbp),%ecx
mov    %eax,%r8d
lea    0xd2d(%rip),%rsi
call   1090 <syslog@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
jmp    1302 <log2file+0x172>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<log2file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e38(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rsi,%rsi
je     12ba <log2file+0x12a>
mov    %rdx,%r14
test   %rdx,%rdx
je     12ba <log2file+0x12a>
mov    %rsi,%r15
mov    %rdi,%r12
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x0(%r13),%esi
mov    -0x48(%rbp),%r8d
mov    -0x44(%rbp),%r9d
add    $0x1,%r9d
mov    -0x40(%rbp),%eax
add    $0xffffff9c,%eax
mov    -0x3c(%rbp),%r10d
mov    -0x34(%rbp),%r11d
mov    -0x38(%rbp),%r13d
lea    0xdda(%rip),%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
push   %r13
push   %r11
push   %r10
push   %rax
call   1050 <snprintf@plt>
add    $0x20,%rsp
mov    0x2d6d(%rip),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d63(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ba <log2file+0x12a>
mov    %eax,%r12d
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
test   %rax,%rax
jns    12b2 <log2file+0x122>
mov    0x2d43(%rip),%rax
mov    (%rax),%r14d
mov    0x2d41(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%edi
call   10b0 <strerror@plt>
lea    0xd7c(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %eax,%r8d
call   1090 <syslog@plt>
mov    %r12d,%edi
call   1070 <close@plt>
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 }
@FILENAME_MAX = global i32 0
@.str = [29 x i8] c"%s_%02d%02d%02d_%02d%02d%02d\00"
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [23 x i8] c"%s write error %d: %s\0A\00"
@errno = global i32 0
define void @log2file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.tm
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%12 = load i32, i32* @FILENAME_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %66
18: ; preds = %3
%19 = load i64, i64* %6
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %66
21: ; preds = %18
%22 = call i32 @time(i32* null)
store i32 %22, i32* %10
%23 = call i32 @localtime_r(i32* %10, %struct.tm* %11)
%24 = load i32, i32* @FILENAME_MAX
%25 = call i32 @memset(i8* %15, i32 0, i32 %24)
%26 = load i32, i32* @FILENAME_MAX
%27 = load i8*, i8** %4
%28 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 1
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
%33 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 2
%34 = load i32, i32* %33
%35 = sub nsw i32 %34, 100
%36 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 3
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 5
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 4
%41 = load i32, i32* %40
%42 = call i32 @snprintf(i8* %15, i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %27, i32 %29, i32 %32, i32 %35, i32 %37, i32 %39, i32 %41)
%43 = load i32, i32* @O_APPEND
%44 = load i32, i32* @O_CREAT
%45 = or i32 %43, %44
%46 = load i32, i32* @O_WRONLY
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %15, i32 %47, i32 420)
store i32 %48, i32* %7
%49 = icmp ne i32 %48, -1
br i1 %49, label %50, label %65
50: ; preds = %21
%51 = load i32, i32* %7
%52 = load i8*, i8** %5
%53 = load i64, i64* %6
%54 = call i64 @write(i32 %51, i8* %52, i64 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i32, i32* @LOG_NOTICE
%58 = load i32, i32* @errno
%59 = load i32, i32* @errno
%60 = call i32 @strerror(i32 %59)
%61 = call i32 @syslog(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %58, i32 %60)
br label %62
62: ; preds = %56, %50
%63 = load i32, i32* %7
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %21
br label %66
66: ; preds = %65, %18, %3
%67 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@FILENAME_MAX = global i32 0
@.str = [29 x i8] c"%s_%02d%02d%02d_%02d%02d%02d\00"
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [23 x i8] c"%s write error %d: %s\0A\00"
@errno = global i32 0
define void @log2file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.tm
%6 = load i32, i32* @FILENAME_MAX
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast i32* %4 to i8*
%10 = bitcast %struct.tm* %5 to i8*
%11 = icmp ne i8* %1, null
%12 = icmp ne i64 %2, 0
%13 = and i1 %11, %12
br i1 %13, label %14, label %52
14: ; preds = %3
%15 = tail call i32 @time(i32* null)
store i32 %15, i32* %4
%16 = call i32 @localtime_r(i32* %4, %struct.tm* %5)
%17 = load i32, i32* @FILENAME_MAX
%18 = call i32 @memset(i8* %8, i32 0, i32 %17)
%19 = load i32, i32* @FILENAME_MAX
%20 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 1
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
%25 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 2
%26 = load i32, i32* %25
%27 = add nsw i32 %26, -100
%28 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 5
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 4
%33 = load i32, i32* %32
%34 = call i32 @snprintf(i8* %8, i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %21, i32 %24, i32 %27, i32 %29, i32 %31, i32 %33)
%35 = load i32, i32* @O_APPEND
%36 = load i32, i32* @O_CREAT
%37 = or i32 %36, %35
%38 = load i32, i32* @O_WRONLY
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %8, i32 %39, i32 420)
%41 = icmp eq i32 %40, -1
br i1 %41, label %52, label %42
42: ; preds = %14
%43 = call i64 @write(i32 %40, i8* %1, i64 %2)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i32, i32* @LOG_NOTICE
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 @syslog(i32 %46, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 %47, i32 %48)
br label %50
50: ; preds = %45, %42
%51 = call i32 @close(i32 %40)
br label %52
52: ; preds = %14, %50, %3
ret void
}