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

/* Variables and functions */
int /*<<< orphan*/ SIGINT;
scalar_t__ fork();
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  pid_t pid;
  int i = 0;
  pid = fork();
  if (pid < 0) {
    perror("fork");
  }
  if (pid == 0) {
    while (1) {
      printf("I am son\n");
      sleep(1);
    }
  } else if (pid > 0) {
    while (1) {
      printf("I am father\n");
      sleep(1);
      i++;
      if (3 == i) {
        kill(pid, SIGINT);
      }
    }
  }
  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)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    118f <main+0x3f>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11ba <main+0x6a>
jmp    119f <main+0x4f>
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    119f <main+0x4f>
cmpq   $0x0,-0x18(%rbp)
jle    120e <main+0xbe>
jmp    11ca <main+0x7a>
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x3,%eax
cmp    -0x1c(%rbp),%eax
jne    1209 <main+0xb9>
mov    -0x18(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
jmp    11ca <main+0x7a>
jmp    1213 <main+0xc3>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11c4 <main+0x74>
je     11a4 <main+0x54>
mov    %rax,%r14
mov    $0x2,%ebp
lea    0xe91(%rip),%rbx
mov    0x2e6d(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
sub    $0x1,%ebp
jae    1180 <main+0x30>
mov    (%r15),%esi
mov    %r14,%rdi
call   1040 <kill@plt>
jmp    1180 <main+0x30>
lea    0xe66(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11b0 <main+0x60>
lea    0xe35(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"I am son\0A\00"
@.str.2 = [13 x i8] c"I am father\0A\00"
@SIGINT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
br label %17
17: ; preds = %16, %17
%18 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @sleep(i32 1)
br label %17
20: ; preds = %13
%21 = load i64, i64* %6
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %36
23: ; preds = %20
br label %24
24: ; preds = %23, %35
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @sleep(i32 1)
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 3, %29
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i64, i64* %6
%33 = load i32, i32* @SIGINT
%34 = call i32 @kill(i64 %32, i32 %33)
br label %35
35: ; preds = %31, %24
br label %24
36: ; preds = %20
br label %37
37: ; preds = %36
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@SIGINT = global i32 0
@str = [12 x i8] c"I am father\00"
@str.3 = [9 x i8] c"I am son\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %20, label %5
5: ; preds = %2
%6 = icmp eq i64 %3, 0
br i1 %6, label %7, label %10
7: ; preds = %5, %7
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.3, i64 0, i64 0))
%9 = tail call i32 @sleep(i32 1)
br label %7
10: ; preds = %5, %19
%11 = phi i32 [ %14, %19 ], [ 0, %5 ]
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 1)
%14 = add nuw nsw i32 %11, 1
%15 = icmp eq i32 %14, 3
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* @SIGINT
%18 = tail call i32 @kill(i64 %3, i32 %17)
br label %19
19: ; preds = %16, %10
br label %10
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
ret i32 0
}