#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct seminfo {
  int semusz;
  int semaem;
};
struct TYPE_2__ {
  scalar_t__ __key;
};
struct semid_ds {
  scalar_t__ sem_nsems;
  TYPE_1__ sem_perm;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EINVAL;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SEM_INFO;
int /*<<< orphan*/ SEM_STAT;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *, ...);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, struct semid_ds *);
int main() {
  int i, maxind, semid;
  struct semid_ds ds;
  struct seminfo info;
  maxind = semctl(0, 0, SEM_INFO, (struct semid_ds *)&info);
  if (maxind == -1) {
    pexit("semctl");
  }
  printf("Maximum index in the kernel's array: %d\n", maxind);
  printf("Total semaphore sets: %d\n", info.semusz);
  printf("Total semaphores: %d\n", info.semaem);
  printf("\n%10s\t%10s\t%10s\t%10s\n", "index", "ID", "key", "semaphores");
  for (i = 0; i <= maxind; i++) {
    semid = semctl(i, 0, SEM_STAT, &ds);
    if (semid == -1) {
      if (errno != EINVAL && errno != EACCES)
        pexit("semctl");
      continue;
    }
    printf("%10d\t%8d\t0x%08lx\t%7ld\n", i, semid,
           (unsigned long)ds.sem_perm.__key, (long)ds.sem_nsems);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
xor    %esi,%esi
mov    %esi,%edi
call   1040 <semctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    117e <main+0x3e>
lea    0xe87(%rip),%rdi
call   1060 <pexit@plt>
mov    -0xc(%rbp),%esi
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea8(%rip),%rdi
lea    0xeb7(%rip),%rsi
lea    0xeb6(%rip),%rdx
lea    0xeb2(%rip),%rcx
lea    0xeaf(%rip),%r8
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1281 <main+0x141>
mov    -0x8(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
lea    -0x20(%rbp),%rcx
call   1040 <semctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1257 <main+0x117>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
je     1252 <main+0x112>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%rax
je     1252 <main+0x112>
lea    0xdb3(%rip),%rdi
call   1060 <pexit@plt>
jmp    1273 <main+0x133>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11e2 <main+0xa2>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <semctl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1176 <main+0x36>
lea    0xe8f(%rip),%rdi
call   1060 <pexit@plt>
lea    0xe8a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rsp),%esi
lea    0xea0(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x4(%rsp),%esi
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeb0(%rip),%rdi
lea    0xebf(%rip),%rsi
lea    0xebe(%rip),%rdx
lea    0xeba(%rip),%rcx
lea    0xeb7(%rip),%r8
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebx,%ebx
jns    11e5 <main+0xa5>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x1,%ebx
mov    0x2dd9(%rip),%r13
lea    0x8(%rsp),%r14
mov    0x2dc5(%rip),%r15
lea    0xe8c(%rip),%r12
xor    %ebp,%ebp
jmp    122f <main+0xef>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%r8
mov    0x10(%rsp),%rcx
mov    %r12,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     11d7 <main+0x97>
mov    0x0(%r13),%edx
mov    %ebp,%edi
xor    %esi,%esi
mov    %r14,%rcx
call   1040 <semctl@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0xd0>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
je     1228 <main+0xe8>
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     1228 <main+0xe8>
lea    0xd9a(%rip),%rdi
call   1060 <pexit@plt>
jmp    1228 <main+0xe8>

LLVM IR (-O0):
%struct.semid_ds = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.seminfo = type { i32, i32 }
@SEM_INFO = global i32 0
@.str = [7 x i8] c"semctl\00"
@.str.1 = [41 x i8] c"Maximum index in the kernel's array: %d\0A\00"
@.str.2 = [26 x i8] c"Total semaphore sets: %d\0A\00"
@.str.3 = [22 x i8] c"Total semaphores: %d\0A\00"
@.str.4 = [22 x i8] c"\0A%10s\09%10s\09%10s\09%10s\0A\00"
@.str.5 = [6 x i8] c"index\00"
@.str.6 = [3 x i8] c"ID\00"
@.str.7 = [4 x i8] c"key\00"
@.str.8 = [11 x i8] c"semaphores\00"
@SEM_STAT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str.9 = [23 x i8] c"%10d\09%8d\090x%08lx\09%7ld\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.semid_ds
%6 = alloca %struct.seminfo
store i32 0, i32* %1
%7 = load i32, i32* @SEM_INFO
%8 = bitcast %struct.seminfo* %6 to %struct.semid_ds*
%9 = call i32 @semctl(i32 0, i32 0, i32 %7, %struct.semid_ds* %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds %struct.seminfo, %struct.seminfo* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds %struct.seminfo, %struct.seminfo* %6, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %2
br label %24
24: ; preds = %54, %14
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %57
28: ; preds = %24
%29 = load i32, i32* %2
%30 = load i32, i32* @SEM_STAT
%31 = call i32 @semctl(i32 %29, i32 0, i32 %30, %struct.semid_ds* %5)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %45
34: ; preds = %28
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINVAL
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = load i64, i64* @errno
%40 = load i64, i64* @EACCES
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %38, %34
br label %54
45: ; preds = %28
%46 = load i32, i32* %2
%47 = load i32, i32* %4
%48 = getelementptr inbounds %struct.semid_ds, %struct.semid_ds* %5, i32 0, i32 1
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = getelementptr inbounds %struct.semid_ds, %struct.semid_ds* %5, i32 0, i32 0
%52 = load i64, i64* %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), i32 %46, i32 %47, i64 %50, i64 %52)
br label %54
54: ; preds = %45, %44
%55 = load i32, i32* %2
%56 = add nsw i32 %55, 1
store i32 %56, i32* %2
br label %24
57: ; preds = %24
%58 = load i32, i32* @EXIT_SUCCESS
%59 = call i32 @exit(i32 %58)
unreachable
}

LLVM IR (-O2):
%struct.semid_ds = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.seminfo = type { i32, i32 }
@SEM_INFO = global i32 0
@.str = [7 x i8] c"semctl\00"
@.str.1 = [41 x i8] c"Maximum index in the kernel's array: %d\0A\00"
@.str.2 = [26 x i8] c"Total semaphore sets: %d\0A\00"
@.str.3 = [22 x i8] c"Total semaphores: %d\0A\00"
@.str.4 = [22 x i8] c"\0A%10s\09%10s\09%10s\09%10s\0A\00"
@.str.5 = [6 x i8] c"index\00"
@.str.6 = [3 x i8] c"ID\00"
@.str.7 = [4 x i8] c"key\00"
@.str.8 = [11 x i8] c"semaphores\00"
@SEM_STAT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str.9 = [23 x i8] c"%10d\09%8d\090x%08lx\09%7ld\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.semid_ds
%2 = alloca %struct.seminfo
%3 = bitcast %struct.semid_ds* %1 to i8*
%4 = bitcast %struct.seminfo* %2 to i8*
%5 = load i32, i32* @SEM_INFO
%6 = bitcast %struct.seminfo* %2 to %struct.semid_ds*
%7 = call i32 @semctl(i32 0, i32 0, i32 %5, %struct.semid_ds* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%13 = getelementptr inbounds %struct.seminfo, %struct.seminfo* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds %struct.seminfo, %struct.seminfo* %2, i64 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%20 = icmp slt i32 %7, 0
br i1 %20, label %45, label %21
21: ; preds = %11
%22 = getelementptr inbounds %struct.semid_ds, %struct.semid_ds* %1, i64 0, i32 1, i32 0
%23 = getelementptr inbounds %struct.semid_ds, %struct.semid_ds* %1, i64 0, i32 0
br label %24
24: ; preds = %21, %42
%25 = phi i32 [ 0, %21 ], [ %43, %42 ]
%26 = load i32, i32* @SEM_STAT
%27 = call i32 @semctl(i32 %25, i32 0, i32 %26, %struct.semid_ds* %1)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINVAL
%32 = icmp eq i64 %30, %31
%33 = load i64, i64* @EACCES
%34 = icmp eq i64 %30, %33
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %42, label %36
36: ; preds = %29
%37 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %42
38: ; preds = %24
%39 = load i64, i64* %22
%40 = load i64, i64* %23
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), i32 %25, i32 %27, i64 %39, i64 %40)
br label %42
42: ; preds = %29, %36, %38
%43 = add nuw i32 %25, 1
%44 = icmp eq i32 %25, %7
br i1 %44, label %45, label %24
45: ; preds = %42, %11
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
}