#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 WEXITSTATUS(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ execve(char *, char **, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid, exit_pid;
  int status;
  char *dummy_environ[] = {NULL};
  if (argc < 2) {
    printf("please some command args\n");
    return -1;
  }
  if (!(pid = fork())) {
    execve(argv[1], &argv[1], dummy_environ);
    perror("execve");
  } else {
    printf("forked pid = %d\n", pid);
    while (1) {
      if ((exit_pid = waitpid(0, &status, WNOHANG)) > 0)
        break;
      printf("waiting for any pid to quit\n");
      sleep(1);
    }
    printf("pid %d exited with value = %d\n", exit_pid, WEXITSTATUS(status));
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11ca <main+0x4a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <main+0x10e>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1207 <main+0x87>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
lea    -0x28(%rbp),%rdx
call   1050 <execve@plt>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
jmp    1287 <main+0x107>
mov    -0x14(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    123d <main+0xbd>
jmp    125a <main+0xda>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1218 <main+0x98>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128e <main+0x10e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
cmp    $0x1,%edi
jg     11b1 <main+0x31>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    1251 <main+0xd1>
mov    %rsi,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1261 <main+0xe1>
lea    0xe3a(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e08(%rip),%r12
mov    (%r12),%edx
lea    0x4(%rsp),%rsi
xor    %edi,%edi
call   1070 <waitpid@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1236 <main+0xb6>
lea    0xe3a(%rip),%r15
lea    0x4(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    (%r12),%edx
xor    %edi,%edi
mov    %rbp,%rsi
call   1070 <waitpid@plt>
test   %eax,%eax
jle    1210 <main+0x90>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xdd2(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <execve@plt>
lea    0xd83(%rip),%rdi
call   1080 <perror@plt>
jmp    1251 <main+0xd1>

LLVM IR (-O0):
@.str = [26 x i8] c"please some command args\0A\00"
@.str.1 = [7 x i8] c"execve\00"
@.str.2 = [17 x i8] c"forked pid = %d\0A\00"
@WNOHANG = global i32 0
@.str.3 = [29 x i8] c"waiting for any pid to quit\0A\00"
@.str.4 = [31 x i8] c"pid %d exited with value = %d\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 [1 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [1 x i8*]* %9 to i8*
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
15: ; preds = %2
%16 = call i32 (...) @fork()
store i32 %16, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = getelementptr inbounds [1 x i8*], [1 x i8*]* %9, i64 0, i64 0
%25 = call i32 @execve(i8* %21, i8** %23, i8** %24)
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %43
27: ; preds = %15
%28 = load i32, i32* %6
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %27, %35
%31 = load i32, i32* @WNOHANG
%32 = call i32 @waitpid(i32 0, i32* %8, i32 %31)
store i32 %32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
br label %38
35: ; preds = %30
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @sleep(i32 1)
br label %30
38: ; preds = %34
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %41)
store i32 0, i32* %3
br label %44
43: ; preds = %18
store i32 0, i32* %3
br label %44
44: ; preds = %43, %38, %13
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"execve\00"
@.str.2 = [17 x i8] c"forked pid = %d\0A\00"
@WNOHANG = global i32 0
@.str.4 = [31 x i8] c"pid %d exited with value = %d\0A\00"
@str = [28 x i8] c"waiting for any pid to quit\00"
@str.5 = [25 x i8] c"please some command args\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca [1 x i8*]
%5 = bitcast i32* %3 to i8*
%6 = bitcast [1 x i8*]* %4 to i8*
%7 = bitcast [1 x i8*]* %4 to i64*
store i64 0, i64* %7
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %36
11: ; preds = %2
%12 = tail call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds [1 x i8*], [1 x i8*]* %4, i64 0, i64 0
%18 = call i32 @execve(i8* %16, i8** %15, i8** %17)
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %36
20: ; preds = %11
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%22 = load i32, i32* @WNOHANG
%23 = call i32 @waitpid(i32 0, i32* %3, i32 %22)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %20, %25
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%27 = call i32 @sleep(i32 1)
%28 = load i32, i32* @WNOHANG
%29 = call i32 @waitpid(i32 0, i32* %3, i32 %28)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %25
31: ; preds = %25, %20
%32 = phi i32 [ %23, %20 ], [ %29, %25 ]
%33 = load i32, i32* %3
%34 = call i32 @WEXITSTATUS(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %34)
br label %36
36: ; preds = %14, %31, %9
%37 = phi i32 [ -1, %9 ], [ 0, %31 ], [ 0, %14 ]
ret i32 %37
}