#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*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open_log(char *);
scalar_t__ strcmp(char *, char *);
int init_log_d(char *name, int *fdp) {
  int fd, nfd;
  if (name) {
    fd = *fdp;
    if (strcmp(name, "/dev/stdout") == 0) {
      if (fd != -1)
        return 0;
      nfd = dup(1);
    } else if (strcmp(name, "/dev/stderr") == 0) {
      if (fd != -1)
        return 0;
      nfd = dup(2);
    } else
      nfd = open_log(name);
    if (nfd == -1)
      return fd == -1 ? -1 : 0;
    if (fd == -1)
      *fdp = nfd;
    else if (nfd != fd) {
      dup2(nfd, fd);
      close(nfd);
      nfd = fd;
    }
    fcntl(nfd, F_SETFD, FD_CLOEXEC);
  }
  return 0;
}


x86 assembly (clang, -O0):
<init_log_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1297 <init_log_d+0x137>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    11c6 <init_log_d+0x66>
cmpl   $0xffffffff,-0x1c(%rbp)
je     11b4 <init_log_d+0x54>
movl   $0x0,-0x4(%rbp)
jmp    129e <init_log_d+0x13e>
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,-0x20(%rbp)
jmp    1219 <init_log_d+0xb9>
mov    -0x10(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1208 <init_log_d+0xa8>
cmpl   $0xffffffff,-0x1c(%rbp)
je     11f6 <init_log_d+0x96>
movl   $0x0,-0x4(%rbp)
jmp    129e <init_log_d+0x13e>
mov    $0x2,%edi
call   1060 <dup@plt>
mov    %eax,-0x20(%rbp)
jmp    1214 <init_log_d+0xb4>
mov    -0x10(%rbp),%rdi
call   1040 <open_log@plt>
mov    %eax,-0x20(%rbp)
jmp    1219 <init_log_d+0xb9>
cmpl   $0xffffffff,-0x20(%rbp)
jne    123b <init_log_d+0xdb>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0xffffffff,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    129e <init_log_d+0x13e>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1253 <init_log_d+0xf3>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    127d <init_log_d+0x11d>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1278 <init_log_d+0x118>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    127d <init_log_d+0x11d>
mov    -0x20(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d48(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_log_d>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1218 <init_log_d+0xb8>
mov    %rsi,%r15
mov    %rdi,%rbp
mov    (%rsi),%r14d
lea    0xe7e(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11aa <init_log_d+0x4a>
lea    0xe79(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11b7 <init_log_d+0x57>
mov    %rbp,%rdi
call   1040 <open_log@plt>
jmp    11c7 <init_log_d+0x67>
cmp    $0xffffffff,%r14d
jne    1218 <init_log_d+0xb8>
mov    $0x1,%edi
jmp    11c2 <init_log_d+0x62>
cmp    $0xffffffff,%r14d
jne    1218 <init_log_d+0xb8>
mov    $0x2,%edi
call   1060 <dup@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11ef <init_log_d+0x8f>
cmp    $0xffffffff,%r14d
je     11fc <init_log_d+0x9c>
cmp    %r14d,%ebp
je     11ea <init_log_d+0x8a>
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%ebp
jmp    11ff <init_log_d+0x9f>
xor    %ebx,%ebx
cmp    $0xffffffff,%r14d
sete   %bl
neg    %ebx
jmp    1218 <init_log_d+0xb8>
mov    %ebp,(%r15)
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/stdout\00"
@.str.1 = [12 x i8] c"/dev/stderr\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @init_log_d(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %67
10: ; preds = %2
%11 = load i32*, i32** %5
%12 = load i32, i32* %11
store i32 %12, i32* %6
%13 = load i8*, i8** %4
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 0, i32* %3
br label %68
20: ; preds = %16
%21 = call i32 @dup(i32 1)
store i32 %21, i32* %7
br label %36
22: ; preds = %10
%23 = load i8*, i8** %4
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 0, i32* %3
br label %68
30: ; preds = %26
%31 = call i32 @dup(i32 2)
store i32 %31, i32* %7
br label %35
32: ; preds = %22
%33 = load i8*, i8** %4
%34 = call i32 @open_log(i8* %33)
store i32 %34, i32* %7
br label %35
35: ; preds = %32, %30
br label %36
36: ; preds = %35, %20
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, -1
%42 = zext i1 %41 to i64
%43 = select i1 %41, i32 -1, i32 0
store i32 %43, i32* %3
br label %68
44: ; preds = %36
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %7
%49 = load i32*, i32** %5
store i32 %48, i32* %49
br label %62
50: ; preds = %44
%51 = load i32, i32* %7
%52 = load i32, i32* %6
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = load i32, i32* %7
%56 = load i32, i32* %6
%57 = call i32 @dup2(i32 %55, i32 %56)
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %6
store i32 %60, i32* %7
br label %61
61: ; preds = %54, %50
br label %62
62: ; preds = %61, %47
%63 = load i32, i32* %7
%64 = load i32, i32* @F_SETFD
%65 = load i32, i32* @FD_CLOEXEC
%66 = call i32 @fcntl(i32 %63, i32 %64, i32 %65)
br label %67
67: ; preds = %62, %2
store i32 0, i32* %3
br label %68
68: ; preds = %67, %39, %29, %19
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/stdout\00"
@.str.1 = [12 x i8] c"/dev/stderr\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @init_log_d(i8* %0, i32* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %39, label %4
4: ; preds = %2
%5 = load i32, i32* %1
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = icmp eq i32 %5, -1
br i1 %9, label %10, label %39
10: ; preds = %8
%11 = tail call i32 @dup(i32 1)
br label %21
12: ; preds = %4
%13 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = icmp eq i32 %5, -1
br i1 %16, label %17, label %39
17: ; preds = %15
%18 = tail call i32 @dup(i32 2)
br label %21
19: ; preds = %12
%20 = tail call i32 @open_log(i8* %0)
br label %21
21: ; preds = %17, %19, %10
%22 = phi i32 [ %11, %10 ], [ %18, %17 ], [ %20, %19 ]
%23 = icmp eq i32 %22, -1
%24 = icmp eq i32 %5, -1
br i1 %23, label %25, label %27
25: ; preds = %21
%26 = sext i1 %24 to i32
br label %39
27: ; preds = %21
br i1 %24, label %28, label %29
28: ; preds = %27
store i32 %22, i32* %1
br label %34
29: ; preds = %27
%30 = icmp eq i32 %22, %5
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = tail call i32 @dup2(i32 %22, i32 %5)
%33 = tail call i32 @close(i32 %22)
br label %34
34: ; preds = %29, %31, %28
%35 = phi i32 [ %22, %28 ], [ %5, %31 ], [ %5, %29 ]
%36 = load i32, i32* @F_SETFD
%37 = load i32, i32* @FD_CLOEXEC
%38 = tail call i32 @fcntl(i32 %35, i32 %36, i32 %37)
br label %39
39: ; preds = %2, %34, %15, %8, %25
%40 = phi i32 [ %26, %25 ], [ 0, %8 ], [ 0, %15 ], [ 0, %34 ], [ 0, %2 ]
ret i32 %40
}