#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*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int fork_and_exit();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *,
                           int);
int open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int daemonize(const char *dir, int noclose) {
  int fd;
  if (fork_and_exit() == -1) {
    fprintf(
        stderr,
        "%s: "
        "fork: %s"
        " (%s:%d)\n",
        "warning", strerror(errno),
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
        470);
    ;
    ;
    return -1;
  }
  if (setsid() == -1) {
    fprintf(
        stderr,
        "%s: "
        "setsid: %s"
        " (%s:%d)\n",
        "warning", strerror(errno),
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
        474);
    ;
    ;
    return -1;
  }
  if (fork_and_exit() == -1) {
    fprintf(
        stderr,
        "%s: "
        "fork: %s"
        " (%s:%d)\n",
        "warning", strerror(errno),
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
        478);
    ;
    ;
    return -1;
  }
  if (dir) {
    chdir(dir);
  }
  if (!noclose) {
    if ((fd = open("/dev/null", O_RDWR)) == -1) {
      fprintf(
          stderr,
          "%s: "
          "open: %s"
          " (%s:%d)\n",
          "warning", strerror(errno),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
          486);
      ;
      ;
      return -1;
    }
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > STDERR_FILENO) {
      close(fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1030 <fork_and_exit@plt>
cmp    $0xffffffff,%eax
jne    11eb <daemonize+0x6b>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rcx
lea    0xe3a(%rip),%rsi
lea    0xe49(%rip),%rdx
lea    0xe4a(%rip),%r8
mov    $0x1d6,%r9d
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <daemonize+0x207>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1247 <daemonize+0xc7>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rcx
lea    0xe7f(%rip),%rsi
lea    0xded(%rip),%rdx
lea    0xdee(%rip),%r8
mov    $0x1da,%r9d
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <daemonize+0x207>
mov    $0x0,%al
call   1030 <fork_and_exit@plt>
cmp    $0xffffffff,%eax
jne    12a3 <daemonize+0x123>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rcx
lea    0xd82(%rip),%rsi
lea    0xd91(%rip),%rdx
lea    0xd92(%rip),%r8
mov    $0x1de,%r9d
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <daemonize+0x207>
cmpq   $0x0,-0x10(%rbp)
je     12b7 <daemonize+0x137>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1380 <daemonize+0x200>
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    132e <daemonize+0x1ae>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rcx
lea    0xdba(%rip),%rsi
lea    0xd06(%rip),%rdx
lea    0xd07(%rip),%r8
mov    $0x1e6,%r9d
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <daemonize+0x207>
mov    -0x18(%rbp),%edi
mov    0x2c80(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2ca7(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2c66(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%eax
mov    0x2c55(%rip),%rcx
cmp    (%rcx),%eax
jle    137b <daemonize+0x1fb>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    1380 <daemonize+0x200>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <fork_and_exit@plt>
cmp    $0xffffffff,%eax
je     1236 <daemonize+0xb6>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     126f <daemonize+0xef>
xor    %eax,%eax
call   1030 <fork_and_exit@plt>
cmp    $0xffffffff,%eax
je     12a8 <daemonize+0x128>
test   %rbx,%rbx
je     11c6 <daemonize+0x46>
mov    %rbx,%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %ebp,%ebp
jne    12e9 <daemonize+0x169>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xed9(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f0 <daemonize+0x170>
mov    %eax,%ebp
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2da9(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    (%r14),%ebp
jle    12e9 <daemonize+0x169>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12e9 <daemonize+0x169>
mov    0x2d93(%rip),%rax
mov    (%rax),%ebx
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdac(%rip),%rsi
lea    0xdbb(%rip),%rdx
lea    0xdbc(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x1d6,%r9d
jmp    12df <daemonize+0x15f>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe14(%rip),%rsi
lea    0xd82(%rip),%rdx
lea    0xd83(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x1da,%r9d
jmp    12df <daemonize+0x15f>
mov    0x2d21(%rip),%rax
mov    (%rax),%ebx
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd3a(%rip),%rsi
lea    0xd49(%rip),%rdx
lea    0xd4a(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x1de,%r9d
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cd9(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb5(%rip),%rsi
lea    0xd01(%rip),%rdx
lea    0xd02(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x1e6,%r9d
jmp    12df <daemonize+0x15f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"%s: fork: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@errno = global i32 0
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
@.str.3 = [24 x i8] c"%s: setsid: %s (%s:%d)\0A\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.5 = [22 x i8] c"%s: open: %s (%s:%d)\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = call i32 (...) @fork_and_exit()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 470)
store i32 -1, i32* %3
br label %66
14: ; preds = %2
%15 = call i32 (...) @setsid()
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 474)
store i32 -1, i32* %3
br label %66
22: ; preds = %14
%23 = call i32 (...) @fork_and_exit()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 478)
store i32 -1, i32* %3
br label %66
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i8*, i8** %4
%35 = call i32 @chdir(i8* %34)
br label %36
36: ; preds = %33, %30
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
br i1 %38, label %65, label %39
39: ; preds = %36
%40 = load i32, i32* @O_RDWR
%41 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %40)
store i32 %41, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %46, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 486)
store i32 -1, i32* %3
br label %66
48: ; preds = %39
%49 = load i32, i32* %6
%50 = load i32, i32* @STDIN_FILENO
%51 = call i32 @dup2(i32 %49, i32 %50)
%52 = load i32, i32* %6
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @dup2(i32 %52, i32 %53)
%55 = load i32, i32* %6
%56 = load i32, i32* @STDERR_FILENO
%57 = call i32 @dup2(i32 %55, i32 %56)
%58 = load i32, i32* %6
%59 = load i32, i32* @STDERR_FILENO
%60 = icmp sgt i32 %58, %59
br i1 %60, label %61, label %64
61: ; preds = %48
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %48
br label %65
65: ; preds = %64, %36
store i32 0, i32* %3
br label %66
66: ; preds = %65, %43, %25, %17, %9
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"%s: fork: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@errno = global i32 0
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
@.str.3 = [24 x i8] c"%s: setsid: %s (%s:%d)\0A\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.5 = [22 x i8] c"%s: open: %s (%s:%d)\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i8* %0, i32 %1) {
%3 = tail call i32 (...) @fork_and_exit()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 470)
br label %52
10: ; preds = %2
%11 = tail call i32 (...) @setsid()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 474)
br label %52
18: ; preds = %10
%19 = tail call i32 (...) @fork_and_exit()
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 478)
br label %52
26: ; preds = %18
%27 = icmp eq i8* %0, null
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = tail call i32 @chdir(i8* %0)
br label %30
30: ; preds = %28, %26
%31 = icmp eq i32 %1, 0
br i1 %31, label %32, label %52
32: ; preds = %30
%33 = load i32, i32* @O_RDWR
%34 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = tail call i8* @strerror(i32 %38)
%40 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 486)
br label %52
41: ; preds = %32
%42 = load i32, i32* @STDIN_FILENO
%43 = tail call i32 @dup2(i32 %34, i32 %42)
%44 = load i32, i32* @STDOUT_FILENO
%45 = tail call i32 @dup2(i32 %34, i32 %44)
%46 = load i32, i32* @STDERR_FILENO
%47 = tail call i32 @dup2(i32 %34, i32 %46)
%48 = load i32, i32* @STDERR_FILENO
%49 = icmp sgt i32 %34, %48
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = tail call i32 @close(i32 %34)
br label %52
52: ; preds = %30, %50, %41, %36, %21, %13, %5
%53 = phi i32 [ -1, %5 ], [ -1, %13 ], [ -1, %21 ], [ -1, %36 ], [ 0, %41 ], [ 0, %50 ], [ 0, %30 ]
ret i32 %53
}