#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 */
scalar_t__ WCOREDUMP(int);
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int /*<<< orphan*/ pause();
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  int ret, code;
  if ((pid = fork()) == 0) {
    fprintf(stderr, "[FILS] Mon PID : %d\n", getpid());
    if ((argc == 1) || (sscanf(argv[1], "%d", &ret) != 1))
      pause();
    exit(ret);
  } else {
    waitpid(-1, &code, 0);
    if (WIFEXITED(code)) {
      fprintf(stderr, "[PERE] Mon fils %d termine avec code %d\n", pid,
              WEXITSTATUS(code));
    } else if (WIFSIGNALED(code)) {
      fprintf(stderr, "[PERE] Mon fils %d tue par signal %d ", pid,
              WTERMSIG(code));
      if (WCOREDUMP(code))
        fprintf(stderr, "(core dump)");
      fprintf(stderr, "\n");
    }
  }
  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)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1249 <main+0x89>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x1,-0x8(%rbp)
je     123a <main+0x7a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xded(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     1241 <main+0x81>
mov    $0x0,%al
call   1070 <pause@plt>
mov    -0x18(%rbp),%edi
call   10d0 <exit@plt>
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12a0 <main+0xe0>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1327 <main+0x167>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1322 <main+0x162>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <WCOREDUMP@plt>
cmp    $0x0,%rax
je     130b <main+0x14b>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xcf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1327 <main+0x167>
jmp    132c <main+0x16c>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1295 <main+0xd5>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    0x8(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     1225 <main+0x65>
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xe02(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    128a <main+0xca>
mov    0x8(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     128a <main+0xca>
mov    0x2da6(%rip),%r14
mov    (%r14),%ebp
mov    0x8(%rsp),%edi
call   1080 <WTERMSIG@plt>
lea    0xdf4(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x8(%rsp),%edi
call   1040 <WCOREDUMP@plt>
test   %rax,%rax
je     1279 <main+0xb9>
mov    (%r14),%edi
lea    0xdf5(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
lea    0xd90(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d44(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd54(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x1,%ebp
je     12d6 <main+0x116>
mov    0x8(%r14),%rdi
lea    0xd4e(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     12dd <main+0x11d>
xor    %eax,%eax
call   1070 <pause@plt>
mov    0xc(%rsp),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"[FILS] Mon PID : %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [41 x i8] c"[PERE] Mon fils %d termine avec code %d\0A\00"
@.str.3 = [38 x i8] c"[PERE] Mon fils %d tue par signal %d \00"
@.str.4 = [12 x i8] c"(core dump)\00"
@.str.5 = [2 x i8] c"\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 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (...) @getpid()
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %17, %11
%24 = call i32 (...) @pause()
br label %25
25: ; preds = %23, %17
%26 = load i32, i32* %7
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
%29 = call i32 @waitpid(i32 -1, i32* %8, i32 0)
%30 = load i32, i32* %8
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = load i32, i32* %6
%36 = load i32, i32* %8
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %37)
br label %59
39: ; preds = %28
%40 = load i32, i32* %8
%41 = call i64 @WIFSIGNALED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %58
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i32, i32* %6
%46 = load i32, i32* %8
%47 = call i32 @WTERMSIG(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 %47)
%49 = load i32, i32* %8
%50 = call i64 @WCOREDUMP(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %43
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %52, %43
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %55, %39
br label %59
59: ; preds = %58, %33
br label %60
60: ; preds = %59
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"[FILS] Mon PID : %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [41 x i8] c"[PERE] Mon fils %d termine avec code %d\0A\00"
@.str.3 = [38 x i8] c"[PERE] Mon fils %d tue par signal %d \00"
@.str.4 = [12 x i8] c"(core dump)\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %0, 1
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%18 = icmp eq i32 %17, 1
br i1 %18, label %21, label %19
19: ; preds = %14, %9
%20 = call i32 (...) @pause()
br label %21
21: ; preds = %19, %14
%22 = load i32, i32* %3
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = call i32 @waitpid(i32 -1, i32* %4, i32 0)
%26 = load i32, i32* %4
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i32, i32* %4
%32 = call i32 @WEXITSTATUS(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %7, i32 %32)
br label %52
34: ; preds = %24
%35 = load i32, i32* %4
%36 = call i64 @WIFSIGNALED(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %52, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i32, i32* %4
%41 = call i32 @WTERMSIG(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 %41)
%43 = load i32, i32* %4
%44 = call i64 @WCOREDUMP(i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %38
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %46, %38
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %29, %49, %34
ret i32 0
}