#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;
  int /*<<< orphan*/ sem_flg;
  scalar_t__ sem_num;
};

/* Variables and functions */
int /*<<< orphan*/ SEM_UNDO;
int /*<<< orphan*/ perror(char *);
int semget(int, int, int /*<<< orphan*/);
int semop(int, struct sembuf *, int);
int semlock() {
  struct sembuf lock[1];
  int ret = 0;
  int lockid = semget(34552, 1, 0);
  if (lockid == -1) {
    perror("get singal error\n");
    return -1;
  }
  lock[0].sem_num = 0;
  lock[0].sem_op = -1;
  lock[0].sem_flg = SEM_UNDO;
  ret = semop(lockid, lock, 1);
  if (ret < 0)
    perror("op signal error\n");
  return ret;
}


x86 assembly (clang, -O0):
<semlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x24(%rbp)
mov    $0x86f8,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1175 <semlock+0x45>
lea    0xe9c(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <semlock+0x90>
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ba <semlock+0x8a>
lea    0xe5d(%rip),%rdi
call   1040 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semlock>:
push   %rbx
sub    $0x10,%rsp
mov    $0x86f8,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     1185 <semlock+0x55>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <semop@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1198 <semlock+0x68>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe74(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    117d <semlock+0x4d>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
jmp    117d <semlock+0x4d>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [18 x i8] c"get singal error\0A\00"
@SEM_UNDO = global i32 0
@.str.1 = [17 x i8] c"op signal error\0A\00"
define i32 @semlock() {
%1 = alloca i32
%2 = alloca [1 x %struct.sembuf]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %3
%5 = call i32 @semget(i32 34552, i32 1, i32 0)
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
10: ; preds = %0
%11 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 2
store i64 0, i64* %12
%13 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %13, i32 0, i32 0
store i32 -1, i32* %14
%15 = load i32, i32* @SEM_UNDO
%16 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0
%17 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load i32, i32* %4
%19 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0
%20 = call i32 @semop(i32 %18, %struct.sembuf* %19, i32 1)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %10
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %10
%26 = load i32, i32* %3
store i32 %26, i32* %1
br label %27
27: ; preds = %25, %8
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str = [18 x i8] c"get singal error\0A\00"
@SEM_UNDO = global i32 0
@.str.1 = [17 x i8] c"op signal error\0A\00"
define i32 @semlock() {
%1 = alloca [1 x %struct.sembuf]
%2 = bitcast [1 x %struct.sembuf]* %1 to i8*
%3 = tail call i32 @semget(i32 34552, i32 1, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %17
7: ; preds = %0
%8 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0
%9 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0, i32 2
store i64 0, i64* %9
%10 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0, i32 0
store i32 -1, i32* %10
%11 = load i32, i32* @SEM_UNDO
%12 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @semop(i32 %3, %struct.sembuf* %8, i32 1)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %7, %15, %5
%18 = phi i32 [ -1, %5 ], [ %13, %15 ], [ %13, %7 ]
ret i32 %18
}