#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 */
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *, ...);
int /*<<< orphan*/ execv(char *, char **);
scalar_t__ fork();
int setsid();
pid_t run(char **argv, int do_setsid) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    error(0, errno, "fork");
    return 0;
  }
  if (pid > 0)
    return pid;
  else {
    if (do_setsid && setsid() == -1)
      error(0, errno, "setsid");
    errno = 0;
    execv(argv[0], argv);
    error(127, errno, "%s", argv[0]);
  }
  return -1;
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    118c <run+0x4c>
mov    0x2e74(%rip),%rax
mov    (%rax),%rsi
xor    %edi,%edi
lea    0xe88(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1222 <run+0xe2>
cmpq   $0x0,-0x20(%rbp)
jle    11a4 <run+0x64>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1222 <run+0xe2>
cmpl   $0x0,-0x14(%rbp)
je     11d8 <run+0x98>
mov    $0x0,%al
call   1030 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11d8 <run+0x98>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rsi
xor    %edi,%edi
lea    0xe34(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
mov    0x2e01(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <execv@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x7f,%edi
lea    0xdf9(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     11c3 <run+0x83>
mov    %rax,%rbx
jne    11df <run+0x9f>
test   %ebp,%ebp
je     1188 <run+0x48>
xor    %eax,%eax
call   1030 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1188 <run+0x48>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rsi
lea    0xe86(%rip),%rdx
xor    %edi,%edi
xor    %eax,%eax
call   1040 <error@plt>
mov    0x2e51(%rip),%rbx
movq   $0x0,(%rbx)
mov    (%r14),%rdi
mov    %r14,%rsi
call   1050 <execv@plt>
mov    (%rbx),%rsi
mov    (%r14),%rcx
lea    0xe5e(%rip),%rdx
mov    $0x7f,%edi
xor    %eax,%eax
call   1040 <error@plt>
mov    $0xffffffffffffffff,%rbx
jmp    11df <run+0x9f>
mov    0x2e16(%rip),%rax
mov    (%rax),%rsi
lea    0xe2c(%rip),%rdx
xor    %ebx,%ebx
xor    %edi,%edi
xor    %eax,%eax
call   1040 <error@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [5 x i8] c"fork\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [3 x i8] c"%s\00"
define i64 @run(i8** %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i64
store i8** %0, i8*** %4
store i32 %1, i32* %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = call i32 (i32, i64, i8*, ...) @error(i32 0, i64 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
br label %39
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i64, i64* %6
store i64 %17, i64* %3
br label %39
18: ; preds = %13
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = call i32 (...) @setsid()
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = call i32 (i32, i64, i8*, ...) @error(i32 0, i64 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %21, %18
store i64 0, i64* @errno
%28 = load i8**, i8*** %4
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %4
%32 = call i32 @execv(i8* %30, i8** %31)
%33 = load i64, i64* @errno
%34 = load i8**, i8*** %4
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 (i32, i64, i8*, ...) @error(i32 127, i64 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %27
store i64 -1, i64* %3
br label %39
39: ; preds = %38, %16, %10
%40 = load i64, i64* %3
ret i64 %40
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [5 x i8] c"fork\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [3 x i8] c"%s\00"
define i64 @run(i8** %0, i32 %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = tail call i32 (i32, i64, i8*, ...) @error(i32 0, i64 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
8: ; preds = %2
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %24
10: ; preds = %8
%11 = icmp eq i32 %1, 0
br i1 %11, label %18, label %12
12: ; preds = %10
%13 = tail call i32 (...) @setsid()
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = tail call i32 (i32, i64, i8*, ...) @error(i32 0, i64 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %15, %12, %10
store i64 0, i64* @errno
%19 = load i8*, i8** %0
%20 = tail call i32 @execv(i8* %19, i8** %0)
%21 = load i64, i64* @errno
%22 = load i8*, i8** %0
%23 = tail call i32 (i32, i64, i8*, ...) @error(i32 127, i64 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %8, %18, %5
%25 = phi i64 [ 0, %5 ], [ -1, %18 ], [ %3, %8 ]
ret i64 %25
}