#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*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getpgrp();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "usage: %s program [arg ...]\n", (char *)argv[0]);
  }
  if (getpgrp() == getpid()) {
    switch (fork()) {
    case 0:
      break;
    case -1:
      perror("fork");
      exit(1);
    default:
      exit(0);
    }
  }
  if (setsid() < 0) {
    perror("setsid");
    exit(1);
  }
  execvp(argv[1], argv + 1);
  perror("execvp");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11bc <main+0x3c>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1060 <getpgrp@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
jne    122f <main+0xaf>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     120d <main+0x8d>
jmp    11f8 <main+0x78>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
jne    1223 <main+0xa3>
jmp    1208 <main+0x88>
jmp    122a <main+0xaa>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    122f <main+0xaf>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    1256 <main+0xd6>
lea    0xddb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
lea    0xdb7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     11a4 <main+0x24>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1060 <getpgrp@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %rax,%rbx
jne    11d3 <main+0x53>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11d3 <main+0x53>
cmp    $0xffffffff,%eax
jne    120e <main+0x8e>
lea    0xe4c(%rip),%rdi
jmp    11ff <main+0x7f>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
jns    11e8 <main+0x68>
lea    0xe3c(%rip),%rdi
jmp    11ff <main+0x7f>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   1080 <execvp@plt>
lea    0xe2a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s program [arg ...]\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %8, %2
%15 = call i64 (...) @getpgrp()
%16 = call i64 (...) @getpid()
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = call i32 (...) @fork()
switch i32 %19, label %24 [
i32 0, label %20
i32 -1, label %21
]
20: ; preds = %18
br label %26
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %20
br label %27
27: ; preds = %26, %14
%28 = call i64 (...) @setsid()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = call i32 @execvp(i8* %36, i8** %38)
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s program [arg ...]\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %4, %2
%9 = tail call i64 (...) @getpgrp()
%10 = tail call i64 (...) @getpid()
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = tail call i32 (...) @fork()
switch i32 %13, label %17 [
i32 0, label %19
i32 -1, label %14
]
14: ; preds = %12
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %12
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %12, %8
%20 = tail call i64 (...) @setsid()
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @execvp(i8* %27, i8** %26)
%29 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
}