#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*/ *SEM_FAILED;
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 argc, char **argv) {
  sem_t *sem;
  int ret;
  int sval;
  sem = sem_open("/sem_test", 0);
  if (sem == SEM_FAILED) {
    perror("sem_open");
    return -1;
  }
  ret = sem_getvalue(sem, &sval);
  printf("sval=%d\n", sval);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
xor    %esi,%esi
call   1050 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <main+0x54>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <main+0x7a>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <sem_getvalue@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
call   1050 <sem_open@plt>
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%rax
je     1186 <main+0x46>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
call   1040 <sem_getvalue@plt>
mov    0xc(%rsp),%esi
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    117e <main+0x3e>

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

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