#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
void *startproc(void *arg) {
  pid_t pid;
  char *argv[] = {"/bin/ls", "/bin/ls", NULL};
  if ((pid = fork()) == -1) {
    perror("fork error");
  } else if (pid == 0) {
    dup2(2, 1);
    execv(argv[0], argv);
  } else {
    *((pid_t *)arg) = pid;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<startproc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2cad(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2caa(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ca7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1191 <startproc+0x51>
lea    0xe81(%rip),%rdi
call   1040 <perror@plt>
jmp    11ca <startproc+0x8a>
cmpl   $0x0,-0xc(%rbp)
jne    11bc <startproc+0x7c>
mov    $0x2,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <execv@plt>
jmp    11c5 <startproc+0x85>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ca <startproc+0x8a>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startproc>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
mov    0x2cc1(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2ca5(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1173 <startproc+0x33>
cmp    $0xffffffff,%eax
je     1199 <startproc+0x59>
mov    %eax,(%rbx)
jmp    1191 <startproc+0x51>
mov    $0x2,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xe77(%rip),%rdi
mov    %rsp,%rsi
call   1050 <execv@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe68(%rip),%rdi
call   1040 <perror@plt>
jmp    1191 <startproc+0x51>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/ls\00"
@__const.startproc.argv = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [11 x i8] c"fork error\00"
define i8* @startproc(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [3 x i8*]
store i8* %0, i8** %2
%5 = bitcast [3 x i8*]* %4 to i8*
%6 = call i32 (...) @fork()
store i32 %6, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %24
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i32 @dup2(i32 2, i32 1)
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%16 = load i8*, i8** %15
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%18 = call i32 @execv(i8* %16, i8** %17)
br label %23
19: ; preds = %10
%20 = load i32, i32* %3
%21 = load i8*, i8** %2
%22 = bitcast i8* %21 to i32*
store i32 %20, i32* %22
br label %23
23: ; preds = %19, %13
br label %24
24: ; preds = %23, %8
ret i8* null
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/ls\00"
@__const.startproc.argv = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [11 x i8] c"fork error\00"
define noalias i8* @startproc(i8* nocapture writeonly %0) {
%2 = alloca [3 x i8*]
%3 = bitcast [3 x i8*]* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %11 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %13
7: ; preds = %1
%8 = tail call i32 @dup2(i32 2, i32 1)
%9 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%10 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %9)
br label %13
11: ; preds = %1
%12 = bitcast i8* %0 to i32*
store i32 %4, i32* %12
br label %13
13: ; preds = %7, %11, %5
ret i8* null
}