#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getpid();
int /*<<< orphan*/ pause();
int /*<<< orphan*/ printf(char *, ...);
int sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int sem_wait(int /*<<< orphan*/ *);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  sem_t *sem;
  int value;
  if (argc != 2) {
    printf("usage: %s <name>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((sem = sem_open(argv[1], 0)) == SEM_FAILED) {
    printf("sem_open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (sem_wait(sem) == -1) {
    printf("sem_wait error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (sem_getvalue(sem, &value) == -1) {
    printf("sem_getvalue error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  printf("pid %ld has semaphore, value = %d\n", (long)getpid(), value);
  pause();
  exit(EXIT_SUCCESS);
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11c3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <main+0x93>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    1252 <main+0xd2>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1060 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    1295 <main+0x115>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
mov    -0x1c(%rbp),%edx
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <pause@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    11bb <main+0x3b>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e49(%rip),%rax
cmp    (%rax),%rbx
jne    11c7 <main+0x47>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe59(%rip),%rdi
jmp    1212 <main+0x92>
mov    (%rsi),%rsi
lea    0xe3b(%rip),%rdi
jmp    1215 <main+0x95>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    11eb <main+0x6b>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe3d(%rip),%rdi
jmp    1212 <main+0x92>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    122a <main+0xaa>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe28(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0xc(%rsp),%edx
lea    0xe16(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <pause@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"usage: %s <name>\0A\00"
@EXIT_FAILURE = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [20 x i8] c"sem_open error: %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"sem_wait error: %s\0A\00"
@.str.3 = [24 x i8] c"sem_getvalue error: %s\0A\00"
@.str.4 = [35 x i8] c"pid %ld has semaphore, value = %d\0A\00"
@EXIT_SUCCESS = global i32 0
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* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @sem_open(i8* %20, i32 0)
store i32* %21, i32** %6
%22 = load i32*, i32** @SEM_FAILED
%23 = icmp eq i32* %21, %22
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %17
%31 = load i32*, i32** %6
%32 = call i32 @sem_wait(i32* %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = load i32*, i32** %6
%42 = call i32 @sem_getvalue(i32* %41, i32* %7)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
%51 = call i64 (...) @getpid()
%52 = load i32, i32* %7
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i64 %51, i32 %52)
%54 = call i32 (...) @pause()
%55 = load i32, i32* @EXIT_SUCCESS
%56 = call i32 @exit(i32 %55)
unreachable
}

LLVM IR (-O2):
@.str = [18 x i8] c"usage: %s <name>\0A\00"
@EXIT_FAILURE = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [20 x i8] c"sem_open error: %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"sem_wait error: %s\0A\00"
@.str.3 = [24 x i8] c"sem_getvalue error: %s\0A\00"
@.str.4 = [35 x i8] c"pid %ld has semaphore, value = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @sem_open(i8* %13, i32 0)
%15 = load i32*, i32** @SEM_FAILED
%16 = icmp eq i32* %14, %15
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %11
%24 = tail call i32 @sem_wait(i32* %14)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = call i32 @sem_getvalue(i32* %14, i32* %3)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %32
%42 = call i64 (...) @getpid()
%43 = load i32, i32* %3
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i64 %42, i32 %43)
%45 = call i32 (...) @pause()
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
}