#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 fork();
int /*<<< orphan*/ printf(char *, ...);
void *foo(void *a) {
  pid_t p = fork();
  printf("Fork na watku %d \n", p);
  if (p == 0) {
    printf("Child process \n");
  } else if (p < 0) {
    printf("Failed fork \n");
  } else {
    printf("Parent process \n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1164 <foo+0x44>
lea    0xebb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1194 <foo+0x74>
cmpl   $0x0,-0xc(%rbp)
jge    1181 <foo+0x61>
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    118f <foo+0x6f>
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1194 <foo+0x74>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbx
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ebx
lea    0xebf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
lea    0xed0(%rip),%rax
lea    0xeb9(%rip),%rcx
cmovs  %rax,%rcx
lea    0xecb(%rip),%rdi
cmovne %rcx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Fork na watku %d \0A\00"
@.str.1 = [16 x i8] c"Child process \0A\00"
@.str.2 = [14 x i8] c"Failed fork \0A\00"
@.str.3 = [17 x i8] c"Parent process \0A\00"
define i8* @foo(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %19
11: ; preds = %1
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %18
16: ; preds = %11
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
br label %19
19: ; preds = %18, %9
ret i8* null
}

LLVM IR (-O2):
@.str = [19 x i8] c"Fork na watku %d \0A\00"
@str = [16 x i8] c"Parent process \00"
@str.4 = [13 x i8] c"Failed fork \00"
@str.5 = [15 x i8] c"Child process \00"
define noalias i8* @foo(i8* nocapture readnone %0) {
%2 = tail call i32 (...) @fork()
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %2, 0
%5 = icmp slt i32 %2, 0
%6 = select i1 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0)
%7 = select i1 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0), i8* %6
%8 = tail call i32 @puts(i8* dereferenceable(1) %7)
ret i8* null
}