#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int semop(int, struct sembuf *, int);
int ipc_sem_lock(int semid, struct sembuf *sb) {
  sb->sem_num = 0;
  sb->sem_op = -1;
  sb->sem_flg = 0;
  if (semop(semid, sb, 1) == -1) {
    perror("semop");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ipc_sem_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
jne    1191 <ipc_sem_lock+0x61>
lea    0xe7e(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_sem_lock>:
push   %rax
movl   $0xffffffff,(%rsi)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsi)
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
je     1151 <ipc_sem_lock+0x21>
xor    %eax,%eax
pop    %rcx
ret
lea    0xea8(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@.str = [6 x i8] c"semop\00"
define i32 @ipc_sem_lock(i32 %0, %struct.sembuf* %1) {
%3 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
store i32 -1, i32* %3
%4 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
%5 = bitcast i64* %4 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %5
%6 = tail call i32 @semop(i32 %0, %struct.sembuf* %1, i32 1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
ret i32 0
}