#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int sem_wait(int /*<<< orphan*/ *);
int main() {
  sem_t *sem;
  sem = sem_open("/mysem", 0);
  if (sem == SEM_FAILED) {
    perror("sem_open");
    exit(errno);
  };
  if (sem_wait(sem) == -1) {
    perror("sem_wait");
    exit(errno);
  };
  if (sem_wait(sem) == -1) {
    perror("sem_wait");
    exit(errno);
  };
  printf("%ld sem_wait() succeeded\n", (long)getpid());
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <main+0x4f>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    11db <main+0x7b>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    1207 <main+0xa7>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe98(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e6f(%rip),%rax
cmp    (%rax),%rbx
jne    1187 <main+0x27>
lea    0xe82(%rip),%rdi
jmp    11a8 <main+0x48>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     11a1 <main+0x41>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    11bb <main+0x5b>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe50(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"/mysem\00"
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@errno = global i32 0
@.str.2 = [9 x i8] c"sem_wait\00"
@.str.3 = [26 x i8] c"%ld sem_wait() succeeded\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %3, i32** %2
%4 = load i32*, i32** %2
%5 = load i32*, i32** @SEM_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32*, i32** %2
%13 = call i32 @sem_wait(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i32*, i32** %2
%21 = call i32 @sem_wait(i32* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @errno
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = call i64 (...) @getpid()
%29 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %28)
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"/mysem\00"
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@errno = global i32 0
@.str.2 = [9 x i8] c"sem_wait\00"
@.str.3 = [26 x i8] c"%ld sem_wait() succeeded\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
%2 = load i32*, i32** @SEM_FAILED
%3 = icmp eq i32* %1, %2
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @errno
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @sem_wait(i32* %1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = tail call i32 @sem_wait(i32* %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @errno
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
%23 = tail call i64 (...) @getpid()
%24 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %23)
%25 = load i32, i32* @EXIT_SUCCESS
%26 = tail call i32 @exit(i32 %25)
unreachable
}