#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 */
typedef scalar_t__ key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int S_IRUSR;
int S_IWUSR;
scalar_t__ getpid();
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(char *);
int shmget(scalar_t__, size_t, int);
int main(void) {
  int shm_id, ret;
  key_t key;
  size_t sz = 64 * 1024;
  char *addr;
  key = (key_t)getpid();
  shm_id = shmget(key, sz, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
  if (shm_id < 0)
    return -1;
  addr = shmat(shm_id, NULL, 0);
  if (addr == (char *)-1)
    goto reap;
  (void)shmdt(addr);
reap:
  ret = shmctl(shm_id, IPC_RMID, NULL);
  (void)ret;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x10000,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    0x2e66(%rip),%rax
or     (%rax),%edx
mov    0x2e35(%rip),%rax
or     (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bc <main+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <main+0xbc>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    11e4 <main+0x94>
jmp    11ed <main+0x9d>
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2e5f(%rip),%rcx
mov    0x2e88(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e55(%rip),%rcx
or     (%rcx),%edx
mov    0x2e64(%rip),%rcx
or     (%rcx),%edx
mov    $0x10000,%esi
mov    %rax,%rdi
call   1070 <shmget@plt>
test   %eax,%eax
js     11c0 <main+0x70>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11ac <main+0x5c>
mov    %rax,%rdi
call   1030 <shmdt@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
jmp    11c5 <main+0x75>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i32 0, i32* %1
store i64 65536, i64* %5
%7 = call i64 (...) @getpid()
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = load i64, i64* %5
%10 = load i32, i32* @IPC_CREAT
%11 = load i32, i32* @IPC_EXCL
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 @shmget(i64 %8, i64 %9, i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %0
store i32 -1, i32* %1
br label %35
21: ; preds = %0
%22 = load i32, i32* %2
%23 = call i8* @shmat(i32 %22, i32* null, i32 0)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp eq i8* %24, inttoptr (i64 -1 to i8*)
br i1 %25, label %26, label %27
26: ; preds = %21
br label %30
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = call i32 @shmdt(i8* %28)
br label %30
30: ; preds = %27, %26
%31 = load i32, i32* %2
%32 = load i32, i32* @IPC_RMID
%33 = call i32 @shmctl(i32 %31, i32 %32, i32* null)
store i32 %33, i32* %3
%34 = load i32, i32* %3
store i32 0, i32* %1
br label %35
35: ; preds = %30, %20
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @getpid()
%2 = load i32, i32* @IPC_CREAT
%3 = load i32, i32* @IPC_EXCL
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRUSR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = tail call i32 @shmget(i64 %1, i64 65536, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %0
%12 = tail call i8* @shmat(i32 %9, i32* null, i32 0)
%13 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @shmdt(i8* %12)
br label %16
16: ; preds = %11, %14
%17 = load i32, i32* @IPC_RMID
%18 = tail call i32 @shmctl(i32 %9, i32 %17, i32* null)
br label %19
19: ; preds = %0, %16
%20 = phi i32 [ 0, %16 ], [ -1, %0 ]
ret i32 %20
}