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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmdt(char *);
int shmget(int, int, int);
int main() {
  int shmid, shmer;
  key_t key;
  char *shm, *s;
  char c0 = '\0';
  key = 5678;
  if ((shmid = shmget(key, 27, 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  for (s = shm; *s != c0; s++)
    putchar(*s);
  putchar('\n');
  *shm = '*';
  if ((shmer = shmdt(shm)) == -1) {
    perror("shmdt");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x162e,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    $0x1b,%esi
mov    $0x1b6,%edx
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ae <main+0x4e>
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11e6 <main+0x86>
lea    0xe30(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
je     121e <main+0xbe>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11ee <main+0x8e>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
movb   $0x2a,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    125a <main+0xfa>
lea    0xdc2(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x162e,%edi
mov    $0x1b,%esi
mov    $0x1b6,%edx
call   1070 <shmget@plt>
test   %eax,%eax
jns    1185 <main+0x25>
lea    0xe7d(%rip),%rdi
jmp    11e5 <main+0x85>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    119f <main+0x3f>
lea    0xe6a(%rip),%rdi
jmp    11e5 <main+0x85>
mov    %rax,%r14
mov    (%rax),%al
test   %al,%al
je     11c3 <main+0x63>
mov    %r14,%rbx
add    $0x1,%rbx
nop
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11b0 <main+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x2a,(%r14)
mov    %r14,%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    11f4 <main+0x94>
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [6 x i8] c"shmdt\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
store i32 0, i32* %1
store i8 0, i8* %7
store i32 5678, i32* %4
%8 = load i32, i32* %4
%9 = call i32 @shmget(i32 %8, i32 27, i32 438)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i8* @shmat(i32 %15, i32* null, i32 0)
store i8* %16, i8** %5
%17 = icmp eq i8* %16, inttoptr (i64 -1 to i8*)
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i8*, i8** %5
store i8* %22, i8** %6
br label %23
23: ; preds = %34, %21
%24 = load i8*, i8** %6
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = load i8, i8* %7
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %26, %28
br i1 %29, label %30, label %37
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = load i8, i8* %31
%33 = call i32 @putchar(i8 signext %32)
br label %34
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %6
br label %23
37: ; preds = %23
%38 = call i32 @putchar(i8 signext 10)
%39 = load i8*, i8** %5
store i8 42, i8* %39
%40 = load i8*, i8** %5
%41 = call i32 @shmdt(i8* %40)
store i32 %41, i32* %3
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [6 x i8] c"shmdt\00"
define i32 @main() {
%1 = tail call i32 @shmget(i32 5678, i32 27, i32 438)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i8* @shmat(i32 %1, i32* null, i32 0)
%8 = icmp eq i8* %7, inttoptr (i64 -1 to i8*)
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = load i8, i8* %7
%11 = icmp eq i8 %10, 0
br i1 %11, label %22, label %15
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9, %15
%16 = phi i8 [ %20, %15 ], [ %10, %9 ]
%17 = phi i8* [ %19, %15 ], [ %7, %9 ]
%18 = tail call i32 @putchar(i8 signext %16)
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %15
22: ; preds = %15, %9
%23 = tail call i32 @putchar(i8 signext 10)
store i8 42, i8* %7
%24 = tail call i32 @shmdt(i8* %7)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = tail call i32 @exit(i32 0)
unreachable
}