#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_RDWR;
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
scalar_t__ sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char const *, int /*<<< orphan*/);
int sem_value(const char *sem_name) {
  int value = -1;
  sem_t *sp = sem_open(sem_name, O_RDWR);
  int rv = -1;
  if (sp != NULL) {
    if (sem_getvalue(sp, &value) == 0) {
      printf("%d\n", value);
      rv = 0;
    }
    sem_close(sp);
  }
  return rv;
}


x86 assembly (clang, -O0):
<sem_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1050 <sem_open@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b1 <sem_value+0x71>
mov    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <sem_getvalue@plt>
cmp    $0x0,%rax
jne    11a8 <sem_value+0x68>
mov    -0xc(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <sem_close@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_value>:
push   %rbp
push   %rbx
push   %rax
movl   $0xffffffff,0x4(%rsp)
ff
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <sem_open@plt>
test   %rax,%rax
je     1194 <sem_value+0x54>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   1040 <sem_getvalue@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    118a <sem_value+0x4a>
mov    0x4(%rsp),%esi
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1060 <sem_close@plt>
jmp    1199 <sem_value+0x59>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @sem_value(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = call i32* @sem_open(i8* %6, i32 %7)
store i32* %8, i32** %4
store i32 -1, i32* %5
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %21
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i64 @sem_getvalue(i32* %12, i32* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 0, i32* %5
br label %18
18: ; preds = %15, %11
%19 = load i32*, i32** %4
%20 = call i32 @sem_close(i32* %19)
br label %21
21: ; preds = %18, %1
%22 = load i32, i32* %5
ret i32 %22
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @sem_value(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%4 = load i32, i32* @O_RDWR
%5 = tail call i32* @sem_open(i8* %0, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %16, label %7
7: ; preds = %1
%8 = call i64 @sem_getvalue(i32* %5, i32* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* %2
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %7
%14 = phi i32 [ 0, %10 ], [ -1, %7 ]
%15 = call i32 @sem_close(i32* %5)
br label %16
16: ; preds = %13, %1
%17 = phi i32 [ %14, %13 ], [ -1, %1 ]
ret i32 %17
}