#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 errno;
int /*<<< orphan*/ printf(char *, int, ...);
int sem_close(int /*<<< orphan*/ *);
int sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
int sem_post(int /*<<< orphan*/ *);
int sem_wait(int /*<<< orphan*/ *);
int main(void) {
  int rc;
  int sem_value = -99;
  sem_t *pSemaphore = NULL;
  pSemaphore = sem_open("/my_semaphore", O_CREAT, 0600, 0);
  if (pSemaphore == SEM_FAILED)
    printf("Creating the semaphore failed; errno is %d\n", errno);
  rc = sem_post(pSemaphore);
  if (rc)
    printf("Releasing the semaphore failed; errno is %d\n", errno);
  rc = sem_getvalue(pSemaphore, &sem_value);
  printf("sem_getvalue() returned %d, the semaphore's value is %d\n", rc,
         sem_value);
  rc = sem_wait(pSemaphore);
  if (rc)
    printf("Acquiring the semaphore failed; errno is %d\n", errno);
  rc = sem_close(pSemaphore);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffff9d,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
mov    $0x180,%edx
xor    %ecx,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    11c9 <main+0x69>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <sem_post@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11f6 <main+0x96>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <sem_getvalue@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1247 <main+0xe7>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <sem_close@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0xffffff9d,0xc(%rsp)
ff
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
mov    $0x180,%edx
xor    %ecx,%ecx
call   1070 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e4d(%rip),%rax
cmp    (%rax),%rbx
jne    11af <main+0x4f>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
test   %eax,%eax
je     11d2 <main+0x72>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <sem_getvalue@plt>
mov    0xc(%rsp),%edx
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %eax,%eax
je     1216 <main+0xb6>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1080 <sem_close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/my_semaphore\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [44 x i8] c"Creating the semaphore failed; errno is %d\0A\00"
@errno = global i32 0
@.str.2 = [45 x i8] c"Releasing the semaphore failed; errno is %d\0A\00"
@.str.3 = [57 x i8] c"sem_getvalue() returned %d, the semaphore's value is %d\0A\00"
@.str.4 = [45 x i8] c"Acquiring the semaphore failed; errno is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 -99, i32* %3
store i32* null, i32** %4
%5 = load i32, i32* @O_CREAT
%6 = call i32* @sem_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5, i32 384, i32 0)
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = load i32*, i32** @SEM_FAILED
%9 = icmp eq i32* %7, %8
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %0
%14 = load i32*, i32** %4
%15 = call i32 @sem_post(i32* %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %13
%22 = load i32*, i32** %4
%23 = call i32 @sem_getvalue(i32* %22, i32* %3)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = load i32, i32* %3
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32*, i32** %4
%28 = call i32 @sem_wait(i32* %27)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = load i32, i32* @errno
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %21
%35 = load i32*, i32** %4
%36 = call i32 @sem_close(i32* %35)
store i32 %36, i32* %2
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"/my_semaphore\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [44 x i8] c"Creating the semaphore failed; errno is %d\0A\00"
@errno = global i32 0
@.str.2 = [45 x i8] c"Releasing the semaphore failed; errno is %d\0A\00"
@.str.3 = [57 x i8] c"sem_getvalue() returned %d, the semaphore's value is %d\0A\00"
@.str.4 = [45 x i8] c"Acquiring the semaphore failed; errno is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 -99, i32* %1
%3 = load i32, i32* @O_CREAT
%4 = tail call i32* @sem_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3, i32 384, i32 0)
%5 = load i32*, i32** @SEM_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %7, %0
%11 = tail call i32 @sem_post(i32* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13, %10
%17 = call i32 @sem_getvalue(i32* %4, i32* %1)
%18 = load i32, i32* %1
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %18)
%20 = call i32 @sem_wait(i32* %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = load i32, i32* @errno
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %16
%26 = call i32 @sem_close(i32* %4)
ret i32 0
}