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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe1a(%rip),%rdi
mov    $0x61,%esi
call   10c0 <ftok@plt>
mov    %eax,%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1070 <init_sem@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1240 <main+0x70>
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
jmp    1313 <main+0x143>
cmpl   $0x0,-0x8(%rbp)
jne    12af <main+0xdf>
mov    -0xc(%rbp),%edi
call   10b0 <sem_p@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%edx
mov    %eax,%ecx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1060 <sem_v@plt>
jmp    130e <main+0x13e>
cmpl   $0x0,-0x8(%rbp)
jle    1309 <main+0x139>
mov    -0xc(%rbp),%edi
call   10b0 <sem_p@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1060 <sem_v@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
mov    -0xc(%rbp),%edi
call   1050 <del_sem@plt>
jmp    130e <main+0x13e>
jmp    1313 <main+0x143>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe25(%rip),%rdi
mov    $0x61,%esi
call   10c0 <ftok@plt>
mov    0x2dec(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1070 <init_sem@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
jne    122c <main+0x5c>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1287 <main+0xb7>
mov    %ebx,%edi
call   10b0 <sem_p@plt>
mov    0x2da0(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xdc4(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    0x0(%rbp),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdcf(%rip),%rsi
mov    %ebp,%edi
xor    %edx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1060 <sem_v@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jle    12cb <main+0xfb>
mov    %ebx,%edi
call   10b0 <sem_p@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdcc(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1060 <sem_v@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
mov    %ebx,%edi
call   1050 <del_sem@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@.str.1 = [12 x i8] c"fork failed\00"
@stdout = global i32 0
@.str.2 = [45 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 = [58 x i8] c"The returned value is %d in the parent 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 1)
%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 ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %47
15: ; preds = %0
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i32, i32* %3
%20 = call i32 @sem_p(i32 %19)
%21 = load i32, i32* @stdout
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @sleep(i32 3)
%24 = load i32, i32* @stdout
%25 = load i32, i32* %2
%26 = call i32 (...) @getpid()
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %26)
%28 = load i32, i32* %3
%29 = call i32 @sem_v(i32 %28)
br label %46
30: ; preds = %15
%31 = load i32, i32* %2
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = load i32, i32* %3
%35 = call i32 @sem_p(i32 %34)
%36 = load i32, i32* @stdout
%37 = load i32, i32* %2
%38 = call i32 (...) @getpid()
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32 %37, i32 %38)
%40 = load i32, i32* %3
%41 = call i32 @sem_v(i32 %40)
%42 = call i32 @wait(i32* null)
%43 = load i32, i32* %3
%44 = call i32 @del_sem(i32 %43)
br label %45
45: ; preds = %33, %30
br label %46
46: ; preds = %45, %18
br label %47
47: ; preds = %46, %13
%48 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@.str.1 = [12 x i8] c"fork failed\00"
@stdout = global i32 0
@.str.2 = [45 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 = [58 x i8] c"The returned value is %d in the parent process(PID = %d)\0A\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 1)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %18 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %28
9: ; preds = %0
%10 = tail call i32 @sem_p(i32 %4)
%11 = load i32, i32* @stdout
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 3)
%14 = load i32, i32* @stdout
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %15)
%17 = tail call i32 @sem_v(i32 %4)
br label %28
18: ; preds = %0
%19 = icmp sgt i32 %6, 0
br i1 %19, label %20, label %28
20: ; preds = %18
%21 = tail call i32 @sem_p(i32 %4)
%22 = load i32, i32* @stdout
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32 %6, i32 %23)
%25 = tail call i32 @sem_v(i32 %4)
%26 = tail call i32 @wait(i32* null)
%27 = tail call i32 @del_sem(i32 %4)
br label %28
28: ; preds = %9, %20, %18, %7
%29 = tail call i32 @exit(i32 0)
unreachable
}