#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*/ perror(char *);
scalar_t__ sem_trywait(int /*<<< orphan*/ *);
scalar_t__ sem_wait(int /*<<< orphan*/ *);
int posix_wait(sem_t *sem_id) {
  if (sem_id) {
    while (sem_trywait(sem_id) == 0) {
    }
    if (sem_wait(sem_id) < 0) {
      perror("sem_wait");
      return -2;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<posix_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <posix_wait+0x64>
jmp    114c <posix_wait+0x1c>
mov    -0x10(%rbp),%rdi
call   1040 <sem_trywait@plt>
cmp    $0x0,%rax
jne    1164 <posix_wait+0x34>
jmp    114c <posix_wait+0x1c>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0x0,%rax
jge    118f <posix_wait+0x5f>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    119b <posix_wait+0x6b>
jmp    1194 <posix_wait+0x64>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_wait>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     115a <posix_wait+0x2a>
mov    %rdi,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <sem_trywait@plt>
test   %rax,%rax
je     1140 <posix_wait+0x10>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
js     1163 <posix_wait+0x33>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffe,%ebp
jmp    115a <posix_wait+0x2a>

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

LLVM IR (-O2):
@.str = [9 x i8] c"sem_wait\00"
define i32 @posix_wait(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %11, label %3
3: ; preds = %1, %3
%4 = tail call i64 @sem_trywait(i32* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %3, label %6
6: ; preds = %3
%7 = tail call i64 @sem_wait(i32* %0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %1, %6, %9
%12 = phi i32 [ -2, %9 ], [ 0, %6 ], [ 0, %1 ]
ret i32 %12
}