#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 ipc_perm {
  int mode;
  int uid;
};
struct shmid_ds {
  int /*<<< orphan*/ shm_nattch;
  int /*<<< orphan*/ shm_segsz;
  struct ipc_perm shm_perm;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPC_STAT;
int atoi(char const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int main(int argc, char const *argv[]) {
  if (argc <= 1) {
    printf("An Identifier must be passed through as an argument\n");
    printf("Usage: './shminfo [sharedMemID]'\n");
    exit(EXIT_FAILURE);
  }
  int sharedMemID = atoi(argv[1]);
  struct shmid_ds *shm_info;
  struct ipc_perm *shm_info_perm;
  if (shmctl(sharedMemID, IPC_STAT, shm_info) == 0) {
    shm_info_perm = &(shm_info->shm_perm);
    printf("Segment ID:     %d\n", sharedMemID);
    printf("I was unable to find where the key was located in mysystem\n");
    printf("Mode:           %u\n", shm_info_perm->mode);
    printf("Owner UID       %u\n", shm_info_perm->uid);
    printf("Size	        %lu\n", shm_info->shm_segsz);
    printf("Num attaches    %lu\n", shm_info->shm_nattch);
  } else
    printf("Failure Gettting info: Id may be incorrect\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     118a <main+0x4a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1040 <shmctl@plt>
cmp    $0x0,%rax
jne    123b <main+0xfb>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1249 <main+0x109>
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jle    11e0 <main+0x90>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <shmctl@plt>
test   %rax,%rax
je     1188 <main+0x38>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
jmp    11dc <main+0x8c>
lea    0xe71(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
lea    0xf0a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i32, i32, %struct.ipc_perm }
%struct.ipc_perm = type { i32, i32 }
@.str = [53 x i8] c"An Identifier must be passed through as an argument\0A\00"
@.str.1 = [34 x i8] c"Usage: './shminfo [sharedMemID]'\0A\00"
@EXIT_FAILURE = global i32 0
@IPC_STAT = global i32 0
@.str.2 = [20 x i8] c"Segment ID: %d\0A\00"
@.str.3 = [60 x i8] c"I was unable to find where the key was located in mysystem\0A\00"
@.str.4 = [20 x i8] c"Mode: %u\0A\00"
@.str.5 = [20 x i8] c"Owner UID %u\0A\00"
@.str.6 = [18 x i8] c"Size\09 %lu\0A\00"
@.str.7 = [21 x i8] c"Num attaches %lu\0A\00"
@.str.8 = [44 x i8] c"Failure Gettting info: Id may be incorrect\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.shmid_ds*
%8 = alloca %struct.ipc_perm*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32, i32* @IPC_STAT
%23 = load %struct.shmid_ds*, %struct.shmid_ds** %7
%24 = call i64 @shmctl(i32 %21, i32 %22, %struct.shmid_ds* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %48
26: ; preds = %16
%27 = load %struct.shmid_ds*, %struct.shmid_ds** %7
%28 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %27, i32 0, i32 2
store %struct.ipc_perm* %28, %struct.ipc_perm** %8
%29 = load i32, i32* %6
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
%32 = load %struct.ipc_perm*, %struct.ipc_perm** %8
%33 = getelementptr inbounds %struct.ipc_perm, %struct.ipc_perm* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = load %struct.ipc_perm*, %struct.ipc_perm** %8
%37 = getelementptr inbounds %struct.ipc_perm, %struct.ipc_perm* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = load %struct.shmid_ds*, %struct.shmid_ds** %7
%41 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = load %struct.shmid_ds*, %struct.shmid_ds** %7
%45 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %46)
br label %50
48: ; preds = %16
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0))
br label %50
50: ; preds = %48, %26
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32, i32, %struct.ipc_perm }
%struct.ipc_perm = type { i32, i32 }
@EXIT_FAILURE = global i32 0
@IPC_STAT = global i32 0
@.str.2 = [20 x i8] c"Segment ID: %d\0A\00"
@.str.4 = [20 x i8] c"Mode: %u\0A\00"
@.str.5 = [20 x i8] c"Owner UID %u\0A\00"
@.str.6 = [18 x i8] c"Size\09 %lu\0A\00"
@.str.7 = [21 x i8] c"Num attaches %lu\0A\00"
@str = [43 x i8] c"Failure Gettting info: Id may be incorrect\00"
@str.9 = [59 x i8] c"I was unable to find where the key was located in mysystem\00"
@str.10 = [52 x i8] c"An Identifier must be passed through as an argument\00"
@str.11 = [33 x i8] c"Usage: './shminfo [sharedMemID]'\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.10, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.11, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @IPC_STAT
%14 = tail call i64 @shmctl(i32 %12, i32 %13, %struct.shmid_ds* undef)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %9
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.9, i64 0, i64 0))
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 undef)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 undef)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 undef)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 undef)
br label %25
23: ; preds = %9
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
ret i32 0
}