#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 EEXIST;
int chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getppid();
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int osmo_daemonize(void) {
  int rc;
  pid_t pid, sid;
  if (getppid() == 1)
    return -EEXIST;
  pid = fork();
  if (pid < 0) {
    return pid;
  }
  if (pid > 0) {
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0)
    return sid;
  rc = chdir("/tmp");
  if (rc < 0)
    return rc;
  return 0;
}


x86 assembly (clang, -O0):
<osmo_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
jne    118b <osmo_daemonize+0x2b>
xor    %eax,%eax
mov    0x2e5f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <osmo_daemonize+0xb3>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ad <osmo_daemonize+0x4d>
mov    -0x10(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1213 <osmo_daemonize+0xb3>
cmpq   $0x0,-0x10(%rbp)
jle    11bf <osmo_daemonize+0x5f>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1050 <umask@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11e8 <osmo_daemonize+0x88>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1213 <osmo_daemonize+0xb3>
lea    0xe11(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    120c <osmo_daemonize+0xac>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <osmo_daemonize+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<osmo_daemonize>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
cmp    $0x1,%eax
jne    117a <osmo_daemonize+0x1a>
mov    0x2e6a(%rip),%rax
sub    (%rax),%ebx
jmp    11b3 <osmo_daemonize+0x53>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11b0 <osmo_daemonize+0x50>
jne    11b7 <osmo_daemonize+0x57>
xor    %edi,%edi
call   1050 <umask@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     11b0 <osmo_daemonize+0x50>
lea    0xe5e(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,%ebx
sar    $0x1f,%ebx
and    %eax,%ebx
jmp    11b3 <osmo_daemonize+0x53>
mov    %rax,%rbx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EEXIST = global i32 0
@.str = [5 x i8] c"/tmp\00"
define i32 @osmo_daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = call i32 (...) @getppid()
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @EEXIST
%9 = sub nsw i32 0, %8
store i32 %9, i32* %1
br label %37
10: ; preds = %0
%11 = call i64 (...) @fork()
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i64, i64* %3
%16 = trunc i64 %15 to i32
store i32 %16, i32* %1
br label %37
17: ; preds = %10
%18 = load i64, i64* %3
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %17
%23 = call i32 @umask(i32 0)
%24 = call i64 (...) @setsid()
store i64 %24, i64* %4
%25 = load i64, i64* %4
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i64, i64* %4
%29 = trunc i64 %28 to i32
store i32 %29, i32* %1
br label %37
30: ; preds = %22
%31 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %31, i32* %2
%32 = load i32, i32* %2
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = load i32, i32* %2
store i32 %35, i32* %1
br label %37
36: ; preds = %30
store i32 0, i32* %1
br label %37
37: ; preds = %36, %34, %27, %14, %7
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@EEXIST = global i32 0
@.str = [5 x i8] c"/tmp\00"
define i32 @osmo_daemonize() {
%1 = tail call i32 (...) @getppid()
%2 = icmp eq i32 %1, 1
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EEXIST
%5 = sub nsw i32 0, %4
br label %25
6: ; preds = %0
%7 = tail call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = trunc i64 %7 to i32
br label %25
11: ; preds = %6
%12 = icmp eq i64 %7, 0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %11
%16 = tail call i32 @umask(i32 0)
%17 = tail call i64 (...) @setsid()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = trunc i64 %17 to i32
br label %25
21: ; preds = %15
%22 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = icmp slt i32 %22, 0
%24 = select i1 %23, i32 %22, i32 0
br label %25
25: ; preds = %21, %19, %9, %3
%26 = phi i32 [ %5, %3 ], [ %10, %9 ], [ %20, %19 ], [ %24, %21 ]
ret i32 %26
}