#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 getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int up_sem_init(int, int);
scalar_t__ up_sem_p(int, int /*<<< orphan*/);
scalar_t__ up_sem_v(int, int /*<<< orphan*/);
int main() {
  printf("process is running (pid = %d)\n", getpid());
  int semid = up_sem_init(1, 1);
  printf("up_sem_init doen\n");
  if (up_sem_p(semid, 0) < 0) {
    do {
      perror("up_sem_p");
      return -1;
    } while (0);
  }
  printf("i will block 15 seconds, semid: %d\n", semid);
  sleep(15);
  if (up_sem_v(semid, 0) < 0) {
    do {
      perror("up_sem_v");
      return -1;
    } while (0);
  }
  printf("unblock sem, semid = %d\n", semid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%esi
mov    %esi,%edi
call   1070 <up_sem_init@plt>
mov    %eax,-0x8(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <up_sem_p@plt>
cmp    $0x0,%rax
jge    11e4 <main+0x74>
jmp    11cc <main+0x5c>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <main+0xd8>
mov    -0x8(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xf,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <up_sem_v@plt>
cmp    $0x0,%rax
jge    1230 <main+0xc0>
jmp    1218 <main+0xa8>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <main+0xd8>
mov    -0x8(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
mov    $0x1,%esi
call   1080 <up_sem_init@plt>
mov    %eax,%ebx
lea    0xebc(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <up_sem_p@plt>
test   %rax,%rax
js     1208 <main+0x88>
lea    0xe5c(%rip),%rdi
xor    %ebp,%ebp
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xf,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <up_sem_v@plt>
test   %rax,%rax
js     1211 <main+0x91>
lea    0xe5f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe10(%rip),%rdi
jmp    1218 <main+0x98>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11ff <main+0x7f>

LLVM IR (-O0):
@.str = [31 x i8] c"process is running (pid = %d)\0A\00"
@.str.1 = [18 x i8] c"up_sem_init doen\0A\00"
@.str.2 = [9 x i8] c"up_sem_p\00"
@.str.3 = [36 x i8] c"i will block 15 seconds, semid: %d\0A\00"
@.str.4 = [9 x i8] c"up_sem_v\00"
@.str.5 = [25 x i8] c"unblock sem, semid = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @up_sem_init(i32 1, i32 1)
store i32 %5, i32* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* %2
%8 = call i64 @up_sem_p(i32 %7, i32 0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
13: ; No predecessors!
br label %14
14: ; preds = %13, %0
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 @sleep(i32 15)
%18 = load i32, i32* %2
%19 = call i64 @up_sem_v(i32 %18, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
br label %22
22: ; preds = %21
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
24: ; No predecessors!
br label %25
25: ; preds = %24, %14
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %26)
store i32 0, i32* %1
br label %28
28: ; preds = %25, %22, %11
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [31 x i8] c"process is running (pid = %d)\0A\00"
@.str.2 = [9 x i8] c"up_sem_p\00"
@.str.3 = [36 x i8] c"i will block 15 seconds, semid: %d\0A\00"
@.str.4 = [9 x i8] c"up_sem_v\00"
@.str.5 = [25 x i8] c"unblock sem, semid = %d\0A\00"
@str = [17 x i8] c"up_sem_init doen\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 1)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%5 = tail call i64 @up_sem_p(i32 %3, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %18
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %3)
%11 = tail call i32 @sleep(i32 15)
%12 = tail call i64 @up_sem_v(i32 %3, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %18
16: ; preds = %9
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %3)
br label %18
18: ; preds = %16, %14, %7
%19 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %16 ]
ret i32 %19
}