#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_INFO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ setsid();
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write_pid();
void fork_now(int ttyfd) {
  int i;
  int ret;
  if ((ret = fork()) > 0)
    exit(0);
  if (ret == -1)
    syslog(LOG_INFO, "forking: %m");
  if (setsid() < 0)
    syslog(LOG_INFO, "detaching from tty: %m");
  if ((ret = fork()) > 0) {
    exit(0);
  }
  for (i = 0; i < 64; i++)
    if (i != ttyfd)
      close(i);
  write_pid();
}


x86 assembly (clang, -O0):
<fork_now>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1185 <fork_now+0x25>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a4 <fork_now+0x44>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1050 <syslog@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    11ca <fork_now+0x6a>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1050 <syslog@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11e4 <fork_now+0x84>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x40,-0x8(%rbp)
jge    121c <fork_now+0xbc>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     1209 <fork_now+0xa9>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
jmp    120e <fork_now+0xae>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11eb <fork_now+0x8b>
mov    $0x0,%al
call   1060 <write_pid@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_now>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jg     11b8 <fork_now+0x58>
cmp    $0xffffffff,%eax
jne    118a <fork_now+0x2a>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
call   1050 <syslog@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
jns    11ab <fork_now+0x4b>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1050 <syslog@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jle    11c8 <fork_now+0x68>
xor    %edi,%edi
call   1070 <exit@plt>
nop
add    $0x1,%ebp
cmp    $0x40,%ebp
je     11d5 <fork_now+0x75>
cmp    %ebp,%ebx
je     11c0 <fork_now+0x60>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11c0 <fork_now+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <write_pid@plt>

LLVM IR (-O0):
@LOG_INFO = global i32 0
@.str = [12 x i8] c"forking: %m\00"
@.str.1 = [23 x i8] c"detaching from tty: %m\00"
define void @fork_now(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (...) @fork()
store i32 %5, i32* %4
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @exit(i32 0)
unreachable
9: ; preds = %1
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @LOG_INFO
%14 = call i32 @syslog(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %9
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @LOG_INFO
%20 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %18, %15
%22 = call i32 (...) @fork()
store i32 %22, i32* %4
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %21
store i32 0, i32* %3
br label %27
27: ; preds = %38, %26
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 64
br i1 %29, label %30, label %41
30: ; preds = %27
%31 = load i32, i32* %3
%32 = load i32, i32* %2
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %30
br label %38
38: ; preds = %37
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %27
41: ; preds = %27
%42 = call i32 (...) @write_pid()
ret void
}

LLVM IR (-O2):
@LOG_INFO = global i32 0
@.str = [12 x i8] c"forking: %m\00"
@.str.1 = [23 x i8] c"detaching from tty: %m\00"
define void @fork_now(i32 %0) {
%2 = tail call i32 (...) @fork()
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %1
%7 = icmp eq i32 %2, -1
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = load i32, i32* @LOG_INFO
%10 = tail call i32 @syslog(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %6
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @LOG_INFO
%16 = tail call i32 @syslog(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %14, %11
%18 = tail call i32 (...) @fork()
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %17, %27
%23 = phi i32 [ %28, %27 ], [ 0, %17 ]
%24 = icmp eq i32 %23, %0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 @close(i32 %23)
br label %27
27: ; preds = %22, %25
%28 = add nuw nsw i32 %23, 1
%29 = icmp eq i32 %28, 64
br i1 %29, label %30, label %22
30: ; preds = %27
%31 = tail call i32 (...) @write_pid()
ret void
}