#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 */
struct sembuf {
  int sem_num;
  int sem_op;
  scalar_t__ sem_flg;
};

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
scalar_t__ semop(int, struct sembuf *, int);
int my_sem_wait(int semid, int sem_num) {
  struct sembuf op;
  op.sem_num = sem_num;
  op.sem_op = -1;
  op.sem_flg = 0;
  if (semop(semid, &op, 1) < 0) {
    perror("Fail to semop");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<my_sem_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0x0,%rax
jge    1176 <my_sem_wait+0x56>
lea    0xe9b(%rip),%rdi
call   1030 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    117d <my_sem_wait+0x5d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sem_wait>:
sub    $0x18,%rsp
mov    %esi,0x8(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1157 <my_sem_wait+0x37>
add    $0x18,%rsp
ret
lea    0xea2(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [14 x i8] c"Fail to semop\00"
define i32 @my_sem_wait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sembuf
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i32 -1, i32* %9
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 2
store i64 0, i64* %10
%11 = load i32, i32* %4
%12 = call i64 @semop(i32 %11, %struct.sembuf* %6, i32 1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %17
16: ; preds = %2
store i32 0, i32* %3
br label %17
17: ; preds = %16, %14
%18 = load i32, i32* %3
ret i32 %18
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str = [14 x i8] c"Fail to semop\00"
define i32 @my_sem_wait(i32 %0, i32 %1) {
%3 = alloca %struct.sembuf
%4 = bitcast %struct.sembuf* %3 to i8*
%5 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 %1, i32* %5
%6 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
store i32 -1, i32* %6
%7 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
store i64 0, i64* %7
%8 = call i64 @semop(i32 %0, %struct.sembuf* %3, i32 1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %2, %10
%13 = phi i32 [ -1, %10 ], [ 0, %2 ]
ret i32 %13
}