#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*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int up_sem_init(int, int /*<<< orphan*/);
int /*<<< orphan*/ up_sem_p(int, int /*<<< orphan*/);
int /*<<< orphan*/ up_sem_v(int, int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  printf("process is running (pid = %d)\n", getpid());
  int semid = up_sem_init(1, 0);
  pid_t pid = fork();
  if (pid < 0) {
    do {
      perror("fork");
      exit(-1);
    } while (0);
  } else if (pid == 0) {
    printf("child ----pid = %d, ppid = %d\n", getpid(), getppid());
    up_sem_p(semid, 0);
    printf("child unblock\n");
    up_sem_v(semid, 0);
    printf("child done\n");
    exit(0);
  }
  printf("parent --- pid = %d, childid = %d\n", getpid(), pid);
  printf("parent will realase resouce after 5 seconds\n");
  sleep(5);
  up_sem_v(semid, 0);
  wait(NULL);
  printf("parent clean done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <up_sem_init@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1216 <main+0x66>
jmp    1200 <main+0x50>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    127c <main+0xcc>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <up_sem_p@plt>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <up_sem_v@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1281 <main+0xd1>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <up_sem_v@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
xor    %esi,%esi
call   1080 <up_sem_init@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     1254 <main+0x94>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     126a <main+0xaa>
lea    0xe35(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe45(%rip),%rdi
call   1050 <puts@plt>
mov    $0x5,%edi
call   10c0 <sleep@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <up_sem_v@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
lea    0xe4a(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
lea    0xdac(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1030 <up_sem_p@plt>
lea    0xe10(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <up_sem_v@plt>
lea    0xe08(%rip),%rdi
call   1050 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"process is running (pid = %d)\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [31 x i8] c"child ----pid = %d, ppid = %d\0A\00"
@.str.3 = [15 x i8] c"child unblock\0A\00"
@.str.4 = [12 x i8] c"child done\0A\00"
@.str.5 = [35 x i8] c"parent --- pid = %d, childid = %d\0A\00"
@.str.6 = [45 x i8] c"parent will realase resouce after 5 seconds\0A\00"
@.str.7 = [19 x i8] c"parent clean done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = call i32 (...) @getpid()
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @up_sem_init(i32 1, i32 0)
store i32 %6, i32* %2
%7 = call i64 (...) @fork()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; No predecessors!
br label %30
15: ; preds = %0
%16 = load i64, i64* %3
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @up_sem_p(i32 %22, i32 0)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @up_sem_v(i32 %25, i32 0)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %15
br label %30
30: ; preds = %29, %14
%31 = call i32 (...) @getpid()
%32 = load i64, i64* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %31, i64 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%35 = call i32 @sleep(i32 5)
%36 = load i32, i32* %2
%37 = call i32 @up_sem_v(i32 %36, i32 0)
%38 = call i32 @wait(i32* null)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [31 x i8] c"process is running (pid = %d)\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [31 x i8] c"child ----pid = %d, ppid = %d\0A\00"
@.str.5 = [35 x i8] c"parent --- pid = %d, childid = %d\0A\00"
@str = [44 x i8] c"parent will realase resouce after 5 seconds\00"
@str.8 = [18 x i8] c"parent clean done\00"
@str.9 = [14 x i8] c"child unblock\00"
@str.10 = [11 x i8] c"child done\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 @up_sem_init(i32 1, i32 0)
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %4, 0
%11 = tail call i32 (...) @getpid()
br i1 %10, label %12, label %20
12: ; preds = %9
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %13)
%15 = tail call i32 @up_sem_p(i32 %3, i32 0)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%17 = tail call i32 @up_sem_v(i32 %3, i32 0)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.10, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %9
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %11, i64 %4)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @sleep(i32 5)
%24 = tail call i32 @up_sem_v(i32 %3, i32 0)
%25 = tail call i32 @wait(i32* null)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}