#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pb_log(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
void detach_and_sleep(int sec) {
  static int forked = 0;
  int rc = 0;
  if (sec <= 0)
    return;
  if (!forked) {
    pb_log("running in background...");
    rc = fork();
    forked = 1;
  }
  if (rc == 0) {
    sleep(sec);
  } else if (rc == -1) {
    perror("fork()");
    exit(EXIT_FAILURE);
  } else {
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<detach_and_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     1171 <detach_and_sleep+0x21>
jmp    11e7 <detach_and_sleep+0x97>
cmpl   $0x0,0x2ed4(%rip)
jne    119e <detach_and_sleep+0x4e>
lea    0xe7b(%rip),%rdi
call   1030 <pb_log@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
movl   $0x1,0x2eae(%rip)
cmpl   $0x0,-0x8(%rbp)
jne    11b5 <detach_and_sleep+0x65>
mov    -0x4(%rbp),%edi
call   1060 <sleep@plt>
jmp    11e7 <detach_and_sleep+0x97>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d9 <detach_and_sleep+0x89>
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach_and_sleep>:
push   %rbx
test   %edi,%edi
jle    1186 <detach_and_sleep+0x36>
mov    %edi,%ebx
cmpb   $0x0,0x2eee(%rip)
jne    117e <detach_and_sleep+0x2e>
lea    0xe99(%rip),%rdi
call   1030 <pb_log@plt>
xor    %eax,%eax
call   1070 <fork@plt>
movb   $0x1,0x2ed2(%rip)
test   %eax,%eax
jne    1188 <detach_and_sleep+0x38>
mov    %ebx,%edi
pop    %rbx
jmp    1060 <sleep@plt>
pop    %rbx
ret
cmp    $0xffffffff,%eax
jne    11a7 <detach_and_sleep+0x57>
lea    0xe85(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@detach_and_sleep.forked = internal global i32 0
@.str = [25 x i8] c"running in background...\00"
@.str.1 = [7 x i8] c"fork()\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define void @detach_and_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = icmp sle i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %1
br label %29
7: ; preds = %1
%8 = load i32, i32* @detach_and_sleep.forked
%9 = icmp ne i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i32 @pb_log(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (...) @fork()
store i32 %12, i32* %3
store i32 1, i32* @detach_and_sleep.forked
br label %13
13: ; preds = %10, %7
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* %2
%18 = call i32 @sleep(i32 %17)
br label %29
19: ; preds = %13
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %19
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %6, %16
ret void
}

LLVM IR (-O2):
@detach_and_sleep.forked = internal unnamed_addr global i1 false
@.str = [25 x i8] c"running in background...\00"
@.str.1 = [7 x i8] c"fork()\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define void @detach_and_sleep(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = load i1, i1* @detach_and_sleep.forked
br i1 %4, label %8, label %5
5: ; preds = %3
%6 = tail call i32 @pb_log(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (...) @fork()
store i1 true, i1* @detach_and_sleep.forked
switch i32 %7, label %14 [
i32 0, label %8
i32 -1, label %10
]
8: ; preds = %3, %5
%9 = tail call i32 @sleep(i32 %0)
br label %17
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %1, %8
ret void
}