#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int setup_daemon(void) {
  pid_t pid;
  if ((pid = fork()) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day06/daemon.c",
              18, "fork", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  setsid();
  chdir("/");
  int i;
  for (i = 0; i < 100; i++)
    close(i);
  while (1)
    ;
}


x86 assembly (clang, -O0):
<setup_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11dd <setup_daemon+0x6d>
jmp    1190 <setup_daemon+0x20>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%r9
lea    0xe49(%rip),%rsi
lea    0xe52(%rip),%rdx
mov    $0x12,%ecx
lea    0xedb(%rip),%r8
call   1060 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11f5 <setup_daemon+0x85>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xea7(%rip),%rdi
call   1030 <chdir@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x64,-0x8(%rbp)
jge    122f <setup_daemon+0xbf>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    120f <setup_daemon+0x9f>
jmp    1234 <setup_daemon+0xc4>
jmp    1234 <setup_daemon+0xc4>

x86 assembly (clang, -O2):
<setup_daemon>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11b2 <setup_daemon+0x42>
test   %eax,%eax
jg     11fb <setup_daemon+0x8b>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xf19(%rip),%rdi
call   1030 <chdir@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    $0x64,%ebx
jne    11a0 <setup_daemon+0x30>
nop
jmp    11b0 <setup_daemon+0x40>
mov    0x2e27(%rip),%rax
mov    (%rax),%ebx
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe30(%rip),%rsi
lea    0xe39(%rip),%rdx
lea    0xec7(%rip),%r8
mov    %ebx,%edi
mov    $0x12,%ecx
mov    %rax,%r9
call   1060 <fprintf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day06/daemon.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [2 x i8] c"/\00"
define i32 @setup_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %14
5: ; preds = %0
br label %6
6: ; preds = %5
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.1, i64 0, i64 0), i32 18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; No predecessors!
br label %14
14: ; preds = %13, %0
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
%21 = call i32 (...) @setsid()
%22 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %23
23: ; preds = %29, %20
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 100
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
br label %23
32: ; preds = %23
br label %33
33: ; preds = %32, %33
br label %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day06/daemon.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [2 x i8] c"/\00"
define i32 @setup_daemon() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %10
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.1, i64 0, i64 0), i32 18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = icmp sgt i32 %1, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = load i32, i32* @EXIT_SUCCESS
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %10
%16 = tail call i32 (...) @setsid()
%17 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %15, %18
%19 = phi i32 [ 0, %15 ], [ %21, %18 ]
%20 = tail call i32 @close(i32 %19)
%21 = add nuw nsw i32 %19, 1
%22 = icmp eq i32 %21, 100
br i1 %22, label %23, label %18
23: ; preds = %18, %23
br label %23
}