#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 dummy;
};
typedef int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int main() {
  char c, c0 = '\0';
  int shmid, shmer;
  key_t key;
  char *shm, *s;
  struct shmid_ds shmds;
  key = 5678;
  if ((shmid = shmget(key, 27, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shmer = shmctl(shmid, IPC_STAT, &shmds))) {
    perror("shmctl STAT");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  s = shm;
  for (c = 'a'; c <= 'z'; c++)
    *s++ = c;
  *s = c0;
  while (*shm != '*')
    sleep(1);
  if ((shmer = shmctl(shmid, IPC_RMID, &shmds)) == -1) {
    perror("shmctl RMID");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x6(%rbp)
movl   $0x162e,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1b,%esi
call   1060 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11b8 <main+0x58>
lea    0xe57(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <shmctl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     11ef <main+0x8f>
lea    0xe27(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1227 <main+0xc7>
lea    0xdfb(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movb   $0x61,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x7a,%eax
jg     1261 <main+0x101>
mov    -0x5(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x5(%rbp),%al
add    $0x1,%al
mov    %al,-0x5(%rbp)
jmp    1233 <main+0xd3>
mov    -0x6(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     1289 <main+0x129>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    126a <main+0x10a>
mov    -0xc(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <shmctl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    12c0 <main+0x160>
lea    0xd68(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x162e,%edi
mov    $0x1b,%esi
call   1060 <shmget@plt>
test   %eax,%eax
jns    1190 <main+0x30>
lea    0xe85(%rip),%rdi
jmp    122e <main+0xce>
mov    %eax,%ebp
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <shmctl@plt>
test   %eax,%eax
jne    11c3 <main+0x63>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11cc <main+0x6c>
lea    0xe62(%rip),%rdi
jmp    122e <main+0xce>
lea    0xe4d(%rip),%rdi
jmp    122e <main+0xce>
mov    %rax,%rbx
movaps 0xe2a(%rip),%xmm0
movups %xmm0,(%rax)
movabs $0x7877767574737271,%rax
76 77 78
mov    %rax,0x10(%rbx)
movw   $0x7a79,0x18(%rbx)
movb   $0x0,0x1a(%rbx)
cmpb   $0x2a,(%rbx)
je     120f <main+0xaf>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1080 <sleep@plt>
cmpb   $0x2a,(%rbx)
jne    1200 <main+0xa0>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    123d <main+0xdd>
lea    0xdfb(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@IPC_STAT = global i32 0
@.str.1 = [12 x i8] c"shmctl STAT\00"
@.str.2 = [6 x i8] c"shmat\00"
@IPC_RMID = global i32 0
@.str.3 = [12 x i8] c"shmctl RMID\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.shmid_ds
store i32 0, i32* %1
store i8 0, i8* %3
store i32 5678, i32* %6
%10 = load i32, i32* %6
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 438
%13 = call i32 @shmget(i32 %10, i32 27, i32 %12)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load i32, i32* %4
%20 = load i32, i32* @IPC_STAT
%21 = call i32 @shmctl(i32 %19, i32 %20, %struct.shmid_ds* %9)
store i32 %21, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* %4
%28 = call i8* @shmat(i32 %27, i32* null, i32 0)
store i8* %28, i8** %7
%29 = icmp eq i8* %28, inttoptr (i64 -1 to i8*)
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = load i8*, i8** %7
store i8* %34, i8** %8
store i8 97, i8* %2
br label %35
35: ; preds = %43, %33
%36 = load i8, i8* %2
%37 = sext i8 %36 to i32
%38 = icmp sle i32 %37, 122
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i8, i8* %2
%41 = load i8*, i8** %8
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %8
store i8 %40, i8* %41
br label %43
43: ; preds = %39
%44 = load i8, i8* %2
%45 = add i8 %44, 1
store i8 %45, i8* %2
br label %35
46: ; preds = %35
%47 = load i8, i8* %3
%48 = load i8*, i8** %8
store i8 %47, i8* %48
br label %49
49: ; preds = %54, %46
%50 = load i8*, i8** %7
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %52, 42
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @sleep(i32 1)
br label %49
56: ; preds = %49
%57 = load i32, i32* %4
%58 = load i32, i32* @IPC_RMID
%59 = call i32 @shmctl(i32 %57, i32 %58, %struct.shmid_ds* %9)
store i32 %59, i32* %5
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %56
%65 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@IPC_STAT = global i32 0
@.str.1 = [12 x i8] c"shmctl STAT\00"
@.str.2 = [6 x i8] c"shmat\00"
@IPC_RMID = global i32 0
@.str.3 = [12 x i8] c"shmctl RMID\00"
define i32 @main() {
%1 = alloca %struct.shmid_ds
%2 = bitcast %struct.shmid_ds* %1 to i8*
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 438
%5 = tail call i32 @shmget(i32 5678, i32 27, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @IPC_STAT
%12 = call i32 @shmctl(i32 %5, i32 %11, %struct.shmid_ds* %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = call i8* @shmat(i32 %5, i32* null, i32 0)
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %35, label %20
20: ; preds = %17
%21 = getelementptr inbounds i8, i8* %18, i64 16
%22 = bitcast i8* %18 to <16 x i8>*
store <16 x i8> <i8 97, i8 98, i8 99, i8 100, i8 101, i8 102, i8 103, i8 104, i8 105, i8 106, i8 107, i8 108, i8 109, i8 110, i8 111, i8 112>, <16 x i8>* %22
%23 = getelementptr inbounds i8, i8* %18, i64 17
store i8 113, i8* %21
%24 = getelementptr inbounds i8, i8* %18, i64 18
store i8 114, i8* %23
%25 = getelementptr inbounds i8, i8* %18, i64 19
store i8 115, i8* %24
%26 = getelementptr inbounds i8, i8* %18, i64 20
store i8 116, i8* %25
%27 = getelementptr inbounds i8, i8* %18, i64 21
store i8 117, i8* %26
%28 = getelementptr inbounds i8, i8* %18, i64 22
store i8 118, i8* %27
%29 = getelementptr inbounds i8, i8* %18, i64 23
store i8 119, i8* %28
%30 = getelementptr inbounds i8, i8* %18, i64 24
store i8 120, i8* %29
%31 = getelementptr inbounds i8, i8* %18, i64 25
store i8 121, i8* %30
%32 = getelementptr inbounds i8, i8* %18, i64 26
store i8 122, i8* %31
store i8 0, i8* %32
%33 = load i8, i8* %18
%34 = icmp eq i8 %33, 42
br i1 %34, label %42, label %38
35: ; preds = %17
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20, %38
%39 = call i32 @sleep(i32 1)
%40 = load i8, i8* %18
%41 = icmp eq i8 %40, 42
br i1 %41, label %42, label %38
42: ; preds = %38, %20
%43 = load i32, i32* @IPC_RMID
%44 = call i32 @shmctl(i32 %5, i32 %43, %struct.shmid_ds* %1)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42
%50 = call i32 @exit(i32 0)
unreachable
}