#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 /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ ftok(char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int /*<<< orphan*/, int, int);
int sem_init(int proj_id, int value) {
  int semid = semget(ftok(".", proj_id), 1, IPC_CREAT | 0600);
  if (semid < 0) {
    perror("Semget()");
    return -1;
  }
  if (semctl(semid, 0, SETVAL, value) < 0) {
    perror("Semctl()");
    return -1;
  }
  return semid;
}


x86 assembly (clang, -O0):
<sem_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%esi
lea    0xea8(%rip),%rdi
call   1060 <ftok@plt>
mov    %eax,%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    119d <sem_init+0x5d>
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <sem_init+0x9b>
mov    -0x10(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%ecx
xor    %esi,%esi
call   1050 <semctl@plt>
cmp    $0x0,%rax
jge    11d5 <sem_init+0x95>
lea    0xe47(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <sem_init+0x9b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_init>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%esi
lea    0xeb2(%rip),%rdi
call   1060 <ftok@plt>
mov    0x2e86(%rip),%rcx
mov    $0x180,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
test   %eax,%eax
js     1195 <sem_init+0x55>
mov    %eax,%ebp
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
mov    %ebx,%ecx
call   1050 <semctl@plt>
test   %rax,%rax
js     119e <sem_init+0x5e>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe66(%rip),%rdi
jmp    11a5 <sem_init+0x65>
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebp
jmp    118c <sem_init+0x4c>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@IPC_CREAT = 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 @sem_init(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 384
%11 = call i32 @semget(i32 %8, i32 1, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
16: ; preds = %2
%17 = load i32, i32* %6
%18 = load i32, i32* @SETVAL
%19 = load i32, i32* %5
%20 = call i64 @semctl(i32 %17, i32 0, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
24: ; preds = %16
%25 = load i32, i32* %6
store i32 %25, i32* %3
br label %26
26: ; preds = %24, %22, %14
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@IPC_CREAT = 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 @sem_init(i32 %0, i32 %1) {
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %4, 384
%6 = tail call i32 @semget(i32 %3, i32 1, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @SETVAL
%10 = tail call i64 @semctl(i32 %6, i32 0, i32 %9, i32 %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8, %2
%13 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %8 ]
%14 = tail call i32 @perror(i8* %13)
br label %15
15: ; preds = %12, %8
%16 = phi i32 [ %6, %8 ], [ -1, %12 ]
ret i32 %16
}