#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 */
char *basename(char *);
int daemon(int, int);
int execv(char *, char **);
int /*<<< orphan*/ printf(char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  int result = -1;
  int nochdir = 0;
  int noclose = 0;
  if (1 < argc)
    nochdir = strtol(argv[1], NULL, 10);
  if (2 < argc)
    noclose = strtol(argv[2], NULL, 10);
  if (3 < argc) {
    int i, j;
    char *path;
    char *eargv[argc];
    path = argv[3];
    eargv[0] = basename(path);
    for (i = 1, j = 4; j < argc; i++, j++)
      eargv[i] = argv[j];
    eargv[i] = NULL;
    if (-1 != (result = daemon(nochdir, noclose)))
      result = execv(path, eargv);
  } else
    printf("daemon nochdir noclose command...\n");
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x1,%eax
cmp    -0x8(%rbp),%eax
jge    11a2 <main+0x52>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
jge    11c9 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x3,%eax
cmp    -0x8(%rbp),%eax
jge    12af <main+0x15f>
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <basename@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x20(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1266 <main+0x116>
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
mov    (%rcx,%rdx,8),%rdx
movslq -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    122b <main+0xdb>
mov    -0x48(%rbp),%rax
movslq -0x20(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <daemon@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     12a3 <main+0x153>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rdi
call   1070 <execv@plt>
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    12bb <main+0x16b>
lea    0xd4a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     123b <main+0xdb>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    $0x2,%ebx
je     123b <main+0xdb>
mov    %eax,%r15d
mov    0x10(%r14),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    $0x4,%ebx
jb     123b <main+0xdb>
mov    %eax,-0x2c(%rbp)
mov    %r15d,-0x30(%rbp)
mov    %rsp,-0x38(%rbp)
mov    %ebx,%eax
mov    %rsp,%r12
lea    0xf(,%rax,8),%r15
and    $0xfffffffffffffff0,%r15
mov    %r12,%r13
sub    %r15,%r13
mov    %r13,%rsp
neg    %r15
mov    0x18(%r14),%rdi
mov    %rdi,-0x40(%rbp)
call   1040 <basename@plt>
mov    %rax,(%r12,%r15,1)
mov    $0x1,%eax
cmp    $0x4,%ebx
je     1214 <main+0xb4>
lea    0x8(%r13),%rdi
add    $0x20,%r14
lea    -0x4(%rbx),%edx
shl    $0x3,%rdx
mov    %r14,%rsi
call   1060 <memcpy@plt>
add    $0xfffffffd,%ebx
mov    %rbx,%rax
movq   $0x0,0x0(%r13,%rax,8)
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1070 <daemon@plt>
cmp    $0xffffffff,%eax
je     125b <main+0xfb>
mov    -0x40(%rbp),%rdi
mov    %r13,%rsi
call   1080 <execv@plt>
jmp    1260 <main+0x100>
lea    0xdbe(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
jmp    124c <main+0xec>

LLVM IR (-O0):
@.str = [35 x i8] c"daemon nochdir noclose command...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%14 = load i32, i32* %4
%15 = icmp slt i32 1, %14
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strtol(i8* %19, i32* null, i32 10)
store i32 %20, i32* %7
br label %21
21: ; preds = %16, %2
%22 = load i32, i32* %4
%23 = icmp slt i32 2, %22
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i32* null, i32 10)
store i32 %28, i32* %8
br label %29
29: ; preds = %24, %21
%30 = load i32, i32* %4
%31 = icmp slt i32 3, %30
br i1 %31, label %32, label %74
32: ; preds = %29
%33 = load i32, i32* %4
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
store i8* %35, i8** %12
%36 = alloca i8*, i64 %34
store i64 %34, i64* %13
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 3
%39 = load i8*, i8** %38
store i8* %39, i8** %11
%40 = load i8*, i8** %11
%41 = call i8* @basename(i8* %40)
%42 = getelementptr inbounds i8*, i8** %36, i64 0
store i8* %41, i8** %42
store i32 1, i32* %9
store i32 4, i32* %10
br label %43
43: ; preds = %56, %32
%44 = load i32, i32* %10
%45 = load i32, i32* %4
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %61
47: ; preds = %43
%48 = load i8**, i8*** %5
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
%53 = load i32, i32* %9
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %36, i64 %54
store i8* %52, i8** %55
br label %56
56: ; preds = %47
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %9
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %43
61: ; preds = %43
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %36, i64 %63
store i8* null, i8** %64
%65 = load i32, i32* %7
%66 = load i32, i32* %8
%67 = call i32 @daemon(i32 %65, i32 %66)
store i32 %67, i32* %6
%68 = icmp ne i32 -1, %67
br i1 %68, label %69, label %72
69: ; preds = %61
%70 = load i8*, i8** %11
%71 = call i32 @execv(i8* %70, i8** %36)
store i32 %71, i32* %6
br label %72
72: ; preds = %69, %61
%73 = load i8*, i8** %12
br label %76
74: ; preds = %29
%75 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %76
76: ; preds = %74, %72
%77 = load i32, i32* %6
ret i32 %77
}

LLVM IR (-O2):
@str = [34 x i8] c"daemon nochdir noclose command...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %41
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @strtol(i8* nocapture %6, i32* null, i32 10)
%8 = icmp eq i32 %0, 2
br i1 %8, label %41, label %9
9: ; preds = %4
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @strtol(i8* nocapture %11, i32* null, i32 10)
%13 = icmp ugt i32 %0, 3
br i1 %13, label %14, label %41
14: ; preds = %9
%15 = zext i32 %0 to i64
%16 = tail call i8* @llvm.stacksave()
%17 = alloca i8*, i64 %15
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i8* @basename(i8* %19)
store i8* %20, i8** %17
%21 = icmp eq i32 %0, 4
br i1 %21, label %32, label %22
22: ; preds = %14
%23 = getelementptr inbounds i8*, i8** %17, i64 1
%24 = bitcast i8** %23 to i8*
%25 = getelementptr i8*, i8** %1, i64 4
%26 = bitcast i8** %25 to i8*
%27 = add nsw i32 %0, -4
%28 = zext i32 %27 to i64
%29 = shl nuw nsw i64 %28, 3
%30 = add nsw i32 %0, -3
%31 = zext i32 %30 to i64
br label %32
32: ; preds = %22, %14
%33 = phi i64 [ 1, %14 ], [ %31, %22 ]
%34 = getelementptr inbounds i8*, i8** %17, i64 %33
store i8* null, i8** %34
%35 = tail call i32 @daemon(i32 %7, i32 %12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = call i32 @execv(i8* %19, i8** %17)
br label %39
39: ; preds = %37, %32
%40 = phi i32 [ %38, %37 ], [ -1, %32 ]
br label %43
41: ; preds = %2, %4, %9
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
%44 = phi i32 [ %40, %39 ], [ -1, %41 ]
ret i32 %44
}