#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 TYPE_2__ {
  scalar_t__ __key;
};
struct msqid_ds {
  scalar_t__ msg_qnum;
  TYPE_1__ msg_perm;
};
struct msginfo {
  int dummy;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EINVAL;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ MSG_INFO;
int /*<<< orphan*/ MSG_STAT;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int msgctl(int, int /*<<< orphan*/, struct msqid_ds *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int maxind, ind, msqid;
  struct msqid_ds ds;
  struct msginfo msginfo;
  maxind = msgctl(0, MSG_INFO, (struct msqid_ds *)&msginfo);
  if (maxind == -1)
    perror("msgctl-MSG_INFO");
  printf("maxind: %d\n\n", maxind);
  printf("index     id       key      messages\n");
  for (ind = 0; ind <= maxind; ind++) {
    msqid = msgctl(ind, MSG_STAT, &ds);
    if (msqid == -1) {
      if (errno != EINVAL && errno != EACCES)
        perror("msgctl-MSG_STAT");
      continue;
    }
    printf("%4d %8d  0x%08lx %7ld\n", ind, msqid,
           (unsigned long)ds.msg_perm.__key, (long)ds.msg_qnum);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
xor    %edi,%edi
call   1040 <msgctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1183 <main+0x43>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1246 <main+0x106>
mov    -0x18(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <msgctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121c <main+0xdc>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <main+0xd7>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <main+0xd7>
lea    0xe31(%rip),%rdi
call   1050 <perror@plt>
jmp    1238 <main+0xf8>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11a9 <main+0x69>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1050 <msgctl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1184 <main+0x34>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
lea    0xe85(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jns    11b2 <main+0x62>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x1,%ebx
mov    0x2e34(%rip),%r13
lea    0x8(%rsp),%r14
mov    0x2df8(%rip),%r15
lea    0xe5e(%rip),%r12
xor    %ebp,%ebp
jmp    11ff <main+0xaf>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%r8
mov    0x10(%rsp),%rcx
mov    %r12,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     11a4 <main+0x54>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <msgctl@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x90>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
je     11f8 <main+0xa8>
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
je     11f8 <main+0xa8>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
jmp    11f8 <main+0xa8>

LLVM IR (-O0):
%struct.msqid_ds = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.msginfo = type { i32 }
@MSG_INFO = global i32 0
@.str = [16 x i8] c"msgctl-MSG_INFO\00"
@.str.1 = [13 x i8] c"maxind: %d\0A\0A\00"
@.str.2 = [38 x i8] c"index id key messages\0A\00"
@MSG_STAT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str.3 = [16 x i8] c"msgctl-MSG_STAT\00"
@.str.4 = [23 x i8] c"%4d %8d 0x%08lx %7ld\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.msqid_ds
%10 = alloca %struct.msginfo
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @MSG_INFO
%12 = bitcast %struct.msginfo* %10 to %struct.msqid_ds*
%13 = call i32 @msgctl(i32 0, i32 %11, %struct.msqid_ds* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %22
22: ; preds = %52, %18
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp sle i32 %23, %24
br i1 %25, label %26, label %55
26: ; preds = %22
%27 = load i32, i32* %7
%28 = load i32, i32* @MSG_STAT
%29 = call i32 @msgctl(i32 %27, i32 %28, %struct.msqid_ds* %9)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %43
32: ; preds = %26
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINVAL
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EACCES
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %36, %32
br label %52
43: ; preds = %26
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %9, i32 0, i32 1
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %9, i32 0, i32 0
%50 = load i64, i64* %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 %45, i64 %48, i64 %50)
br label %52
52: ; preds = %43, %42
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %22
55: ; preds = %22
%56 = load i32, i32* @EXIT_SUCCESS
%57 = call i32 @exit(i32 %56)
unreachable
}

LLVM IR (-O2):
%struct.msqid_ds = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.msginfo = type { i32 }
@MSG_INFO = global i32 0
@.str = [16 x i8] c"msgctl-MSG_INFO\00"
@.str.1 = [13 x i8] c"maxind: %d\0A\0A\00"
@MSG_STAT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str.3 = [16 x i8] c"msgctl-MSG_STAT\00"
@.str.4 = [23 x i8] c"%4d %8d 0x%08lx %7ld\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [37 x i8] c"index id key messages\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.msqid_ds
%4 = alloca %struct.msginfo
%5 = bitcast %struct.msqid_ds* %3 to i8*
%6 = bitcast %struct.msginfo* %4 to i8*
%7 = load i32, i32* @MSG_INFO
%8 = bitcast %struct.msginfo* %4 to %struct.msqid_ds*
%9 = call i32 @msgctl(i32 0, i32 %7, %struct.msqid_ds* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%16 = icmp slt i32 %9, 0
br i1 %16, label %41, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 1, i32 0
%19 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 0
br label %20
20: ; preds = %17, %38
%21 = phi i32 [ 0, %17 ], [ %39, %38 ]
%22 = load i32, i32* @MSG_STAT
%23 = call i32 @msgctl(i32 %21, i32 %22, %struct.msqid_ds* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINVAL
%28 = icmp eq i64 %26, %27
%29 = load i64, i64* @EACCES
%30 = icmp eq i64 %26, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %38, label %32
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %38
34: ; preds = %20
%35 = load i64, i64* %18
%36 = load i64, i64* %19
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32 %23, i64 %35, i64 %36)
br label %38
38: ; preds = %25, %32, %34
%39 = add nuw i32 %21, 1
%40 = icmp eq i32 %21, %9
br i1 %40, label %41, label %20
41: ; preds = %38, %13
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
}