#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 shmid_ds {
  scalar_t__ shm_nattch;
  scalar_t__ shm_segsz;
  TYPE_1__ shm_perm;
};
struct shm_info {
  int used_ids;
  int /*<<< orphan*/ shm_tot;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EINVAL;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SHM_INFO;
int /*<<< orphan*/ SHM_STAT;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int main() {
  int i, maxind, shmid;
  struct shmid_ds ds;
  struct shm_info info;
  maxind = shmctl(0, SHM_INFO, (struct shmid_ds *)&info);
  if (maxind == -1)
    pexit("shmctl");
  printf("Total shared memory segments: %d\n", info.used_ids);
  printf("Number of memory pages these occupy: %ld\n", info.shm_tot);
  printf("\n%10s\t%10s\t%10s\t%10s\n", "ID", "key", "size (b)", "processes");
  for (i = 0; i <= maxind; i++) {
    shmid = shmctl(i, SHM_STAT, &ds);
    if (shmid == -1) {
      if (errno != EINVAL && errno != EACCES)
        pexit("semctl");
      continue;
    }
    printf("%10d\t%10ld\t%10ld\t%10ld\n", shmid, (long)ds.shm_perm.__key,
           (long)ds.shm_segsz, (long)ds.shm_nattch);
  }
  printf("\n");
  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),%esi
lea    -0x30(%rbp),%rdx
xor    %edi,%edi
call   1040 <shmctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    117c <main+0x3c>
lea    0xe89(%rip),%rdi
call   1060 <pexit@plt>
mov    -0x30(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%esi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeae(%rip),%rdi
lea    0xebd(%rip),%rsi
lea    0xeb9(%rip),%rdx
lea    0xeb6(%rip),%rcx
lea    0xeb8(%rip),%r8
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     126d <main+0x12d>
mov    -0x8(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1242 <main+0x102>
mov    0x2dc4(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
je     123d <main+0xfd>
mov    0x2daa(%rip),%rax
mov    (%rax),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
je     123d <main+0xfd>
lea    0xe4b(%rip),%rdi
call   1060 <pexit@plt>
jmp    125f <main+0x11f>
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11cf <main+0x8f>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d36(%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    $0x28,%rsp
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1050 <shmctl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1186 <main+0x36>
lea    0xe7f(%rip),%rdi
call   1070 <pexit@plt>
mov    0x8(%rsp),%esi
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea2(%rip),%rdi
lea    0xeb1(%rip),%rsi
lea    0xead(%rip),%rdx
lea    0xeaa(%rip),%rcx
lea    0xeac(%rip),%r8
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jns    11f0 <main+0xa0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x1,%ebx
mov    0x2dd6(%rip),%r13
lea    0x10(%rsp),%r14
mov    0x2dba(%rip),%r15
lea    0xe7d(%rip),%r12
xor    %ebp,%ebp
jmp    1242 <main+0xf2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rdx
mov    0x10(%rsp),%r8
mov    0x18(%rsp),%rcx
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     11d8 <main+0x88>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0xd0>
mov    0x2d6c(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
je     123b <main+0xeb>
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
je     123b <main+0xeb>
lea    0xe0c(%rip),%rdi
call   1070 <pexit@plt>
jmp    123b <main+0xeb>

LLVM IR (-O0):
%struct.shmid_ds = type { i64, i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.shm_info = type { i32, i32 }
@SHM_INFO = global i32 0
@.str = [7 x i8] c"shmctl\00"
@.str.1 = [34 x i8] c"Total shared memory segments: %d\0A\00"
@.str.2 = [42 x i8] c"Number of memory pages these occupy: %ld\0A\00"
@.str.3 = [22 x i8] c"\0A%10s\09%10s\09%10s\09%10s\0A\00"
@.str.4 = [3 x i8] c"ID\00"
@.str.5 = [4 x i8] c"key\00"
@.str.6 = [9 x i8] c"size (b)\00"
@.str.7 = [10 x i8] c"processes\00"
@SHM_STAT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str.8 = [7 x i8] c"semctl\00"
@.str.9 = [24 x i8] c"%10d\09%10ld\09%10ld\09%10ld\0A\00"
@.str.10 = [2 x i8] c"\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.shmid_ds
%6 = alloca %struct.shm_info
store i32 0, i32* %1
%7 = load i32, i32* @SHM_INFO
%8 = bitcast %struct.shm_info* %6 to %struct.shmid_ds*
%9 = call i32 @shmctl(i32 0, i32 %7, %struct.shmid_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 = getelementptr inbounds %struct.shm_info, %struct.shm_info* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds %struct.shm_info, %struct.shm_info* %6, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %2
br label %22
22: ; preds = %53, %14
%23 = load i32, i32* %2
%24 = load i32, i32* %3
%25 = icmp sle i32 %23, %24
br i1 %25, label %26, label %56
26: ; preds = %22
%27 = load i32, i32* %2
%28 = load i32, i32* @SHM_STAT
%29 = call i32 @shmctl(i32 %27, i32 %28, %struct.shmid_ds* %5)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%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 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %42
42: ; preds = %40, %36, %32
br label %53
43: ; preds = %26
%44 = load i32, i32* %4
%45 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i32 0, i32 2
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i32 0, i32 1
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i32 0, i32 0
%51 = load i64, i64* %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i32 %44, i64 %47, i64 %49, i64 %51)
br label %53
53: ; preds = %43, %42
%54 = load i32, i32* %2
%55 = add nsw i32 %54, 1
store i32 %55, i32* %2
br label %22
56: ; preds = %22
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%58 = load i32, i32* @EXIT_SUCCESS
%59 = call i32 @exit(i32 %58)
unreachable
}

LLVM IR (-O2):
%struct.shmid_ds = type { i64, i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.shm_info = type { i32, i32 }
@SHM_INFO = global i32 0
@.str = [7 x i8] c"shmctl\00"
@.str.1 = [34 x i8] c"Total shared memory segments: %d\0A\00"
@.str.2 = [42 x i8] c"Number of memory pages these occupy: %ld\0A\00"
@.str.3 = [22 x i8] c"\0A%10s\09%10s\09%10s\09%10s\0A\00"
@.str.4 = [3 x i8] c"ID\00"
@.str.5 = [4 x i8] c"key\00"
@.str.6 = [9 x i8] c"size (b)\00"
@.str.7 = [10 x i8] c"processes\00"
@SHM_STAT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str.8 = [7 x i8] c"semctl\00"
@.str.9 = [24 x i8] c"%10d\09%10ld\09%10ld\09%10ld\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.shmid_ds
%2 = alloca %struct.shm_info
%3 = bitcast %struct.shmid_ds* %1 to i8*
%4 = bitcast %struct.shm_info* %2 to i8*
%5 = load i32, i32* @SHM_INFO
%6 = bitcast %struct.shm_info* %2 to %struct.shmid_ds*
%7 = call i32 @shmctl(i32 0, i32 %5, %struct.shmid_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 = getelementptr inbounds %struct.shm_info, %struct.shm_info* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds %struct.shm_info, %struct.shm_info* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%19 = icmp slt i32 %7, 0
br i1 %19, label %46, label %20
20: ; preds = %11
%21 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 2, i32 0
%22 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 1
%23 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 0
br label %24
24: ; preds = %20, %43
%25 = phi i32 [ 0, %20 ], [ %44, %43 ]
%26 = load i32, i32* @SHM_STAT
%27 = call i32 @shmctl(i32 %25, i32 %26, %struct.shmid_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 %43, label %36
36: ; preds = %29
%37 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %43
38: ; preds = %24
%39 = load i64, i64* %21
%40 = load i64, i64* %22
%41 = load i64, i64* %23
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i32 %27, i64 %39, i64 %40, i64 %41)
br label %43
43: ; preds = %29, %36, %38
%44 = add nuw i32 %25, 1
%45 = icmp eq i32 %25, %7
br i1 %45, label %46, label %24
46: ; preds = %43, %11
%47 = call i32 @putchar(i32 10)
%48 = load i32, i32* @EXIT_SUCCESS
%49 = call i32 @exit(i32 %48)
unreachable
}