#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 */
struct shmid_ds {
  int shm_segsz;
  int shm_cpid;
  int shm_lpid;
  int shm_nattch;
  int shm_atime;
  int shm_dtime;
  int shm_ctime;
  int /*<<< orphan*/ shm_perm;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ dump_perm(int /*<<< orphan*/ *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int dump_shm(char const *prompt, int mid, int with_perm) {
  struct shmid_ds mbuf;
  int ret = shmctl(mid, IPC_STAT, &mbuf);
  if (ret < 0)
    err_sys("shmctl to stat shm failed");
  printf("%s: \n"
         "   shm id: %d\n"
         "   segment size: %d\n"
         "   creator: %d\n"
         "   last shmop pid: %d\n"
         "   attach process: %d\n"
         "   last attach time: %d\n"
         "   last detach time: %d\n"
         "   last change time: %d\n",
         prompt, mid, mbuf.shm_segsz, mbuf.shm_cpid, mbuf.shm_lpid,
         mbuf.shm_nattch, mbuf.shm_atime, mbuf.shm_dtime, mbuf.shm_ctime);
  int n = 0;
  printf("   \n");
  if (with_perm)
    dump_perm(&mbuf.shm_perm);
  return mbuf.shm_segsz;
}


x86 assembly (clang, -O0):
<dump_shm>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x58,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <shmctl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1181 <dump_shm+0x41>
lea    0xe84(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x2c(%rbp),%ebx
mov    -0x28(%rbp),%r11d
mov    -0x24(%rbp),%r10d
mov    -0x20(%rbp),%eax
lea    0xe72(%rip),%rdi
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x40(%rbp)
lea    0xef7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     11ec <dump_shm+0xac>
lea    -0x38(%rbp),%rdi
add    $0x1c,%rdi
call   1050 <dump_perm@plt>
mov    -0x38(%rbp),%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_shm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1070 <shmctl@plt>
test   %eax,%eax
jns    1187 <dump_shm+0x37>
lea    0xe7e(%rip),%rdi
call   1040 <err_sys@plt>
mov    (%rsp),%ecx
mov    0x4(%rsp),%r8d
mov    0x8(%rsp),%r9d
mov    0xc(%rsp),%r10d
mov    0x10(%rsp),%r11d
mov    0x14(%rsp),%r12d
mov    0x18(%rsp),%ebx
lea    0xe6c(%rip),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
push   %rbx
push   %r12
push   %r11
push   %r10
call   1050 <printf@plt>
add    $0x20,%rsp
lea    0xef9(%rip),%rdi
call   1030 <puts@plt>
test   %r14d,%r14d
je     11e0 <dump_shm+0x90>
lea    0x1c(%rsp),%rdi
call   1060 <dump_perm@plt>
mov    (%rsp),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i32, i32, i32, i32, i32, i32, i32, i32 }
@IPC_STAT = global i32 0
@.str = [26 x i8] c"shmctl to stat shm failed\00"
@.str.1 = [171 x i8] c"%s: \0A shm id: %d\0A segment size: %d\0A creator: %d\0A last shmop pid: %d\0A attach process: %d\0A last attach time: %d\0A last detach time: %d\0A last change time: %d\0A\00"
@.str.2 = [5 x i8] c" \0A\00"
define i32 @dump_shm(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.shmid_ds
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %5
%11 = load i32, i32* @IPC_STAT
%12 = call i32 @shmctl(i32 %10, i32 %11, %struct.shmid_ds* %7)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @err_sys(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %3
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 2
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 5
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 6
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %19, i32 %21, i32 %23, i32 %25, i32 %27, i32 %29, i32 %31, i32 %33)
store i32 0, i32* %9
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %17
%39 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 7
%40 = call i32 @dump_perm(i32* %39)
br label %41
41: ; preds = %38, %17
%42 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 0
%43 = load i32, i32* %42
ret i32 %43
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32, i32, i32, i32, i32, i32, i32, i32 }
@IPC_STAT = global i32 0
@.str = [26 x i8] c"shmctl to stat shm failed\00"
@.str.1 = [171 x i8] c"%s: \0A shm id: %d\0A segment size: %d\0A creator: %d\0A last shmop pid: %d\0A attach process: %d\0A last attach time: %d\0A last detach time: %d\0A last change time: %d\0A\00"
@str = [4 x i8] c" \00"
define i32 @dump_shm(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.shmid_ds
%5 = bitcast %struct.shmid_ds* %4 to i8*
%6 = load i32, i32* @IPC_STAT
%7 = call i32 @shmctl(i32 %1, i32 %6, %struct.shmid_ds* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @err_sys(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 2
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 5
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 6
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i32 %13, i32 %15, i32 %17, i32 %19, i32 %21, i32 %23, i32 %25)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%28 = icmp eq i32 %2, 0
br i1 %28, label %32, label %29
29: ; preds = %11
%30 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 7
%31 = call i32 @dump_perm(i32* %30)
br label %32
32: ; preds = %29, %11
%33 = load i32, i32* %12
ret i32 %33
}