#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 /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void fork_new_child(void) {
  int old_pid = getpid();
  int pid;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(errno);
  }
  if (pid) {
    pause();
    exit(0);
  } else {
    fprintf(stderr, "Child %05d forked into pid %05d\n", old_pid, getpid());
    kill(old_pid, SIGUSR1);
  }
}


x86 assembly (clang, -O0):
<fork_new_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b0 <fork_new_child+0x40>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     11c8 <fork_new_child+0x58>
mov    $0x0,%al
call   1060 <pause@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%edx
mov    %eax,%ecx
lea    0xe15(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x4(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_new_child>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    11bf <fork_new_child+0x4f>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe67(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %eax,%ecx
call   1040 <fprintf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <kill@plt>
cmp    $0xffffffff,%eax
jne    11de <fork_new_child+0x6e>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1060 <pause@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [33 x i8] c"Child %05d forked into pid %05d\0A\00"
@SIGUSR1 = global i32 0
define void @fork_new_child() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getpid()
store i32 %3, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 (...) @pause()
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i32, i32* %1
%20 = call i32 (...) @getpid()
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %1
%23 = load i32, i32* @SIGUSR1
%24 = call i32 @kill(i32 %22, i32 %23)
br label %25
25: ; preds = %17
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [33 x i8] c"Child %05d forked into pid %05d\0A\00"
@SIGUSR1 = global i32 0
define void @fork_new_child() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @fork()
switch i32 %2, label %7 [
i32 -1, label %3
i32 0, label %10
]
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @errno
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i32 (...) @pause()
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %12)
%14 = load i32, i32* @SIGUSR1
%15 = tail call i32 @kill(i32 %1, i32 %14)
ret void
}