#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 /*<<< orphan*/ sem_t;
typedef int mode_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
scalar_t__ sem_init(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ *sem_open(char const *, int, int, int /*<<< orphan*/);
int sem_create(const char *sem_name, int *iv) {
  int rv = -1;
  int val = iv ? *iv : 0;
  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
  sem_t *sp = sem_open(sem_name, O_CREAT | O_RDWR, mode, 0);
  if (sp != NULL) {
    if (sem_init(sp, 1, val) == 0)
      rv = 0;
    sem_close(sp);
  }
  return rv;
}


x86 assembly (clang, -O0):
<sem_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1160 <sem_create+0x30>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    116a <sem_create+0x3a>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    116a <sem_create+0x3a>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
or     (%rcx),%eax
mov    0x2e37(%rip),%rcx
or     (%rcx),%eax
mov    0x2e46(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <sem_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f1 <sem_create+0xc1>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1,%esi
call   1040 <sem_init@plt>
cmp    $0x0,%rax
jne    11e8 <sem_create+0xb8>
movl   $0x0,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <sem_close@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_create>:
push   %rbp
push   %rbx
push   %rax
test   %rsi,%rsi
je     113c <sem_create+0xc>
mov    (%rsi),%ebp
jmp    113e <sem_create+0xe>
xor    %ebp,%ebp
mov    0x2e73(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e69(%rip),%rax
or     (%rax),%edx
mov    0x2e78(%rip),%rax
or     (%rax),%edx
mov    0x2e8f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ecx,%ecx
call   1030 <sem_open@plt>
test   %rax,%rax
je     11a1 <sem_create+0x71>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1040 <sem_init@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <sem_close@plt>
jmp    11a6 <sem_create+0x76>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
define i32 @sem_create(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 -1, i32* %5
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32*, i32** %4
%13 = load i32, i32* %12
br label %15
14: ; preds = %2
br label %15
15: ; preds = %14, %11
%16 = phi i32 [ %13, %11 ], [ 0, %14 ]
store i32 %16, i32* %6
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IWGRP
%23 = or i32 %21, %22
store i32 %23, i32* %7
%24 = load i8*, i8** %3
%25 = load i32, i32* @O_CREAT
%26 = load i32, i32* @O_RDWR
%27 = or i32 %25, %26
%28 = load i32, i32* %7
%29 = call i32* @sem_open(i8* %24, i32 %27, i32 %28, i32 0)
store i32* %29, i32** %8
%30 = load i32*, i32** %8
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %41
32: ; preds = %15
%33 = load i32*, i32** %8
%34 = load i32, i32* %6
%35 = call i64 @sem_init(i32* %33, i32 1, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 0, i32* %5
br label %38
38: ; preds = %37, %32
%39 = load i32*, i32** %8
%40 = call i32 @sem_close(i32* %39)
br label %41
41: ; preds = %38, %15
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
define i32 @sem_create(i8* %0, i32* %1) {
%3 = icmp eq i32* %1, null
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = load i32, i32* %1
br label %6
6: ; preds = %2, %4
%7 = phi i32 [ %5, %4 ], [ 0, %2 ]
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDWR
%17 = or i32 %16, %15
%18 = tail call i32* @sem_open(i8* %0, i32 %17, i32 %14, i32 0)
%19 = icmp eq i32* %18, null
br i1 %19, label %25, label %20
20: ; preds = %6
%21 = tail call i64 @sem_init(i32* %18, i32 1, i32 %7)
%22 = icmp ne i64 %21, 0
%23 = sext i1 %22 to i32
%24 = tail call i32 @sem_close(i32* %18)
br label %25
25: ; preds = %20, %6
%26 = phi i32 [ %23, %20 ], [ -1, %6 ]
ret i32 %26
}