#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 /*<<< orphan*/ LOG_ERR;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int dup2(int, int);
int /*<<< orphan*/ errno;
int fchmod(int, int);
char *getenv(char const *);
int open(char const *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *,
                          char const *, int /*<<< orphan*/);
void SetUpStdFileDescriptor(const char *env_name, int fileNo) {
  const char *path = getenv(env_name);
  if (path) {
    int fd = open(path, O_RDWR | O_CREAT | O_APPEND);
    if (fd == -1) {
      syslog(LOG_ERR, "Could not open %s for %s - %s", env_name, path,
             strerror(errno));
    } else {
      if (fchmod(fd, 0666) == -1) {
        syslog(LOG_ERR, "Could not chmod %s for %s - %s", env_name, path,
               strerror(errno));
      }
      if (dup2(fd, fileNo) == -1) {
        syslog(LOG_ERR, "Could not dup %s for %s - %s", env_name, path,
               strerror(errno));
      }
    }
  }
}


x86 assembly (clang, -O0):
<SetUpStdFileDescriptor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12bd <SetUpStdFileDescriptor+0x15d>
mov    -0x18(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1201 <SetUpStdFileDescriptor+0xa1>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    %eax,%r8d
lea    0xe09(%rip),%rsi
call   1050 <syslog@plt>
jmp    12b8 <SetUpStdFileDescriptor+0x158>
mov    -0x1c(%rbp),%edi
mov    $0x1b6,%esi
call   1060 <fchmod@plt>
cmp    $0xffffffff,%eax
jne    125b <SetUpStdFileDescriptor+0xfb>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
lea    0xdc8(%rip),%rsi
call   1050 <syslog@plt>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12b3 <SetUpStdFileDescriptor+0x153>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    %eax,%r8d
lea    0xd8f(%rip),%rsi
call   1050 <syslog@plt>
jmp    12b8 <SetUpStdFileDescriptor+0x158>
jmp    12bd <SetUpStdFileDescriptor+0x15d>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetUpStdFileDescriptor>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
call   1030 <getenv@plt>
test   %rax,%rax
je     11f5 <SetUpStdFileDescriptor+0x95>
mov    %rax,%rbx
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fe <SetUpStdFileDescriptor+0x9e>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1b6,%esi
call   1060 <fchmod@plt>
cmp    $0xffffffff,%eax
jne    11e6 <SetUpStdFileDescriptor+0x86>
mov    0x2e03(%rip),%rax
mov    (%rax),%r12d
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe49(%rip),%rsi
mov    %r12d,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %eax,%r8d
call   1050 <syslog@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1207 <SetUpStdFileDescriptor+0xa7>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfb(%rip),%r15
jmp    120e <SetUpStdFileDescriptor+0xae>
lea    0xe2f(%rip),%r15
mov    0x2dab(%rip),%rax
mov    (%rax),%ebp
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %eax,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <syslog@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@LOG_ERR = global i32 0
@.str = [30 x i8] c"Could not open %s for %s - %s\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"Could not chmod %s for %s - %s\00"
@.str.2 = [29 x i8] c"Could not dup %s for %s - %s\00"
define void @SetUpStdFileDescriptor(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i8* @getenv(i8* %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %53
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %28
21: ; preds = %11
%22 = load i32, i32* @LOG_ERR
%23 = load i8*, i8** %3
%24 = load i8*, i8** %5
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @syslog(i32 %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %24, i32 %26)
br label %52
28: ; preds = %11
%29 = load i32, i32* %6
%30 = call i32 @fchmod(i32 %29, i32 438)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = load i32, i32* @LOG_ERR
%34 = load i8*, i8** %3
%35 = load i8*, i8** %5
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 @syslog(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %35, i32 %37)
br label %39
39: ; preds = %32, %28
%40 = load i32, i32* %6
%41 = load i32, i32* %4
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = load i32, i32* @LOG_ERR
%46 = load i8*, i8** %3
%47 = load i8*, i8** %5
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 @syslog(i32 %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %47, i32 %49)
br label %51
51: ; preds = %44, %39
br label %52
52: ; preds = %51, %21
br label %53
53: ; preds = %52, %2
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@LOG_ERR = global i32 0
@.str = [30 x i8] c"Could not open %s for %s - %s\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"Could not chmod %s for %s - %s\00"
@.str.2 = [29 x i8] c"Could not dup %s for %s - %s\00"
define void @SetUpStdFileDescriptor(i8* %0, i32 %1) {
%3 = tail call i8* @getenv(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %30, label %5
5: ; preds = %2
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_APPEND
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %3, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %24, label %13
13: ; preds = %5
%14 = tail call i32 @fchmod(i32 %11, i32 438)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @LOG_ERR
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %3, i32 %19)
br label %21
21: ; preds = %16, %13
%22 = tail call i32 @dup2(i32 %11, i32 %1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %21, %5
%25 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), %21 ]
%26 = load i32, i32* @LOG_ERR
%27 = load i32, i32* @errno
%28 = tail call i32 @strerror(i32 %27)
%29 = tail call i32 @syslog(i32 %26, i8* %25, i8* %0, i8* %3, i32 %28)
br label %30
30: ; preds = %24, %21, %2
ret void
}