#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, 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 lfread, int lfrcvd,
               int laf) {
  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 %04d %s", i, lfread, lfrcvd, laf,
          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    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0x104(%rbp)
lea    -0x104(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x110(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jae    124e <log_write+0x8e>
mov    -0x8(%rbp),%rsi
lea    0xdc3(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x100(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x128(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x124(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x120(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x110(%rbp),%rdi
call   1060 <asctime@plt>
mov    -0x130(%rbp),%rdi
mov    -0x128(%rbp),%edx
mov    -0x124(%rbp),%ecx
mov    -0x120(%rbp),%r8d
mov    -0x11c(%rbp),%r9d
lea    0xd69(%rip),%rsi
mov    %rax,(%rsp)
call   10c0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x118(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    0xd4c(%rip),%rsi
call   1080 <fprintf@plt>
cmp    $0x0,%rax
jne    1311 <log_write+0x151>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd28(%rip),%rdi
call   1070 <printf@plt>
mov    -0x118(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
mov    %r9d,0x18(%rsp)
mov    %r8d,0x14(%rsp)
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %rdi,%rbx
lea    0x20(%rsp),%rdi
call   1040 <strcpy@plt>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0x1c(%rsp)
lea    0x1c(%rsp),%rdi
call   1030 <localtime@plt>
mov    %rax,%r14
lea    0xe03(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1060 <asctime@plt>
mov    %rax,(%rsp)
lea    0xde7(%rip),%rsi
lea    0x90(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15d,%edx
mov    %r13d,%ecx
mov    0x14(%rsp),%r8d
mov    0x18(%rsp),%r9d
call   10c0 <sprintf@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10b0 <strcat@plt>
lea    0xdc4(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
test   %rax,%rax
jne    127f <log_write+0xcf>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xda2(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
add    $0xf8,%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 = [23 x i8] c"%04d %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, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [100 x i8]
%14 = alloca [100 x i8]
%15 = alloca i32
%16 = alloca %struct.tm*
%17 = alloca i32*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%19 = load i8*, i8** %8
%20 = call i32 @strcpy(i8* %18, i8* %19)
%21 = call i32 @time(i32* null)
store i32 %21, i32* %15
%22 = call %struct.tm* @localtime(i32* %15)
store %struct.tm* %22, %struct.tm** %16
%23 = load i8*, i8** %7
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %17
%25 = icmp ult i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %6
%27 = load i8*, i8** %7
%28 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %6
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%32 = load i32, i32* %9
%33 = load i32, i32* %10
%34 = load i32, i32* %11
%35 = load i32, i32* %12
%36 = load %struct.tm*, %struct.tm** %16
%37 = call i8* @asctime(%struct.tm* %36)
%38 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33, i32 %34, i32 %35, i8* %37)
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%41 = call i32 @strcat(i8* %39, i8* %40)
%42 = load i32*, i32** %17
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%44 = call i64 @fprintf(i32* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %30
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %46, %30
%51 = load i32*, i32** %17
%52 = call i32 @fclose(i32* %51)
ret void
}

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