#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 */

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ perror(char *);
int semget(int, int, int);
int semid_init(int *semid) {
  int sid;
  sid = semget(1, 0, 0666);
  if (sid < 0) {
    sid = semget(1, 1, IPC_CREAT | IPC_EXCL | 0666);
    if (sid < 0) {
      perror("semget: ");
      return -1;
    }
  }
  *semid = sid;
  return 0;
}


x86 assembly (clang, -O0):
<semid_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1198 <semid_init+0x78>
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
mov    0x2e96(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
mov    %esi,%edi
call   1030 <semget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1193 <semid_init+0x73>
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <semid_init+0x88>
jmp    1198 <semid_init+0x78>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semid_init>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
mov    $0x1,%edi
xor    %esi,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    1168 <semid_init+0x48>
mov    0x2e94(%rip),%rax
mov    (%rax),%edx
mov    0x2ea3(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
test   %eax,%eax
js     1173 <semid_init+0x53>
mov    %eax,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebp
jmp    116a <semid_init+0x4a>

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

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str = [9 x i8] c"semget: \00"
define i32 @semid_init(i32* nocapture writeonly %0) {
%2 = tail call i32 @semget(i32 1, i32 0, i32 438)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %13
4: ; preds = %1
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @IPC_EXCL
%7 = or i32 %5, %6
%8 = or i32 %7, 438
%9 = tail call i32 @semget(i32 1, i32 1, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
13: ; preds = %4, %1
%14 = phi i32 [ %9, %4 ], [ %2, %1 ]
store i32 %14, i32* %0
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ -1, %11 ], [ 0, %13 ]
ret i32 %16
}