#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void logmessage(const char logfile[200], const char message[200], int loglev,
                int msglev) {
  time_t now;
  char tstr[25];
  struct tm *tm_info;
  FILE *log;
  time(&now);
  tm_info = localtime(&now);
  strftime(tstr, 25, "%Y-%m-%d %H:%M:%S", tm_info);
  if (msglev >= loglev) {
    log = fopen(logfile, "a");
    if (NULL == log) {
      perror("could not open log file");
    } else {
      fprintf(log, "%s ", tstr);
      fprintf(log, "%s\n", message);
      fclose(log);
    }
  }
}


x86 assembly (clang, -O0):
<logmessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1060 <time@plt>
lea    -0x1c(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    $0x19,%esi
lea    0xe50(%rip),%rdx
call   1070 <strftime@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1228 <logmessage+0xb8>
mov    -0x8(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x50(%rbp)
xor    %eax,%eax
cmp    -0x50(%rbp),%rax
jne    11f2 <logmessage+0x82>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
jmp    1223 <logmessage+0xb3>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe2b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe1b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1228 <logmessage+0xb8>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logmessage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <time@plt>
mov    %rbp,%rdi
call   1030 <localtime@plt>
lea    0xe5d(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x19,%esi
mov    %rax,%rcx
call   1070 <strftime@plt>
cmp    %ebx,%r12d
jl     11ff <logmessage+0x8f>
lea    0xe51(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     120c <logmessage+0x9c>
mov    %rax,%rbx
lea    0xe54(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fprintf@plt>
lea    0xe44(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
jmp    11ff <logmessage+0x8f>

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [24 x i8] c"could not open log file\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @logmessage(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [25 x i8]
%11 = alloca %struct.tm*
%12 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = call i32 @time(i32* %9)
%14 = call %struct.tm* @localtime(i32* %9)
store %struct.tm* %14, %struct.tm** %11
%15 = getelementptr inbounds [25 x i8], [25 x i8]* %10, i64 0, i64 0
%16 = load %struct.tm*, %struct.tm** %11
%17 = call i32 @strftime(i8* %15, i32 25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %16)
%18 = load i32, i32* %8
%19 = load i32, i32* %7
%20 = icmp sge i32 %18, %19
br i1 %20, label %21, label %38
21: ; preds = %4
%22 = load i8*, i8** %5
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %12
%24 = load i32*, i32** %12
%25 = icmp eq i32* null, %24
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %37
28: ; preds = %21
%29 = load i32*, i32** %12
%30 = getelementptr inbounds [25 x i8], [25 x i8]* %10, i64 0, i64 0
%31 = call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i32*, i32** %12
%33 = load i8*, i8** %6
%34 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %33)
%35 = load i32*, i32** %12
%36 = call i32 @fclose(i32* %35)
br label %37
37: ; preds = %28, %26
br label %38
38: ; preds = %37, %4
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [24 x i8] c"could not open log file\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @logmessage(i8* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca [25 x i8]
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%9 = call i32 @time(i32* %5)
%10 = call %struct.tm* @localtime(i32* %5)
%11 = call i32 @strftime(i8* %8, i32 25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %10)
%12 = icmp slt i32 %3, %2
br i1 %12, label %22, label %13
13: ; preds = %4
%14 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %22
18: ; preds = %13
%19 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%20 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%21 = call i32 @fclose(i32* %14)
br label %22
22: ; preds = %16, %18, %4
ret void
}