#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*/ SIGINT;
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int semop(int, struct sembuf *, int);
int semap_wait(int sem_id, int nsem, int cnt) {
  struct sembuf op[1];
  op[0].sem_num = nsem;
  op[0].sem_op = -cnt;
  op[0].sem_flg = 0;
  int ret;
  ret = semop(sem_id, op, 1);
  if (ret == -1) {
    kill(0, SIGINT);
  }
  return ret;
}


x86 assembly (clang, -O0):
<semap_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1175 <semap_wait+0x55>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <kill@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semap_wait>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,(%rsp)
neg    %edx
mov    %edx,0x4(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    115b <semap_wait+0x3b>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <kill@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@SIGINT = global i32 0
define i32 @semap_wait(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1 x %struct.sembuf]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%11 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* %6
%13 = sub nsw i32 0, %12
%14 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %14, i32 0, i32 1
store i32 %13, i32* %15
%16 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%17 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %16, i32 0, i32 2
store i64 0, i64* %17
%18 = load i32, i32* %4
%19 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%20 = call i32 @semop(i32 %18, %struct.sembuf* %19, i32 1)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i32, i32* @SIGINT
%25 = call i32 @kill(i32 0, i32 %24)
br label %26
26: ; preds = %23, %3
%27 = load i32, i32* %8
ret i32 %27
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@SIGINT = global i32 0
define i32 @semap_wait(i32 %0, i32 %1, i32 %2) {
%4 = alloca [1 x %struct.sembuf]
%5 = bitcast [1 x %struct.sembuf]* %4 to i8*
%6 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 0
store i32 %1, i32* %7
%8 = sub nsw i32 0, %2
%9 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 2
store i64 0, i64* %10
%11 = call i32 @semop(i32 %0, %struct.sembuf* %6, i32 1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @SIGINT
%15 = call i32 @kill(i32 0, i32 %14)
br label %16
16: ; preds = %13, %3
ret i32 %11
}