#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_op;
  scalar_t__ sem_flg;
  scalar_t__ sem_num;
};

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int semop(int, struct sembuf *, int);
int sem_action(int semid, int val) {
  struct sembuf sb;
  int ret;
  sb.sem_num = 0;
  sb.sem_op = val;
  sb.sem_flg = 0;
  ret = semop(semid, &sb, 1);
  if (ret < 0) {
    perror("semop: ");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sem_action>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    117a <sem_action+0x5a>
lea    0xe97(%rip),%rdi
call   1030 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1181 <sem_action+0x61>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_action>:
sub    $0x18,%rsp
mov    %esi,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    %rsp,%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     1149 <sem_action+0x29>
add    $0x18,%rsp
ret
lea    0xeb0(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

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

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