#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*/ *sem_open(char *, int /*<<< orphan*/);
int sem_trywait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  sem_t *sem;
  sem = sem_open("/mysem", 0);
  if (sem == SEM_FAILED) {
    perror("sem_open");
    exit(errno);
  };
  if (sem_trywait(sem) == -1) {
    perror("sem_trywait");
    exit(errno);
  };
  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)
lea    0xea3(%rip),%rdi
xor    %esi,%esi
call   1040 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <main+0x56>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <sem_trywait@plt>
cmp    $0xffffffff,%eax
jne    11c2 <main+0x82>
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xeb8(%rip),%rdi
xor    %esi,%esi
call   1040 <sem_open@plt>
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%rax
jne    1164 <main+0x24>
lea    0xea5(%rip),%rdi
jmp    1178 <main+0x38>
mov    %rax,%rdi
call   1030 <sem_trywait@plt>
cmp    $0xffffffff,%eax
jne    118b <main+0x4b>
lea    0xe98(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1060 <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 = [12 x i8] c"sem_trywait\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = load i32*, i32** @SEM_FAILED
%10 = icmp eq i32* %8, %9
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = call i32 @sem_trywait(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @errno
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = load i32, i32* @EXIT_SUCCESS
%25 = call i32 @exit(i32 %24)
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 = [12 x i8] c"sem_trywait\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%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 = %2
%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 = %2
%11 = tail call i32 @sem_trywait(i32* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
}