#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*/ atoi(char *);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Specify the desired PID on the command-line\n");
    return 1;
  }
  pid_t wanted = (pid_t)atoi(argv[1]);
  int rc;
  int status;
  while ((rc = fork()) != wanted && rc != 0) {
    if (rc == -1) {
      printf("fork error\n");
      return 1;
    }
    if (wait(&status) == -1)
      printf("wait error\n");
  }
  if (rc == 0) {
    if (getpid() == wanted) {
      printf("Got pid %d, sleeping\n", wanted);
      sleep(200);
    }
    return 0;
  }
  wait(&status);
  return 0;
}


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)
je     119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1278 <main+0x118>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
je     11ce <main+0x6e>
cmpl   $0x0,-0x18(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11de <main+0x7e>
jmp    1227 <main+0xc7>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1202 <main+0xa2>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1278 <main+0x118>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
jne    1222 <main+0xc2>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11aa <main+0x4a>
cmpl   $0x0,-0x18(%rbp)
jne    1268 <main+0x108>
mov    $0x0,%al
call   1030 <getpid@plt>
cmp    -0x14(%rbp),%eax
jne    125c <main+0xfc>
mov    -0x14(%rbp),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xc8,%edi
call   1060 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <main+0x118>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11eb <main+0x7b>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    %ebx,%eax
je     11d9 <main+0x69>
test   %eax,%eax
je     11d9 <main+0x69>
lea    0x4(%rsp),%r14
lea    0xe74(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0xffffffff,%eax
je     121f <main+0xaf>
mov    %r14,%rdi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    11ca <main+0x5a>
mov    %r15,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    %ebx,%eax
je     11d9 <main+0x69>
test   %eax,%eax
jne    11b0 <main+0x40>
test   %eax,%eax
je     11f4 <main+0x84>
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
xor    %ebp,%ebp
jmp    1230 <main+0xc0>
lea    0xe3a(%rip),%rdi
jmp    1226 <main+0xb6>
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %ebx,%eax
jne    1230 <main+0xc0>
lea    0xdf8(%rip),%rdi
xor    %ebp,%ebp
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xc8,%edi
call   1070 <sleep@plt>
jmp    1230 <main+0xc0>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c"Specify the desired PID on the command-line\0A\00"
@.str.1 = [12 x i8] c"fork error\0A\00"
@.str.2 = [12 x i8] c"wait error\0A\00"
@.str.3 = [22 x i8] c"Got pid %d, sleeping\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %52
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %6
br label %18
18: ; preds = %37, %13
%19 = call i32 (...) @fork()
store i32 %19, i32* %7
%20 = load i32, i32* %6
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br label %25
25: ; preds = %22, %18
%26 = phi i1 [ false, %18 ], [ %24, %22 ]
br i1 %26, label %27, label %38
27: ; preds = %25
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %52
32: ; preds = %27
%33 = call i32 @wait(i32* %8)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
br label %18
38: ; preds = %25
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %38
%42 = call i32 (...) @getpid()
%43 = load i32, i32* %6
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* %6
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = call i32 @sleep(i32 200)
br label %49
49: ; preds = %45, %41
store i32 0, i32* %3
br label %52
50: ; preds = %38
%51 = call i32 @wait(i32* %8)
store i32 0, i32* %3
br label %52
52: ; preds = %50, %49, %30, %11
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str.3 = [22 x i8] c"Got pid %d, sleeping\0A\00"
@str = [11 x i8] c"wait error\00"
@str.4 = [11 x i8] c"fork error\00"
@str.5 = [44 x i8] c"Specify the desired PID on the command-line\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.5, i64 0, i64 0))
br label %44
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = bitcast i32* %3 to i8*
%12 = call i32 (...) @fork()
%13 = icmp ne i32 %12, %10
%14 = icmp ne i32 %12, 0
%15 = and i1 %13, %14
br i1 %15, label %16, label %31
16: ; preds = %7, %26
%17 = phi i32 [ %27, %26 ], [ %12, %7 ]
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
br label %42
21: ; preds = %16
%22 = call i32 @wait(i32* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = call i32 (...) @fork()
%28 = icmp ne i32 %27, %10
%29 = icmp ne i32 %27, 0
%30 = and i1 %28, %29
br i1 %30, label %16, label %31
31: ; preds = %26, %7
%32 = phi i32 [ %12, %7 ], [ %27, %26 ]
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = call i32 (...) @getpid()
%36 = icmp eq i32 %35, %10
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%39 = call i32 @sleep(i32 200)
br label %42
40: ; preds = %31
%41 = call i32 @wait(i32* %3)
br label %42
42: ; preds = %34, %37, %40, %19
%43 = phi i32 [ 1, %19 ], [ 0, %40 ], [ 0, %37 ], [ 0, %34 ]
br label %44
44: ; preds = %42, %5
%45 = phi i32 [ 1, %5 ], [ %43, %42 ]
ret i32 %45
}