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

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sem_init(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int init_sem(sem_t **s, int num) {
  int i;
  char sem_r[] = "semxxxx";
  char sem_n[256];
  for (i = 0; i < num; i++) {
    sprintf(sem_n, "%s%d", sem_r, i);
    s[i] = sem_open(sem_n, O_CREAT, 0644, 0);
    if (s[i] == SEM_FAILED) {
      perror("sem_open");
      exit(-1);
    }
    sem_init(s[i], 1, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<init_sem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121d <init_sem+0xcd>
lea    -0x120(%rbp),%rdi
lea    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%ecx
lea    0xe73(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
xor    %ecx,%ecx
call   1030 <sem_open@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    11f7 <init_sem+0xa7>
lea    0xe25(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <sem_init@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1174 <init_sem+0x24>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
movabs $0x787878786d6573,%rax
mov    %rax,0x8(%rsp)
test   %esi,%esi
jle    11da <init_sem+0x8a>
mov    %rdi,%r14
mov    %esi,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r12
mov    0x2e71(%rip),%r15
mov    0x2e52(%rip),%r13
xchg   %ax,%ax
mov    %r12,%rdi
lea    0xe66(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%ecx
call   1060 <sprintf@plt>
mov    (%r15),%esi
mov    %r12,%rdi
mov    $0x1a4,%edx
xor    %ecx,%ecx
call   1030 <sem_open@plt>
mov    %rax,(%r14,%rbx,8)
cmp    0x0(%r13),%rax
je     11ee <init_sem+0x9e>
mov    %rax,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <sem_init@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <init_sem+0x40>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe10(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.init_sem.sem_r = [8 x i8] c"semxxxx\00"
@.str = [5 x i8] c"%s%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
define i32 @init_sem(i32** %0, i32 %1) {
%3 = alloca i32**
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8 x i8]
%7 = alloca [256 x i8]
store i32** %0, i32*** %3
store i32 %1, i32* %4
%8 = bitcast [8 x i8]* %6 to i8*
store i32 0, i32* %5
br label %9
9: ; preds = %42, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %45
13: ; preds = %9
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* @O_CREAT
%20 = call i32* @sem_open(i8* %18, i32 %19, i32 420, i32 0)
%21 = load i32**, i32*** %3
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32*, i32** %21, i64 %23
store i32* %20, i32** %24
%25 = load i32**, i32*** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32*, i32** %25, i64 %27
%29 = load i32*, i32** %28
%30 = load i32*, i32** @SEM_FAILED
%31 = icmp eq i32* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %13
%33 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %13
%36 = load i32**, i32*** %3
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32*, i32** %36, i64 %38
%40 = load i32*, i32** %39
%41 = call i32 @sem_init(i32* %40, i32 1, i32 0)
br label %42
42: ; preds = %35
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %9
45: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
define i32 @init_sem(i32** nocapture writeonly %0, i32 %1) {
%3 = alloca i64
%4 = alloca [256 x i8]
%5 = bitcast i64* %3 to i8*
store i64 33909456017122675, i64* %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %26
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %22
%11 = phi i64 [ 0, %8 ], [ %24, %22 ]
%12 = trunc i64 %11 to i32
%13 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %12)
%14 = load i32, i32* @O_CREAT
%15 = call i32* @sem_open(i8* %6, i32 %14, i32 420, i32 0)
%16 = getelementptr inbounds i32*, i32** %0, i64 %11
store i32* %15, i32** %16
%17 = load i32*, i32** @SEM_FAILED
%18 = icmp eq i32* %15, %17
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %10
%23 = call i32 @sem_init(i32* %15, i32 1, i32 0)
%24 = add nuw nsw i64 %11, 1
%25 = icmp eq i64 %24, %9
br i1 %25, label %26, label %10
26: ; preds = %22, %2
ret i32 0
}