#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 scalar_t__ key_t;

/* Variables and functions */
int IPC_CREAT;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int semget(scalar_t__, int, int);
int getKey(key_t *candidate, int *semid) {
  int i = 0;
  for (i = 1; i <= 500; i++) {
    if ((*semid = semget((key_t)(i * 10), 3 * 2,
                         IPC_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
                             S_IROTH | S_IWOTH)) == -1) {
      perror("Couldn't get semaphore: ");
      return -1;
    } else {
      *candidate = (key_t)(i * 10);
      printf("Semaphore set allocated = %d, key = %d\n", *semid,
             (int)*candidate);
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<getKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x1f4,-0x1c(%rbp)
jg     1202 <getKey+0xd2>
imul   $0xa,-0x1c(%rbp),%eax
movslq %eax,%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e4e(%rip),%rax
or     (%rax),%edx
mov    0x2e65(%rip),%rax
or     (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
mov    0x2e5b(%rip),%rax
or     (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
mov    $0x6,%esi
call   1030 <semget@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    11d2 <getKey+0xa2>
lea    0xe3f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <getKey+0xd2>
imul   $0xa,-0x1c(%rbp),%eax
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edx
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getKey>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e93(%rip),%rax
or     (%rax),%edx
mov    0x2e7a(%rip),%rax
or     (%rax),%edx
mov    0x2e89(%rip),%rax
or     (%rax),%edx
mov    0x2e60(%rip),%rax
or     (%rax),%edx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e31(%rip),%rax
or     (%rax),%edx
mov    $0xa,%edi
mov    $0x6,%esi
call   1030 <semget@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11b3 <getKey+0x83>
movq   $0xa,(%r14)
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ab <getKey+0x7b>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [25 x i8] c"Couldn't get semaphore: \00"
@.str.1 = [40 x i8] c"Semaphore set allocated = %d, key = %d\0A\00"
define i32 @getKey(i64* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i32
store i64* %0, i64** %4
store i32* %1, i32** %5
store i32 0, i32* %6
store i32 1, i32* %6
br label %7
7: ; preds = %43, %2
%8 = load i32, i32* %6
%9 = icmp sle i32 %8, 500
br i1 %9, label %10, label %46
10: ; preds = %7
%11 = load i32, i32* %6
%12 = mul nsw i32 %11, 10
%13 = sext i32 %12 to i64
%14 = load i32, i32* @IPC_CREAT
%15 = load i32, i32* @S_IRUSR
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IWGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IWOTH
%26 = or i32 %24, %25
%27 = call i32 @semget(i64 %13, i32 6, i32 %26)
%28 = load i32*, i32** %5
store i32 %27, i32* %28
%29 = icmp eq i32 %27, -1
br i1 %29, label %30, label %32
30: ; preds = %10
%31 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
32: ; preds = %10
%33 = load i32, i32* %6
%34 = mul nsw i32 %33, 10
%35 = sext i32 %34 to i64
%36 = load i64*, i64** %4
store i64 %35, i64* %36
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = load i64*, i64** %4
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i32
%42 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %41)
store i32 0, i32* %3
br label %46
43: ; No predecessors!
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %7
46: ; preds = %30, %32, %7
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [25 x i8] c"Couldn't get semaphore: \00"
@.str.1 = [40 x i8] c"Semaphore set allocated = %d, key = %d\0A\00"
define i32 @getKey(i64* nocapture writeonly %0, i32* nocapture %1) {
%3 = load i32, i32* @IPC_CREAT
%4 = load i32, i32* @S_IRUSR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IWGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWOTH
%15 = or i32 %13, %14
%16 = tail call i32 @semget(i64 10, i32 6, i32 %15)
store i32 %16, i32* %1
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %22
20: ; preds = %2
store i64 10, i64* %0
%21 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 10)
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ -1, %18 ], [ 0, %20 ]
ret i32 %23
}