#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int sysconf(int /*<<< orphan*/);
void daemonize() {
  pid_t pid;
  int fd, i;
  printf(("Running WeeChat in background..."));
  printf(" ");
  pid = fork();
  if (pid < 0) {
    printf(("fork error"));
    printf("\n");
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    printf(("OK"));
    printf("\n");
    exit(EXIT_SUCCESS);
  }
  setsid();
  for (i = sysconf(_SC_OPEN_MAX); i >= 0; --i) {
    close(i);
  }
  fd = open("/dev/null", O_RDWR);
  (void)dup(fd);
  (void)dup(fd);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
lea    0xe86(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11dc <daemonize+0x5c>
lea    0xe66(%rip),%rdi
call   1030 <printf@plt>
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    120d <daemonize+0x8d>
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
lea    0xe34(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1245 <daemonize+0xc5>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1225 <daemonize+0xa5>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <dup@plt>
mov    -0xc(%rbp),%edi
call   1040 <dup@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1202 <daemonize+0x72>
jne    1226 <daemonize+0x96>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
test   %eax,%eax
js     11dc <daemonize+0x4c>
mov    %eax,%ebx
mov    %ebx,%edi
call   1060 <close@plt>
add    $0xffffffff,%ebx
jb     11d0 <daemonize+0x40>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <dup@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <dup@plt>
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdff(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Running WeeChat in background...\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [2 x i8] c"\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [3 x i8] c"OK\00"
@EXIT_SUCCESS = global i32 0
@_SC_OPEN_MAX = global i32 0
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i64 (...) @fork()
store i64 %6, i64* %1
%7 = load i64, i64* %1
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i64, i64* %1
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
%23 = call i32 (...) @setsid()
%24 = load i32, i32* @_SC_OPEN_MAX
%25 = call i32 @sysconf(i32 %24)
store i32 %25, i32* %3
br label %26
26: ; preds = %32, %22
%27 = load i32, i32* %3
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29
%33 = load i32, i32* %3
%34 = add nsw i32 %33, -1
store i32 %34, i32* %3
br label %26
35: ; preds = %26
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %36)
store i32 %37, i32* %2
%38 = load i32, i32* %2
%39 = call i32 @dup(i32 %38)
%40 = load i32, i32* %2
%41 = call i32 @dup(i32 %40)
ret void
}

LLVM IR (-O2):
@.str = [33 x i8] c"Running WeeChat in background...\00"
@.str.2 = [11 x i8] c"fork error\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [3 x i8] c"OK\00"
@EXIT_SUCCESS = global i32 0
@_SC_OPEN_MAX = global i32 0
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @putchar(i32 32)
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @putchar(i32 10)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = icmp eq i64 %3, 0
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%14 = tail call i32 @putchar(i32 10)
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = tail call i32 (...) @setsid()
%19 = load i32, i32* @_SC_OPEN_MAX
%20 = tail call i32 @sysconf(i32 %19)
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %17, %22
%23 = phi i32 [ %25, %22 ], [ %20, %17 ]
%24 = tail call i32 @close(i32 %23)
%25 = add nsw i32 %23, -1
%26 = icmp eq i32 %23, 0
br i1 %26, label %27, label %22
27: ; preds = %22, %17
%28 = load i32, i32* @O_RDWR
%29 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = tail call i32 @dup(i32 %29)
%31 = tail call i32 @dup(i32 %29)
ret void
}