#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*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int BindDaemonServer(char *pcServerName, int (*ServerMain)(void *pv), void *pv,
                     int (*ControlMain)(long lControlStatus)) {
  int pid;
  pid = fork();
  switch (pid) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    exit(0);
    break;
  }
  setsid();
  pid = fork();
  switch (pid) {
  case -1:
    return -2;
  case 0:
    break;
  default:
    exit(0);
    break;
  }
  umask(0);
  return ServerMain(pv);
}


x86 assembly (clang, -O0):
<BindDaemonServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0xffffffff,%eax
je     1186 <BindDaemonServer+0x46>
jmp    1176 <BindDaemonServer+0x36>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     1192 <BindDaemonServer+0x52>
jmp    1197 <BindDaemonServer+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <BindDaemonServer+0xc3>
jmp    119e <BindDaemonServer+0x5e>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %eax,%eax
mov    %al,-0x35(%rbp)
call   1030 <setsid@plt>
mov    -0x35(%rbp),%al
call   1060 <fork@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0xffffffff,%eax
je     11d7 <BindDaemonServer+0x97>
jmp    11c7 <BindDaemonServer+0x87>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     11e3 <BindDaemonServer+0xa3>
jmp    11e8 <BindDaemonServer+0xa8>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1203 <BindDaemonServer+0xc3>
jmp    11ef <BindDaemonServer+0xaf>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1040 <umask@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rdi
call   *%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BindDaemonServer>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     118e <BindDaemonServer+0x4e>
test   %eax,%eax
jne    1196 <BindDaemonServer+0x56>
xor    %eax,%eax
call   1030 <setsid@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
cmp    $0xffffffff,%ecx
je     118e <BindDaemonServer+0x4e>
test   %ecx,%ecx
jne    1196 <BindDaemonServer+0x56>
xor    %edi,%edi
call   1040 <umask@plt>
mov    %r14,%rdi
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    *%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
define i32 @BindDaemonServer(i8* %0, i32 (i8*)* %1, i8* %2, i32 (i64)* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32 (i8*)*
%8 = alloca i8*
%9 = alloca i32 (i64)*
%10 = alloca i32
store i8* %0, i8** %6
store i32 (i8*)* %1, i32 (i8*)** %7
store i8* %2, i8** %8
store i32 (i64)* %3, i32 (i64)** %9
%11 = call i32 (...) @fork()
store i32 %11, i32* %10
%12 = load i32, i32* %10
switch i32 %12, label %15 [
i32 -1, label %13
i32 0, label %14
]
13: ; preds = %4
store i32 -1, i32* %5
br label %30
14: ; preds = %4
br label %17
15: ; preds = %4
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %14
%18 = call i32 (...) @setsid()
%19 = call i32 (...) @fork()
store i32 %19, i32* %10
%20 = load i32, i32* %10
switch i32 %20, label %23 [
i32 -1, label %21
i32 0, label %22
]
21: ; preds = %17
store i32 -2, i32* %5
br label %30
22: ; preds = %17
br label %25
23: ; preds = %17
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %22
%26 = call i32 @umask(i32 0)
%27 = load i32 (i8*)*, i32 (i8*)** %7
%28 = load i8*, i8** %8
%29 = call i32 %27(i8* %28)
store i32 %29, i32* %5
br label %30
30: ; preds = %25, %21, %13
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
define i32 @BindDaemonServer(i8* nocapture readnone %0, i32 (i8*)* nocapture %1, i8* %2, i32 (i64)* nocapture readnone %3) {
%5 = tail call i32 (...) @fork()
switch i32 %5, label %6 [
i32 -1, label %16
i32 0, label %8
]
6: ; preds = %4
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %4
%9 = tail call i32 (...) @setsid()
%10 = tail call i32 (...) @fork()
switch i32 %10, label %11 [
i32 -1, label %16
i32 0, label %13
]
11: ; preds = %8
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = tail call i32 @umask(i32 0)
%15 = tail call i32 %1(i8* %2)
br label %16
16: ; preds = %8, %4, %13
%17 = phi i32 [ %15, %13 ], [ %5, %4 ], [ -2, %8 ]
ret i32 %17
}