#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int strlen(char *);
int /*<<< orphan*/ writeLog(int, char *);
void logFile(char *msg, int size) {
  int ret = 0;
  char *name = "./server.log";
  FILE *fp = NULL;
  if (!(fp = fopen(name, "a+"))) {
    writeLog(1, "open log file error!");
    exit(-1);
  }
  ret = fwrite(msg, size > 0 ? size : strlen(msg), 1, fp);
  ret = fwrite("\n", 1, 1, fp);
  fflush(fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<logFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d2 <logFile+0x62>
mov    $0x1,%edi
lea    0xe4d(%rip),%rsi
call   1050 <writeLog@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11ef <logFile+0x7f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11fb <logFile+0x8b>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
lea    0xe07(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logFile>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe70(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e5 <logFile+0x65>
mov    %rax,%rbx
test   %ebp,%ebp
jg     11b2 <logFile+0x32>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe24(%rip),%rsi
mov    $0x1,%edi
call   1050 <writeLog@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"./server.log\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [21 x i8] c"open log file error!\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @logFile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %6
store i32* null, i32** %7
%8 = load i8*, i8** %6
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %7
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32 @writeLog(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %4
br label %23
20: ; preds = %14
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
br label %23
23: ; preds = %20, %18
%24 = phi i32 [ %19, %18 ], [ %22, %20 ]
%25 = load i32*, i32** %7
%26 = call i32 @fwrite(i8* %15, i32 %24, i32 1, i32* %25)
store i32 %26, i32* %5
%27 = load i32*, i32** %7
%28 = call i32 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 1, i32* %27)
store i32 %28, i32* %5
%29 = load i32*, i32** %7
%30 = call i32 @fflush(i32* %29)
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"./server.log\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [21 x i8] c"open log file error!\00"
define void @logFile(i8* %0, i32 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @writeLog(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = icmp sgt i32 %1, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @strlen(i8* %0)
br label %12
12: ; preds = %8, %10
%13 = phi i32 [ %11, %10 ], [ %1, %8 ]
%14 = tail call i32 @fwrite(i8* %0, i32 %13, i32 1, i32* %3)
%15 = tail call i32 @fputc(i32 10, i32* %3)
%16 = tail call i32 @fflush(i32* %3)
%17 = tail call i32 @fclose(i32* %3)
ret void
}