#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*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int main() {
  int value;
  sem_t *sem;
  sem = sem_open("/mysem", 0);
  if (sem == SEM_FAILED) {
    perror("sem_open");
    exit(errno);
  };
  if (sem_getvalue(sem, &value) == -1) {
    perror("sem_getvalue");
    exit(errno);
  };
  printf("%d\n", value);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
xor    %esi,%esi
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <main+0x4f>
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    11cf <main+0x7f>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe44(%rip),%rdi
call   1030 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xea8(%rip),%rdi
xor    %esi,%esi
call   1050 <sem_open@plt>
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
jne    1174 <main+0x24>
lea    0xe95(%rip),%rdi
jmp    118d <main+0x3d>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   1040 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    11a0 <main+0x50>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x4(%rsp),%esi
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <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 = [13 x i8] c"sem_getvalue\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = load i32*, i32** @SEM_FAILED
%7 = icmp eq i32* %5, %6
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @errno
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32*, i32** %3
%14 = call i32 @sem_getvalue(i32* %13, i32* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
%21 = load i32, i32* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
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 = [13 x i8] c"sem_getvalue\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = load i32*, i32** @SEM_FAILED
%5 = icmp eq i32* %3, %4
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @errno
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = call i32 @sem_getvalue(i32* %3, i32* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = load i32, i32* %1
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
}