#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_getvalue(int /*<<< orphan*/ *, int *);
scalar_t__ sem_post(int /*<<< orphan*/ *);
int notify(sem_t *sem_id) {
  int sem_value;
  if (sem_id) {
    if (sem_getvalue(sem_id, &sem_value)) {
      perror("sem_getvalue");
    }
    if (sem_value == 0) {
      if (sem_post(sem_id) < 0) {
        perror("sem_wait");
        return -1;
      }
    } else {
      return sem_value;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<notify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b4 <notify+0x84>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <sem_getvalue@plt>
cmp    $0x0,%rax
je     116a <notify+0x3a>
lea    0xe9b(%rip),%rdi
call   1050 <perror@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11a4 <notify+0x74>
mov    -0x10(%rbp),%rdi
call   1040 <sem_post@plt>
cmp    $0x0,%rax
jge    119f <notify+0x6f>
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <notify+0x8b>
jmp    11af <notify+0x7f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <notify+0x8b>
jmp    11b4 <notify+0x84>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<notify>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
test   %rdi,%rdi
je     116b <notify+0x3b>
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
call   1030 <sem_getvalue@plt>
test   %rax,%rax
jne    1171 <notify+0x41>
mov    0xc(%rsp),%eax
test   %eax,%eax
jne    116b <notify+0x3b>
mov    %rbx,%rdi
call   1040 <sem_post@plt>
mov    %rax,%rcx
mov    $0x0,%eax
test   %rcx,%rcx
js     117f <notify+0x4f>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
jmp    114e <notify+0x1e>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    116b <notify+0x3b>

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

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