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

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int IPC_NOWAIT;
int SEM_UNDO;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int, int, int);
int semop(int, struct sembuf *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int ExclusiveLock(int semid) {
  struct sembuf ops[1];
  int ret = -1;
  int lockid = -1;
  lockid = semget(semid, 1, 0666 | IPC_CREAT | IPC_EXCL);
  if (lockid != -1) {
    if (semctl(lockid, 0, SETVAL, 1) < 0) {
      perror("create signal failed");
      return -1;
    }
  } else {
    lockid = semget(semid, 1, 0666);
    if (lockid == -1) {
      perror("get signal failed");
      return -1;
    }
  }
  ops[0].sem_num = 0;
  ops[0].sem_op = -1;
  ops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
  ret = semop(lockid, ops, 1);
  if (ret == -1) {
    fprintf(stderr, "Get lock failed: %s.\n", strerror(errno));
    return (-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ExclusiveLock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    0x2e5e(%rip),%rax
or     (%rax),%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     11ea <ExclusiveLock+0x8a>
mov    -0x28(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x1,%ecx
call   1060 <semctl@plt>
cmp    $0x0,%rax
jge    11e5 <ExclusiveLock+0x85>
lea    0xe2c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <ExclusiveLock+0x147>
jmp    1226 <ExclusiveLock+0xc6>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1221 <ExclusiveLock+0xc1>
lea    0xe05(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <ExclusiveLock+0x147>
jmp    1226 <ExclusiveLock+0xc6>
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    0x2d9b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12a0 <ExclusiveLock+0x140>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd98(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <ExclusiveLock+0x147>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ExclusiveLock>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e78(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     11bb <ExclusiveLock+0x5b>
mov    %eax,%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x1,%ecx
call   1060 <semctl@plt>
test   %rax,%rax
jns    11d3 <ExclusiveLock+0x73>
lea    0xe4c(%rip),%rdi
call   1050 <perror@plt>
jmp    1239 <ExclusiveLock+0xd9>
mov    %ebp,%edi
mov    $0x1,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1245 <ExclusiveLock+0xe5>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x2dfe(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x4(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    123e <ExclusiveLock+0xde>
mov    0x2db0(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1050 <perror@plt>
jmp    1239 <ExclusiveLock+0xd9>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@SETVAL = global i32 0
@.str = [21 x i8] c"create signal failed\00"
@.str.1 = [18 x i8] c"get signal failed\00"
@SEM_UNDO = global i32 0
@IPC_NOWAIT = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"Get lock failed: %s.\0A\00"
@errno = global i32 0
define i32 @ExclusiveLock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1 x %struct.sembuf]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %5
store i32 -1, i32* %6
%7 = load i32, i32* %3
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 438, %8
%10 = load i32, i32* @IPC_EXCL
%11 = or i32 %9, %10
%12 = call i32 @semget(i32 %7, i32 1, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %1
%16 = load i32, i32* %6
%17 = load i32, i32* @SETVAL
%18 = call i64 @semctl(i32 %16, i32 0, i32 %17, i32 1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
22: ; preds = %15
br label %31
23: ; preds = %1
%24 = load i32, i32* %3
%25 = call i32 @semget(i32 %24, i32 1, i32 438)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
30: ; preds = %23
br label %31
31: ; preds = %30, %22
%32 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%33 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %32, i32 0, i32 2
store i64 0, i64* %33
%34 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %34, i32 0, i32 0
store i32 -1, i32* %35
%36 = load i32, i32* @SEM_UNDO
%37 = load i32, i32* @IPC_NOWAIT
%38 = or i32 %36, %37
%39 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%40 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %39, i32 0, i32 1
store i32 %38, i32* %40
%41 = load i32, i32* %6
%42 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%43 = call i32 @semop(i32 %41, %struct.sembuf* %42, i32 1)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %31
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %49)
store i32 -1, i32* %2
br label %52
51: ; preds = %31
store i32 0, i32* %2
br label %52
52: ; preds = %51, %46, %28, %20
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@SETVAL = global i32 0
@.str = [21 x i8] c"create signal failed\00"
@.str.1 = [18 x i8] c"get signal failed\00"
@SEM_UNDO = global i32 0
@IPC_NOWAIT = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"Get lock failed: %s.\0A\00"
@errno = global i32 0
define i32 @ExclusiveLock(i32 %0) {
%2 = alloca [1 x %struct.sembuf]
%3 = bitcast [1 x %struct.sembuf]* %2 to i8*
%4 = load i32, i32* @IPC_CREAT
%5 = load i32, i32* @IPC_EXCL
%6 = or i32 %4, %5
%7 = or i32 %6, 438
%8 = tail call i32 @semget(i32 %0, i32 1, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %16, label %10
10: ; preds = %1
%11 = load i32, i32* @SETVAL
%12 = tail call i64 @semctl(i32 %8, i32 0, i32 %11, i32 1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %37
16: ; preds = %1
%17 = tail call i32 @semget(i32 %0, i32 1, i32 438)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %37
21: ; preds = %16, %10
%22 = phi i32 [ %8, %10 ], [ %17, %16 ]
%23 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0
%24 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0, i32 2
store i64 0, i64* %24
%25 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0, i32 0
store i32 -1, i32* %25
%26 = load i32, i32* @SEM_UNDO
%27 = load i32, i32* @IPC_NOWAIT
%28 = or i32 %27, %26
%29 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %2, i64 0, i64 0, i32 1
store i32 %28, i32* %29
%30 = call i32 @semop(i32 %22, %struct.sembuf* %23, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %21
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %21, %32, %19, %14
%38 = phi i32 [ -1, %14 ], [ -1, %32 ], [ -1, %19 ], [ 0, %21 ]
ret i32 %38
}