#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 IPC_CREAT;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ ftok(char *, char);
int getpid();
int /*<<< orphan*/ init_sem(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sem_p(int);
int /*<<< orphan*/ sem_v(int);
int semget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sleep(int);
int main(void) {
  pid_t result;
  int sem_id;
  sem_id = semget(ftok(".", 'a'), 1, 0666 | IPC_CREAT);
  init_sem(sem_id, 0);
  result = fork();
  if (result == -1) {
    perror("Fork\n");
  } else if (result == 0) {
    printf("Child process will wait for some seconds... \n");
    sleep(3);
    printf("The returned value is %d in the child process(PID = %d)\n", result,
           getpid());
    sem_v(sem_id);
  } else {
    sem_p(sem_id);
    printf("The return value is %d in the father process(PID = %d)\n", result,
           getpid());
    sem_v(sem_id);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x61,%esi
call   10b0 <ftok@plt>
mov    %eax,%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1070 <init_sem@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121d <main+0x6d>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
jmp    12a1 <main+0xf1>
cmpl   $0x0,-0x8(%rbp)
jne    126c <main+0xbc>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <sem_v@plt>
jmp    129c <main+0xec>
mov    -0xc(%rbp),%edi
call   10a0 <sem_p@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <sem_v@plt>
jmp    12a1 <main+0xf1>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe36(%rip),%rdi
mov    $0x61,%esi
call   10c0 <ftok@plt>
mov    0x2e05(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1080 <init_sem@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
jne    1218 <main+0x58>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1246 <main+0x86>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdc6(%rip),%rdi
xor    %esi,%esi
jmp    125d <main+0x9d>
mov    %ebx,%edi
call   10b0 <sem_p@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xde6(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <sem_v@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@.str.1 = [6 x i8] c"Fork\0A\00"
@.str.2 = [46 x i8] c"Child process will wait for some seconds... \0A\00"
@.str.3 = [57 x i8] c"The returned value is %d in the child process(PID = %d)\0A\00"
@.str.4 = [56 x i8] c"The return value is %d in the father process(PID = %d)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext 97)
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 438, %5
%7 = call i32 @semget(i32 %4, i32 1, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @init_sem(i32 %8, i32 0)
%10 = call i32 (...) @fork()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %35
15: ; preds = %0
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @sleep(i32 3)
%21 = load i32, i32* %2
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %22)
%24 = load i32, i32* %3
%25 = call i32 @sem_v(i32 %24)
br label %34
26: ; preds = %15
%27 = load i32, i32* %3
%28 = call i32 @sem_p(i32 %27)
%29 = load i32, i32* %2
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i32, i32* %3
%33 = call i32 @sem_v(i32 %32)
br label %34
34: ; preds = %26, %18
br label %35
35: ; preds = %34, %13
%36 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@.str.1 = [6 x i8] c"Fork\0A\00"
@.str.3 = [57 x i8] c"The returned value is %d in the child process(PID = %d)\0A\00"
@.str.4 = [56 x i8] c"The return value is %d in the father process(PID = %d)\0A\00"
@str = [45 x i8] c"Child process will wait for some seconds... \00"
define i32 @main() {
%1 = tail call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext 97)
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 438
%4 = tail call i32 @semget(i32 %1, i32 1, i32 %3)
%5 = tail call i32 @init_sem(i32 %4, i32 0)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %15 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 3)
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %12)
%14 = tail call i32 @sem_v(i32 %4)
br label %20
15: ; preds = %0
%16 = tail call i32 @sem_p(i32 %4)
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i32 %6, i32 %17)
%19 = tail call i32 @sem_v(i32 %4)
br label %20
20: ; preds = %9, %15, %7
%21 = tail call i32 @exit(i32 0)
unreachable
}