#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 */
struct sembuf {
  int sem_num;
  int sem_op;
  int /*<<< orphan*/ sem_flg;
};

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ SEM_UNDO;
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int semget(int, int, int);
int semop(int, struct sembuf *, int);
int cwirc_sem_create(int key, int nb_sems) {
  struct sembuf sops;
  int semid;
  int i;
  if ((semid = semget(key, nb_sems, IPC_CREAT | 0600)) == -1)
    return (-1);
  for (i = 0; i < nb_sems; i++) {
    sops.sem_num = i;
    sops.sem_op = 1;
    sops.sem_flg = SEM_UNDO;
    if (semop(semid, &sops, 1) == -1) {
      semctl(semid, 0, IPC_RMID, 0);
      semid = -1;
      return (-1);
    }
  }
  return (semid);
}


x86 assembly (clang, -O0):
<cwirc_sem_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
call   1030 <semget@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1170 <cwirc_sem_create+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <cwirc_sem_create+0xc7>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f1 <cwirc_sem_create+0xc1>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
jne    11de <cwirc_sem_create+0xae>
mov    -0x1c(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1040 <semctl@plt>
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <cwirc_sem_create+0xc7>
jmp    11e3 <cwirc_sem_create+0xb3>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1177 <cwirc_sem_create+0x47>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cwirc_sem_create>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r12d
mov    0x2e88(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
call   1030 <semget@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11c9 <cwirc_sem_create+0x99>
mov    %eax,%ebx
test   %r12d,%r12d
jle    11b0 <cwirc_sem_create+0x80>
xor    %ebp,%ebp
mov    0x2e81(%rip),%r13
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,0x8(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
je     11b5 <cwirc_sem_create+0x85>
add    $0x1,%ebp
cmp    %ebp,%r12d
jne    1180 <cwirc_sem_create+0x50>
mov    %ebx,%r14d
jmp    11c9 <cwirc_sem_create+0x99>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1040 <semctl@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@IPC_CREAT = global i32 0
@SEM_UNDO = global i32 0
@IPC_RMID = global i32 0
define i32 @cwirc_sem_create(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sembuf
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 384
%13 = call i32 @semget(i32 %9, i32 %10, i32 %12)
store i32 %13, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %40
16: ; preds = %2
store i32 0, i32* %8
br label %17
17: ; preds = %35, %16
%18 = load i32, i32* %8
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %38
21: ; preds = %17
%22 = load i32, i32* %8
%23 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i32 1, i32* %24
%25 = load i32, i32* @SEM_UNDO
%26 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %7
%28 = call i32 @semop(i32 %27, %struct.sembuf* %6, i32 1)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i32, i32* %7
%32 = load i32, i32* @IPC_RMID
%33 = call i32 @semctl(i32 %31, i32 0, i32 %32, i32 0)
store i32 -1, i32* %7
store i32 -1, i32* %3
br label %40
34: ; preds = %21
br label %35
35: ; preds = %34
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %17
38: ; preds = %17
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %30, %15
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@IPC_CREAT = global i32 0
@SEM_UNDO = global i32 0
@IPC_RMID = global i32 0
define i32 @cwirc_sem_create(i32 %0, i32 %1) {
%3 = alloca %struct.sembuf
%4 = bitcast %struct.sembuf* %3 to i8*
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 384
%7 = tail call i32 @semget(i32 %0, i32 %1, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %26, label %9
9: ; preds = %2
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %26
11: ; preds = %9
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
%13 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
br label %17
15: ; preds = %17
%16 = icmp eq i32 %22, %1
br i1 %16, label %26, label %17
17: ; preds = %11, %15
%18 = phi i32 [ 0, %11 ], [ %22, %15 ]
store i32 %18, i32* %12
store i32 1, i32* %13
%19 = load i32, i32* @SEM_UNDO
store i32 %19, i32* %14
%20 = call i32 @semop(i32 %7, %struct.sembuf* %3, i32 1)
%21 = icmp eq i32 %20, -1
%22 = add nuw nsw i32 %18, 1
br i1 %21, label %23, label %15
23: ; preds = %17
%24 = load i32, i32* @IPC_RMID
%25 = call i32 @semctl(i32 %7, i32 0, i32 %24, i32 0)
br label %26
26: ; preds = %15, %9, %2, %23
%27 = phi i32 [ -1, %23 ], [ -1, %2 ], [ %7, %9 ], [ %7, %15 ]
ret i32 %27
}