#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 /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int /*<<< orphan*/,
                             int);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  sem_t *sem;
  int val;
  sem = sem_open("/Austin", O_CREAT, S_IRWXU, 2);
  if (sem == SEM_FAILED) {
    perror("sem_open fail");
    exit(0);
  }
  sem_getvalue(sem, &val);
  printf("sem value:%d\n", val);
  sem_wait(sem);
  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)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
lea    0xe71(%rip),%rdi
mov    $0x2,%ecx
call   1060 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11c4 <main+0x64>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1050 <sem_getvalue@plt>
mov    -0x1c(%rbp),%esi
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
lea    0xe82(%rip),%rdi
mov    $0x2,%ecx
call   1060 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e46(%rip),%rax
cmp    (%rax),%rbx
je     11c4 <main+0x64>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <sem_getvalue@plt>
mov    0xc(%rsp),%esi
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/Austin\00"
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [14 x i8] c"sem_open fail\00"
@.str.2 = [14 x i8] c"sem value:%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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @S_IRWXU
%10 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9, i32 2)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = load i32*, i32** @SEM_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = call i32 @sem_getvalue(i32* %18, i32* %7)
%20 = load i32, i32* %7
%21 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32*, i32** %6
%23 = call i32 @sem_wait(i32* %22)
ret i32 0
}

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