#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 WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main() {
  int pid, npid;
  int status;
  if ((pid = fork()) == -1) {
    printf("error\n");
  } else if (pid == 0) {
    printf("I am child, And pid is %d\n", getpid());
    sleep(1);
    exit(7);
  } else {
    int t = wait(&status);
    printf("%d\n", t);
    printf("I am father,and status is %d\n", WTERMSIG(status));
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11a5 <main+0x35>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1214 <main+0xa4>
cmpl   $0x0,-0x8(%rbp)
jne    11da <main+0x6a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x7,%edi
call   1060 <exit@plt>
lea    -0x10(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1214 <main+0xa4>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11a4 <main+0x24>
cmp    $0xffffffff,%eax
jne    11cf <main+0x4f>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x7,%edi
call   1070 <exit@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"error\0A\00"
@.str.1 = [27 x i8] c"I am child, And pid is %d\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [30 x i8] c"I am father,and status is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %0
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @sleep(i32 1)
%17 = call i32 @exit(i32 7)
unreachable
18: ; preds = %10
%19 = call i32 @wait(i32* %4)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %4
%23 = call i32 @WTERMSIG(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %18
br label %26
26: ; preds = %25, %8
%27 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [27 x i8] c"I am child, And pid is %d\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [30 x i8] c"I am father,and status is %d\0A\00"
@str = [6 x i8] c"error\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %11 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %17
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i32 @sleep(i32 1)
%10 = tail call i32 @exit(i32 7)
unreachable
11: ; preds = %0
%12 = call i32 @wait(i32* %1)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %1
%15 = call i32 @WTERMSIG(i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %11, %4
%18 = call i32 @exit(i32 0)
unreachable
}