#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 /*<<< orphan*/ sem_t;

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int main() {
  sem_t *psem = sem_open("/uplooking", O_CREAT, 0666, 0);
  if (psem == SEM_FAILED) {
    perror("sem_open");
    goto err;
  }
  printf("pid %d will block,until sem_post by other process\n", getpid());
  sem_wait(psem);
  printf("pid %d  unblock\n", getpid());
  sem_close(psem);
  sem_unlink("/uplooking");
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jne    11c4 <main+0x54>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
jmp    1218 <main+0xa8>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <sem_close@plt>
lea    0xdf9(%rip),%rdi
call   1090 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    121f <main+0xaf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
xor    %ebp,%ebp
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e45(%rip),%rax
cmp    (%rax),%rbx
je     11ef <main+0x7f>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe66(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <sem_close@plt>
lea    0xe1f(%rip),%rdi
call   1090 <sem_unlink@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11e6 <main+0x76>

LLVM IR (-O0):
@.str = [11 x i8] c"/uplooking\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@.str.2 = [51 x i8] c"pid %d will block,until sem_post by other process\0A\00"
@.str.3 = [17 x i8] c"pid %d unblock\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = load i32, i32* @O_CREAT
%4 = call i32* @sem_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438, i32 0)
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = load i32*, i32** @SEM_FAILED
%7 = icmp eq i32* %5, %6
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %20
10: ; preds = %0
%11 = call i32 (...) @getpid()
%12 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %2
%14 = call i32 @sem_wait(i32* %13)
%15 = call i32 (...) @getpid()
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %2
%18 = call i32 @sem_close(i32* %17)
%19 = call i32 @sem_unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %21
20: ; preds = %8
store i32 -1, i32* %1
br label %21
21: ; preds = %20, %10
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [11 x i8] c"/uplooking\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@.str.2 = [51 x i8] c"pid %d will block,until sem_post by other process\0A\00"
@.str.3 = [17 x i8] c"pid %d unblock\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = tail call i32* @sem_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1, i32 438, i32 0)
%3 = load i32*, i32** @SEM_FAILED
%4 = icmp eq i32* %2, %3
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %15
7: ; preds = %0
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = tail call i32 @sem_wait(i32* %2)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sem_close(i32* %2)
%14 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %5, %7
%16 = phi i32 [ -1, %5 ], [ 0, %7 ]
ret i32 %16
}