#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 /*<<< orphan*/ shm_segsz;
};

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
int SHM_R;
int SHM_W;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmdt(void *);
int shmget(int /*<<< orphan*/, int, int);
int main(int argc, const char *argv[]) {
  int shmid = shmget(IPC_PRIVATE, 167, IPC_CREAT | SHM_R | SHM_W);
  if (shmid < 0) {
    perror("shmget()");
    return 1;
  }
  void *addr = shmat(shmid, NULL, 0);
  if (addr == (void *)-1) {
    perror("shmat()");
    return 2;
  }
  struct shmid_ds stats;
  int ret = shmctl(shmid, IPC_STAT, &stats);
  if (ret != 0) {
    perror("shmctl(IPC_STAT)");
    return 3;
  }
  printf("segment size: %zu\n", stats.shm_segsz);
  ret = shmdt(addr);
  if (ret != 0) {
    perror("shmdt()");
    return 4;
  }
  ret = shmctl(shmid, IPC_RMID, NULL);
  if (ret != 0) {
    perror("shmctl(IPC_RMID)");
    return 5;
  }
  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)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e59(%rip),%rax
or     (%rax),%edx
mov    0x2e20(%rip),%rax
or     (%rax),%edx
mov    $0xa7,%esi
call   1080 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c9 <main+0x69>
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bc <main+0x15c>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    1204 <main+0xa4>
lea    0xe16(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bc <main+0x15c>
mov    -0x14(%rbp),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <shmctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     123e <main+0xde>
lea    0xde4(%rip),%rdi
call   1060 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12bc <main+0x15c>
mov    -0x28(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <shmdt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     127b <main+0x11b>
lea    0xdcb(%rip),%rdi
call   1060 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    12bc <main+0x15c>
mov    -0x14(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12b5 <main+0x155>
lea    0xd99(%rip),%rdi
call   1060 <perror@plt>
movl   $0x5,-0x4(%rbp)
jmp    12bc <main+0x15c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e75(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    0x2e6f(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    $0xa7,%esi
call   1080 <shmget@plt>
test   %eax,%eax
js     1202 <main+0xa2>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1216 <main+0xb6>
mov    %rax,%r14
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <shmctl@plt>
test   %eax,%eax
jne    122a <main+0xca>
mov    (%rsp),%esi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <shmdt@plt>
test   %eax,%eax
jne    1239 <main+0xd9>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
test   %eax,%eax
jne    1248 <main+0xe8>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%r14d
jmp    11f7 <main+0x97>
lea    0xdec(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%r14d
jmp    11f7 <main+0x97>
mov    $0x3,%r14d
lea    0xdda(%rip),%rdi
jmp    1255 <main+0xf5>
mov    $0x4,%r14d
lea    0xdef(%rip),%rdi
jmp    1255 <main+0xf5>
mov    $0x5,%r14d
lea    0xde8(%rip),%rdi
call   1060 <perror@plt>
jmp    11f7 <main+0x97>

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@.str = [9 x i8] c"shmget()\00"
@.str.1 = [8 x i8] c"shmat()\00"
@IPC_STAT = global i32 0
@.str.2 = [17 x i8] c"shmctl(IPC_STAT)\00"
@.str.3 = [19 x i8] c"segment size: %zu\0A\00"
@.str.4 = [8 x i8] c"shmdt()\00"
@IPC_RMID = global i32 0
@.str.5 = [17 x i8] c"shmctl(IPC_RMID)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.shmid_ds
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @IPC_PRIVATE
%11 = load i32, i32* @IPC_CREAT
%12 = load i32, i32* @SHM_R
%13 = or i32 %11, %12
%14 = load i32, i32* @SHM_W
%15 = or i32 %13, %14
%16 = call i32 @shmget(i32 %10, i32 167, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %55
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i8* @shmat(i32 %22, i32* null, i32 0)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, inttoptr (i64 -1 to i8*)
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %55
28: ; preds = %21
%29 = load i32, i32* %6
%30 = load i32, i32* @IPC_STAT
%31 = call i32 @shmctl(i32 %29, i32 %30, %struct.shmid_ds* %8)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %55
36: ; preds = %28
%37 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %7
%41 = call i32 @shmdt(i8* %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 4, i32* %3
br label %55
46: ; preds = %36
%47 = load i32, i32* %6
%48 = load i32, i32* @IPC_RMID
%49 = call i32 @shmctl(i32 %47, i32 %48, %struct.shmid_ds* null)
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 5, i32* %3
br label %55
54: ; preds = %46
store i32 0, i32* %3
br label %55
55: ; preds = %54, %52, %44, %34, %26, %19
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@.str = [9 x i8] c"shmget()\00"
@.str.1 = [8 x i8] c"shmat()\00"
@IPC_STAT = global i32 0
@.str.2 = [17 x i8] c"shmctl(IPC_STAT)\00"
@.str.3 = [19 x i8] c"segment size: %zu\0A\00"
@.str.4 = [8 x i8] c"shmdt()\00"
@IPC_RMID = global i32 0
@.str.5 = [17 x i8] c"shmctl(IPC_RMID)\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.shmid_ds
%4 = load i32, i32* @IPC_PRIVATE
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @SHM_R
%7 = or i32 %6, %5
%8 = load i32, i32* @SHM_W
%9 = or i32 %7, %8
%10 = tail call i32 @shmget(i32 %4, i32 167, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %40
14: ; preds = %2
%15 = tail call i8* @shmat(i32 %10, i32* null, i32 0)
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %40
19: ; preds = %14
%20 = bitcast %struct.shmid_ds* %3 to i8*
%21 = load i32, i32* @IPC_STAT
%22 = call i32 @shmctl(i32 %10, i32 %21, %struct.shmid_ds* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @shmdt(i8* %15)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @IPC_RMID
%32 = call i32 @shmctl(i32 %10, i32 %31, %struct.shmid_ds* null)
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30, %24, %19
%35 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %19 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %24 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), %30 ]
%36 = phi i32 [ 3, %19 ], [ 4, %24 ], [ 5, %30 ]
%37 = call i32 @perror(i8* %35)
br label %38
38: ; preds = %34, %30
%39 = phi i32 [ 0, %30 ], [ %36, %34 ]
br label %40
40: ; preds = %17, %38, %12
%41 = phi i32 [ 1, %12 ], [ 2, %17 ], [ %39, %38 ]
ret i32 %41
}