#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 tm_min;
  int tm_sec;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ SYS_gettid;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int snprintf(char *, int, char *, int, int, int, int, int, int, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int write(int, char *, int);
void writelog(int fHandle, char *logmsg, int len) {
  char buf[512];
  int loglen = 0;
  time_t t;
  struct tm local;
  if (logmsg && len > 0) {
    t = time(NULL);
    localtime_r(&t, &local);
    memset(buf, 0, 512);
    if (logmsg[len - 1] == 10) {
      loglen = snprintf(buf, 512, "%02d.%02d.%02d %02d:%02d:%02d %s",
                        local.tm_mday, local.tm_mon + 1, local.tm_year - 100,
                        local.tm_hour, local.tm_min, local.tm_sec, logmsg);
    } else {
      loglen = snprintf(buf, 512, "%02d.%02d.%02d %02d:%02d:%02d %s\n",
                        local.tm_mday, local.tm_mon + 1, local.tm_year - 100,
                        local.tm_hour, local.tm_min, local.tm_sec, logmsg);
    }
  }
  if (loglen > 0) {
    if (fHandle != (-1)) {
      if (write(fHandle, buf, loglen) < 1) {
        syslog(LOG_NOTICE, "logger[%ld]: write(%d) error %d: %s\n",
               syscall(SYS_gettid), loglen, errno, strerror(errno));
        syslog(LOG_NOTICE, "%s", buf);
      }
    } else {
      syslog(LOG_NOTICE, "%s", buf);
    }
  }
}


x86 assembly (clang, -O0):
<writelog>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x278,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x224(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d6 <writelog+0x156>
cmpl   $0x0,-0x1c(%rbp)
jle    12d6 <writelog+0x156>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x228(%rbp)
lea    -0x228(%rbp),%rdi
lea    -0x240(%rbp),%rsi
call   1030 <localtime_r@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    126d <writelog+0xed>
lea    -0x220(%rbp),%rdi
mov    -0x240(%rbp),%ecx
mov    -0x23c(%rbp),%r8d
add    $0x1,%r8d
mov    -0x238(%rbp),%r9d
sub    $0x64,%r9d
mov    -0x234(%rbp),%ebx
mov    -0x230(%rbp),%r11d
mov    -0x22c(%rbp),%r10d
mov    -0x18(%rbp),%rax
mov    $0x200,%esi
lea    0xdb5(%rip),%rdx
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   1050 <snprintf@plt>
mov    %eax,-0x224(%rbp)
jmp    12d1 <writelog+0x151>
lea    -0x220(%rbp),%rdi
mov    -0x240(%rbp),%ecx
mov    -0x23c(%rbp),%r8d
add    $0x1,%r8d
mov    -0x238(%rbp),%r9d
sub    $0x64,%r9d
mov    -0x234(%rbp),%ebx
mov    -0x230(%rbp),%r11d
mov    -0x22c(%rbp),%r10d
mov    -0x18(%rbp),%rax
mov    $0x200,%esi
lea    0xd6d(%rip),%rdx
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   1050 <snprintf@plt>
mov    %eax,-0x224(%rbp)
jmp    12d6 <writelog+0x156>
cmpl   $0x0,-0x224(%rbp)
jle    13cb <writelog+0x24b>
cmpl   $0xffffffff,-0xc(%rbp)
je     13a8 <writelog+0x228>
mov    -0xc(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jge    13a3 <writelog+0x223>
mov    0x2cbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x254(%rbp)
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1070 <syscall@plt>
cltq
mov    %rax,-0x250(%rbp)
mov    -0x224(%rbp),%eax
mov    %eax,-0x248(%rbp)
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x244(%rbp)
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rdx
mov    -0x248(%rbp),%ecx
mov    -0x244(%rbp),%r8d
mov    %eax,%r9d
lea    0xcc5(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    0x2c44(%rip),%rax
mov    (%rax),%edi
lea    -0x220(%rbp),%rdx
lea    0xc82(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
jmp    13c6 <writelog+0x246>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
lea    -0x220(%rbp),%rdx
lea    0xc5f(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
jmp    13cb <writelog+0x24b>
add    $0x278,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<writelog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x228,%rsp
test   %rsi,%rsi
je     12a8 <writelog+0x128>
mov    %edx,%ebp
test   %edx,%edx
jle    12a8 <writelog+0x128>
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <localtime_r@plt>
lea    0x20(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
add    $0xffffffff,%ebp
mov    0xc(%rsp),%r8d
add    $0x1,%r8d
mov    0x10(%rsp),%r9d
add    $0xffffff9c,%r9d
cmpb   $0xa,(%rbx,%rbp,1)
mov    0x8(%rsp),%ecx
mov    0x14(%rsp),%r10d
mov    0x18(%rsp),%ebp
mov    0x1c(%rsp),%eax
lea    0xdfb(%rip),%rsi
lea    0xe15(%rip),%rdx
cmove  %rsi,%rdx
mov    %r15,%rdi
mov    $0x200,%esi
push   %rbx
push   %rax
push   %rbp
push   %r10
call   1050 <snprintf@plt>
add    $0x20,%rsp
test   %eax,%eax
jle    12a8 <writelog+0x128>
cmp    $0xffffffff,%r14d
je     128c <writelog+0x10c>
mov    %eax,%ebx
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
jg     12a8 <writelog+0x128>
mov    0x2d84(%rip),%rax
mov    (%rax),%r14d
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1070 <syscall@plt>
movslq %eax,%r15
mov    0x2d71(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xdcc(%rip),%rsi
mov    %r14d,%edi
mov    %r15,%rdx
mov    %ebx,%ecx
mov    %ebp,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1090 <syslog@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
lea    0x20(%rsp),%rdx
xor    %eax,%eax
call   1090 <syslog@plt>
add    $0x228,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [33 x i8] c"%02d.%02d.%02d %02d:%02d:%02d %s\00"
@.str.1 = [34 x i8] c"%02d.%02d.%02d %02d:%02d:%02d %s\0A\00"
@LOG_NOTICE = global i32 0
@.str.2 = [37 x i8] c"logger[%ld]: write(%d) error %d: %s\0A\00"
@SYS_gettid = global i32 0
@errno = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define void @writelog(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.tm
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %66
13: ; preds = %3
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %66
16: ; preds = %13
%17 = call i32 @time(i32* null)
store i32 %17, i32* %9
%18 = call i32 @localtime_r(i32* %9, %struct.tm* %10)
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 512)
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %47
29: ; preds = %16
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%31 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 1
%34 = load i32, i32* %33
%35 = add nsw i32 %34, 1
%36 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 2
%37 = load i32, i32* %36
%38 = sub nsw i32 %37, 100
%39 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 3
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 4
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 5
%44 = load i32, i32* %43
%45 = load i8*, i8** %5
%46 = call i32 @snprintf(i8* %30, i32 512, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %35, i32 %38, i32 %40, i32 %42, i32 %44, i8* %45)
store i32 %46, i32* %8
br label %65
47: ; preds = %16
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%49 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 1
%52 = load i32, i32* %51
%53 = add nsw i32 %52, 1
%54 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 2
%55 = load i32, i32* %54
%56 = sub nsw i32 %55, 100
%57 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 3
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 4
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 5
%62 = load i32, i32* %61
%63 = load i8*, i8** %5
%64 = call i32 @snprintf(i8* %48, i32 512, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %50, i32 %53, i32 %56, i32 %58, i32 %60, i32 %62, i8* %63)
store i32 %64, i32* %8
br label %65
65: ; preds = %47, %29
br label %66
66: ; preds = %65, %13, %3
%67 = load i32, i32* %8
%68 = icmp sgt i32 %67, 0
br i1 %68, label %69, label %98
69: ; preds = %66
%70 = load i32, i32* %4
%71 = icmp ne i32 %70, -1
br i1 %71, label %72, label %93
72: ; preds = %69
%73 = load i32, i32* %4
%74 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%75 = load i32, i32* %8
%76 = call i32 @write(i32 %73, i8* %74, i32 %75)
%77 = icmp slt i32 %76, 1
br i1 %77, label %78, label %92
78: ; preds = %72
%79 = load i32, i32* @LOG_NOTICE
%80 = load i32, i32* @SYS_gettid
%81 = call i32 @syscall(i32 %80)
%82 = sext i32 %81 to i64
%83 = inttoptr i64 %82 to i8*
%84 = load i32, i32* %8
%85 = load i32, i32* @errno
%86 = load i32, i32* @errno
%87 = call i32 @strerror(i32 %86)
%88 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %79, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %83, i32 %84, i32 %85, i32 %87)
%89 = load i32, i32* @LOG_NOTICE
%90 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%91 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %89, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %90)
br label %92
92: ; preds = %78, %72
br label %97
93: ; preds = %69
%94 = load i32, i32* @LOG_NOTICE
%95 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%96 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %94, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %95)
br label %97
97: ; preds = %93, %92
br label %98
98: ; preds = %97, %66
ret void
}

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