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

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_SYNC;
int O_WRONLY;
int STDERR_FILENO;
int STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ dup2(int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void openLogFile(char *logFilePath) {
  int fd = open(logFilePath, O_WRONLY | O_APPEND | O_CREAT | O_SYNC,
                S_IRUSR | S_IWUSR);
  if (fd < 0) {
    perror("open()");
    return;
  }
  if (fd != STDOUT_FILENO)
    dup2(fd, STDOUT_FILENO);
  if (fd != STDERR_FILENO)
    dup2(fd, STDERR_FILENO);
  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
}


x86 assembly (clang, -O0):
<openLogFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a9 <openLogFile+0x69>
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
jmp    1213 <openLogFile+0xd3>
mov    -0xc(%rbp),%eax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%eax
je     11cc <openLogFile+0x8c>
mov    -0xc(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%eax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%eax
je     11ef <openLogFile+0xaf>
mov    -0xc(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openLogFile>:
push   %rbx
mov    0x2e50(%rip),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e9e(%rip),%rax
or     (%rax),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%esi
mov    0x2e44(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <open@plt>
test   %eax,%eax
js     11cb <openLogFile+0x8b>
mov    %eax,%ebx
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
cmp    %esi,%ebx
je     1196 <openLogFile+0x56>
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
cmp    %esi,%ebx
je     11aa <openLogFile+0x6a>
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <setbuf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
pop    %rbx
jmp    1030 <setbuf@plt>
lea    0xe2e(%rip),%rdi
pop    %rbx
jmp    1060 <perror@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_SYNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [7 x i8] c"open()\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @openLogFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_APPEND
%7 = or i32 %5, %6
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_SYNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %4, i32 %11, i32 %14)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %41
20: ; preds = %1
%21 = load i32, i32* %3
%22 = load i32, i32* @STDOUT_FILENO
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* %3
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @dup2(i32 %25, i32 %26)
br label %28
28: ; preds = %24, %20
%29 = load i32, i32* %3
%30 = load i32, i32* @STDERR_FILENO
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* %3
%34 = load i32, i32* @STDERR_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
br label %36
36: ; preds = %32, %28
%37 = load i32, i32* @stdout
%38 = call i32 @setbuf(i32 %37, i32* null)
%39 = load i32, i32* @stderr
%40 = call i32 @setbuf(i32 %39, i32* null)
br label %41
41: ; preds = %36, %18
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_SYNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [7 x i8] c"open()\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @openLogFile(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_APPEND
%4 = or i32 %3, %2
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @O_SYNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %8, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
16: ; preds = %1
%17 = load i32, i32* @STDOUT_FILENO
%18 = icmp eq i32 %12, %17
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @dup2(i32 %12, i32 %17)
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* @STDERR_FILENO
%23 = icmp eq i32 %12, %22
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = tail call i32 @dup2(i32 %12, i32 %22)
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* @stdout
%28 = tail call i32 @setbuf(i32 %27, i32* null)
%29 = load i32, i32* @stderr
%30 = tail call i32 @setbuf(i32 %29, i32* null)
br label %31
31: ; preds = %26, %14
ret void
}