#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 */
typedef int key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ des_sem(int);
int /*<<< orphan*/ perror(char *);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int, int, int);
int init_sem(key_t key, int value, int flag) {
  if (key == -1) {
    perror("init_sem key");
    return -1;
  }
  int semid = semget(key, 1, IPC_CREAT | IPC_EXCL | 0666);
  if (semid == -1) {
    semid = semget(key, 1, IPC_CREAT | 0666);
    if (semid == -1) {
      perror("semget()");
      return semid;
    }
  }
  if (flag) {
    if (semctl(semid, 0, SETVAL, value) == -1) {
      perror("semctl()");
      des_sem(semid);
      semid = -1;
    }
  }
  return semid;
}


x86 assembly (clang, -O0):
<init_sem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1173 <init_sem+0x33>
lea    0xe9e(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <init_sem+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    0x2e6a(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ea <init_sem+0xaa>
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e5 <init_sem+0xa5>
lea    0xe38(%rip),%rdi
call   1040 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <init_sem+0xf9>
jmp    11ea <init_sem+0xaa>
cmpl   $0x0,-0x10(%rbp)
je     1233 <init_sem+0xf3>
mov    -0x14(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%ecx
xor    %esi,%esi
call   1050 <semctl@plt>
cmp    $0xffffffff,%eax
jne    122e <init_sem+0xee>
lea    0xdfc(%rip),%rdi
call   1040 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <des_sem@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1233 <init_sem+0xf3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0xffffffff,%edi
je     11c6 <init_sem+0x86>
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e7c(%rip),%r12
mov    (%r12),%edx
mov    0x2e89(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    119c <init_sem+0x5c>
mov    $0x1b6,%edx
or     (%r12),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11e4 <init_sem+0xa4>
test   %r15d,%r15d
je     11bb <init_sem+0x7b>
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
mov    %r14d,%ecx
call   1050 <semctl@plt>
cmp    $0xffffffff,%eax
je     11cf <init_sem+0x8f>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rdi
jmp    11eb <init_sem+0xab>
lea    0xe40(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,%edi
call   1060 <des_sem@plt>
jmp    11f0 <init_sem+0xb0>
lea    0xe22(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11bb <init_sem+0x7b>

LLVM IR (-O0):
@.str = [13 x i8] c"init_sem key\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.1 = [9 x i8] c"semget()\00"
@SETVAL = global i32 0
@.str.2 = [9 x i8] c"semctl()\00"
define i32 @init_sem(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
13: ; preds = %3
%14 = load i32, i32* %5
%15 = load i32, i32* @IPC_CREAT
%16 = load i32, i32* @IPC_EXCL
%17 = or i32 %15, %16
%18 = or i32 %17, 438
%19 = call i32 @semget(i32 %14, i32 1, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %33
22: ; preds = %13
%23 = load i32, i32* %5
%24 = load i32, i32* @IPC_CREAT
%25 = or i32 %24, 438
%26 = call i32 @semget(i32 %23, i32 1, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %49
32: ; preds = %22
br label %33
33: ; preds = %32, %13
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %33
%37 = load i32, i32* %8
%38 = load i32, i32* @SETVAL
%39 = load i32, i32* %6
%40 = call i32 @semctl(i32 %37, i32 0, i32 %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %8
%45 = call i32 @des_sem(i32 %44)
store i32 -1, i32* %8
br label %46
46: ; preds = %42, %36
br label %47
47: ; preds = %46, %33
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %29, %11
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [13 x i8] c"init_sem key\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.1 = [9 x i8] c"semget()\00"
@SETVAL = global i32 0
@.str.2 = [9 x i8] c"semctl()\00"
define i32 @init_sem(i32 %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %0, -1
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %31
7: ; preds = %3
%8 = load i32, i32* @IPC_CREAT
%9 = load i32, i32* @IPC_EXCL
%10 = or i32 %8, %9
%11 = or i32 %10, 438
%12 = tail call i32 @semget(i32 %0, i32 1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = load i32, i32* @IPC_CREAT
%16 = or i32 %15, 438
%17 = tail call i32 @semget(i32 %0, i32 1, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %31
21: ; preds = %14, %7
%22 = phi i32 [ %17, %14 ], [ %12, %7 ]
%23 = icmp eq i32 %2, 0
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = load i32, i32* @SETVAL
%26 = tail call i32 @semctl(i32 %22, i32 0, i32 %25, i32 %1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @des_sem(i32 %22)
br label %31
31: ; preds = %19, %24, %28, %21, %5
%32 = phi i32 [ -1, %5 ], [ -1, %19 ], [ -1, %28 ], [ %22, %24 ], [ %22, %21 ]
ret i32 %32
}