#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_FAILURE;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(char *, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  FILE *file = fopen("server.txt", "a");
  if (file == NULL) {
    fprintf(stderr, "Open/Create server log file failed: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  char buf[] = "hello, world";
  fwrite(buf, strlen(buf), sizeof(char), file);
  fclose(file);
  int log_fd = open("server.log", O_WRONLY | O_CREAT | O_APPEND, S_IRWXU);
  if (log_fd == -1) {
    fprintf(stderr, "Open/Create server log file failed: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  char log_buf[] = "hello, world";
  write(log_fd, log_buf, strlen(log_buf));
  close(log_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120b <main+0x6b>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xe15(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0xe23(%rip),%rax
mov    %rax,-0x1d(%rbp)
mov    0xe21(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xe1c(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x1d(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x1d(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    0x2d92(%rip),%rax
or     (%rax),%esi
mov    0x2d39(%rip),%rax
or     (%rax),%esi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
lea    0xdc1(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12cd <main+0x12d>
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd53(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0xd61(%rip),%rax
mov    %rax,-0x31(%rbp)
mov    0xd5f(%rip),%eax
mov    %eax,-0x29(%rbp)
mov    0xd5a(%rip),%al
mov    %al,-0x25(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x31(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x31(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe4e(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     127e <main+0xde>
mov    %rax,%rbx
movabs $0x646c726f77202c,%r15
mov    %r15,0x1d(%rsp)
movabs $0x77202c6f6c6c6568,%r12
2c 20 77
mov    %r12,0x18(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2d9a(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    0xdfb(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     127e <main+0xde>
mov    %eax,%ebx
mov    %r15,0xd(%rsp)
mov    %r12,0x8(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d4b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd71(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"server.txt\00"
@.str.1 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.2 = [40 x i8] c"Open/Create server log file failed: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@__const.main.buf = [13 x i8] c"hello, world\00"
@.str.3 = [11 x i8] c"server.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRWXU = global i32 0
@__const.main.log_buf = [13 x i8] c"hello, world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [13 x i8]
%4 = alloca i32
%5 = alloca [13 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %16
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = bitcast [13 x i8]* %3 to i8*
%18 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%19 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
%21 = load i32*, i32** %2
%22 = call i32 @fwrite(i8* %18, i32 %20, i32 1, i32* %21)
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = load i32, i32* @O_APPEND
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRWXU
%31 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %16
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %16
%42 = bitcast [13 x i8]* %5 to i8*
%43 = load i32, i32* %4
%44 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%45 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"server.txt\00"
@.str.1 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.2 = [40 x i8] c"Open/Create server log file failed: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [11 x i8] c"server.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRWXU = global i32 0
@__const.main.log_buf = [13 x i8] c"hello, world\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = alloca [13 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %12
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = call i32 @fwrite(i8* %13, i32 %14, i32 1, i32* %3)
%16 = call i32 @fclose(i32* %3)
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = load i32, i32* @O_APPEND
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRWXU
%23 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %12
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %12
%33 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %23, i8* %33, i32 %34)
%36 = call i32 @close(i32 %23)
ret i32 0
}