#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 */
char *asctime(struct tm *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, char *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void log_write(char *file, char *logmsg, int i, int lfs, int lar) {
  char full_logmsg[100], loginfo[100];
  strcpy(full_logmsg, logmsg);
  time_t curtime;
  struct tm *loctime;
  curtime = time(NULL);
  loctime = localtime(&curtime);
  FILE *lfp;
  if ((lfp = fopen(file, "a")) < 0) {
    printf("unable to open log file %s", file);
    exit(1);
  }
  sprintf(loginfo, "%04d %04d %04d %s", i, lfs, lar, asctime(loctime));
  strcat(full_logmsg, loginfo);
  if (fprintf(lfp, "%s", full_logmsg) == 0) {
    printf("error writing packet into file%s\n", strerror(errno));
  }
  fclose(lfp);
}


x86 assembly (clang, -O0):
<log_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0xf4(%rbp)
lea    -0xf4(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x100(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x108(%rbp)
cmp    $0x0,%rax
jae    1247 <log_write+0x87>
mov    -0x8(%rbp),%rsi
lea    0xdca(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0xf0(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x114(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x110(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10c(%rbp)
mov    -0x100(%rbp),%rdi
call   1060 <asctime@plt>
mov    -0x120(%rbp),%rdi
mov    -0x114(%rbp),%edx
mov    -0x110(%rbp),%ecx
mov    -0x10c(%rbp),%r8d
mov    %rax,%r9
lea    0xd7d(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    -0xf0(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x108(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xd65(%rip),%rsi
call   1080 <fprintf@plt>
cmp    $0x0,%rax
jne    12f3 <log_write+0x133>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd41(%rip),%rdi
call   1070 <printf@plt>
mov    -0x108(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
mov    %r8d,%r15d
mov    %ecx,%r12d
mov    %edx,%r13d
mov    %rdi,%rbx
lea    0x80(%rsp),%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
mov    %rax,%rbp
lea    0xe04(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <asctime@plt>
lea    0xdec(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r13d,%edx
mov    %r12d,%ecx
mov    %r15d,%r8d
mov    %rax,%r9
call   10c0 <sprintf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   10b0 <strcat@plt>
lea    0xdd0(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fprintf@plt>
test   %rax,%rax
jne    126e <log_write+0xbe>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdae(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [2 x i8] c"a\00"
@.str.1 = [27 x i8] c"unable to open log file %s\00"
@.str.2 = [18 x i8] c"%04d %04d %04d %s\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [34 x i8] c"error writing packet into file%s\0A\00"
@errno = global i32 0
define void @log_write(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [100 x i8]
%12 = alloca [100 x i8]
%13 = alloca i32
%14 = alloca %struct.tm*
%15 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%17 = load i8*, i8** %7
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = call i32 @time(i32* null)
store i32 %19, i32* %13
%20 = call %struct.tm* @localtime(i32* %13)
store %struct.tm* %20, %struct.tm** %14
%21 = load i8*, i8** %6
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %15
%23 = icmp ult i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %5
%25 = load i8*, i8** %6
%26 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %5
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = load i32, i32* %10
%33 = load %struct.tm*, %struct.tm** %14
%34 = call i8* @asctime(%struct.tm* %33)
%35 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %31, i32 %32, i8* %34)
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%38 = call i32 @strcat(i8* %36, i8* %37)
%39 = load i32*, i32** %15
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%41 = call i64 @fprintf(i32* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %28
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %43, %28
%48 = load i32*, i32** %15
%49 = call i32 @fclose(i32* %48)
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [2 x i8] c"a\00"
@.str.2 = [18 x i8] c"%04d %04d %04d %s\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [34 x i8] c"error writing packet into file%s\0A\00"
@errno = global i32 0
define void @log_write(i8* nocapture %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca [100 x i8]
%7 = alloca [100 x i8]
%8 = alloca i32
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%11 = call i32 @strcpy(i8* %9, i8* %1)
%12 = bitcast i32* %8 to i8*
%13 = call i32 @time(i32* null)
store i32 %13, i32* %8
%14 = call %struct.tm* @localtime(i32* %8)
%15 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = call i8* @asctime(%struct.tm* %14)
%17 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %3, i32 %4, i8* %16)
%18 = call i32 @strcat(i8* %9, i8* %10)
%19 = call i64 @fprintf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %5
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %5
%26 = call i32 @fclose(i32* %15)
ret void
}