#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int get_key(char const *);
int /*<<< orphan*/ print_msg(char *, int);
int semget(int, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int create_sem(const char *path, int nbsems) {
  key_t key;
  int semid = -1;
  key = get_key(path);
  if (key == -1)
    return -1;
  semid = semget(key, nbsems, IPC_CREAT | IPC_EXCL | 0600);
  if (semid == -1)
    fprintf(stderr, "create_sem(%s)::semget: %s\n", path, strerror(errno));
  else
    print_msg("%d:\n", semid);
  return semid;
}


x86 assembly (clang, -O0):
<create_sem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <get_key@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1188 <create_sem+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <create_sem+0xba>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    0x2e52(%rip),%rax
or     (%rax),%edx
or     $0x180,%edx
call   1030 <semget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f5 <create_sem+0xa5>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe15(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1204 <create_sem+0xb4>
mov    -0x1c(%rbp),%esi
lea    0xe1d(%rip),%rdi
call   1060 <print_msg@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_sem>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1050 <get_key@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11cf <create_sem+0x7f>
mov    0x2e58(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e77(%rip),%rcx
or     (%rcx),%edx
or     $0x180,%edx
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     11a4 <create_sem+0x54>
mov    %eax,%ebp
lea    0xe84(%rip),%rdi
mov    %eax,%esi
call   1060 <print_msg@plt>
mov    %ebp,%r14d
jmp    11cf <create_sem+0x7f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe3e(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"create_sem(%s)::semget: %s\0A\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"%d:\0A\00"
define i32 @create_sem(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %7
%8 = load i8*, i8** %4
%9 = call i32 @get_key(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %34
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = load i32, i32* @IPC_CREAT
%17 = load i32, i32* @IPC_EXCL
%18 = or i32 %16, %17
%19 = or i32 %18, 384
%20 = call i32 @semget(i32 %14, i32 %15, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %27)
br label %32
29: ; preds = %13
%30 = load i32, i32* %7
%31 = call i32 @print_msg(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %29, %23
%33 = load i32, i32* %7
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %12
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"create_sem(%s)::semget: %s\0A\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"%d:\0A\00"
define i32 @create_sem(i8* %0, i32 %1) {
%3 = tail call i32 @get_key(i8* %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %19, label %5
5: ; preds = %2
%6 = load i32, i32* @IPC_CREAT
%7 = load i32, i32* @IPC_EXCL
%8 = or i32 %6, %7
%9 = or i32 %8, 384
%10 = tail call i32 @semget(i32 %3, i32 %1, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
br label %19
17: ; preds = %5
%18 = tail call i32 @print_msg(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %19
19: ; preds = %12, %17, %2
%20 = phi i32 [ -1, %2 ], [ %10, %17 ], [ -1, %12 ]
ret i32 %20
}