#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
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/day07/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 < 3; i++)
    close(i);
  dup(open("/dev/null", O_RDWR));
  dup(open("/dev/null", O_RDWR));
  dup(open("/dev/null", O_RDWR));
  printf("hehe!\n");
  int fd = open("daemon.txt", O_RDWR | O_CREAT | O_TRUNC, 0666);
  if (fd == -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/day07/daemon.c",
              36, "open", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  while (1) {
    write(fd, ".", 1);
    sleep(1);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<setup_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    122d <setup_daemon+0x6d>
jmp    11e0 <setup_daemon+0x20>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%r9
lea    0xdf9(%rip),%rsi
lea    0xe02(%rip),%rdx
mov    $0x12,%ecx
lea    0xe8b(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1245 <setup_daemon+0x85>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
lea    0xe57(%rip),%rdi
call   1040 <chdir@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    127f <setup_daemon+0xbf>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    125f <setup_daemon+0x9f>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
lea    0xdd6(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%esi
mov    0x2d03(%rip),%rax
or     (%rax),%esi
mov    0x2cf2(%rip),%rax
or     (%rax),%esi
lea    0xdb6(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1372 <setup_daemon+0x1b2>
jmp    1325 <setup_daemon+0x165>
mov    0x2c9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%r9
lea    0xcb4(%rip),%rsi
lea    0xcbd(%rip),%rdx
mov    $0x24,%ecx
lea    0xd69(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1377 <setup_daemon+0x1b7>
mov    -0xc(%rbp),%edi
lea    0xd4c(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    1377 <setup_daemon+0x1b7>

x86 assembly (clang, -O2):
<setup_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
je     12bb <setup_daemon+0xfb>
test   %eax,%eax
jg     12f0 <setup_daemon+0x130>
xor    %eax,%eax
call   1080 <setsid@plt>
lea    0xec0(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    0x2dc7(%rip),%r14
mov    (%r14),%esi
lea    0xe91(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12fe <setup_daemon+0x13e>
mov    %eax,%ebx
lea    0xe30(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    12a0 <setup_daemon+0xe0>
mov    0x2d06(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd27(%rip),%rsi
lea    0xd30(%rip),%rdx
lea    0xdbe(%rip),%r8
mov    %ebx,%edi
mov    $0x12,%ecx
jmp    1331 <setup_daemon+0x171>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%ebx
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xce4(%rip),%rsi
lea    0xced(%rip),%rdx
lea    0xd97(%rip),%r8
mov    %ebx,%edi
mov    $0x24,%ecx
mov    %rax,%r9
call   1090 <fprintf@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10b0 <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/day07/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"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.5 = [7 x i8] c"hehe!\0A\00"
@.str.6 = [11 x i8] c"daemon.txt\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.7 = [5 x i8] c"open\00"
@.str.8 = [2 x i8] c".\00"
define i32 @setup_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %15
6: ; preds = %0
br label %7
7: ; preds = %6
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, 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* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %0
%16 = load i32, i32* %1
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = call i32 (...) @setsid()
%23 = 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 %24
24: ; preds = %30, %21
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 3
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %24
33: ; preds = %24
%34 = load i32, i32* @O_RDWR
%35 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 @dup(i32 %35)
%37 = load i32, i32* @O_RDWR
%38 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = call i32 @dup(i32 %38)
%40 = load i32, i32* @O_RDWR
%41 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = call i32 @dup(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%44 = load i32, i32* @O_RDWR
%45 = load i32, i32* @O_CREAT
%46 = or i32 %44, %45
%47 = load i32, i32* @O_TRUNC
%48 = or i32 %46, %47
%49 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %48, i32 438)
store i32 %49, i32* %3
%50 = load i32, i32* %3
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %61
52: ; preds = %33
br label %53
53: ; preds = %52
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, 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 36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; No predecessors!
br label %61
61: ; preds = %60, %33
br label %62
62: ; preds = %61, %62
%63 = load i32, i32* %3
%64 = call i32 @write(i32 %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%65 = call i32 @sleep(i32 1)
br label %62
}

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/day07/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"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.6 = [11 x i8] c"daemon.txt\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.7 = [5 x i8] c"open\00"
@.str.8 = [2 x i8] c".\00"
@str = [6 x i8] c"hehe!\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))
%18 = tail call i32 @close(i32 0)
%19 = tail call i32 @close(i32 1)
%20 = tail call i32 @close(i32 2)
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = tail call i32 @dup(i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = tail call i32 @dup(i32 %25)
%27 = load i32, i32* @O_RDWR
%28 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = tail call i32 @dup(i32 %28)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%31 = load i32, i32* @O_RDWR
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %33, %34
%36 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %35, i32 438)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %45
38: ; preds = %15
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = tail call i8* @strerror(i32 %40)
%42 = tail call i32 @fprintf(i32 %39, 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 36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %15, %45
%46 = tail call i32 @write(i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%47 = tail call i32 @sleep(i32 1)
br label %45
}