#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*/ die(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int main() {
  int shmid;
  key_t key;
  char *shm, *s;
  key = 5678;
  if ((shmid = shmget(key, 27, 0666)) < 0)
    die("shmget");
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1)
    die("shmat");
  for (s = shm; *s != '\0'; s++)
    putchar(*s);
  putchar('\n');
  *shm = '*';
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x162e,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1b,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1190 <main+0x40>
lea    0xe75(%rip),%rdi
call   1040 <die@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11be <main+0x6e>
lea    0xe4e(%rip),%rdi
call   1040 <die@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f3 <main+0xa3>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c6 <main+0x76>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
movb   $0x2a,(%rax)
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x162e,%edi
mov    $0x1b,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    117a <main+0x2a>
lea    0xe8b(%rip),%rdi
call   1040 <die@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
jne    119a <main+0x4a>
lea    0xe72(%rip),%rdi
call   1040 <die@plt>
mov    (%r14),%al
test   %al,%al
je     11c3 <main+0x73>
mov    %r14,%rbx
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11b0 <main+0x60>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x2a,(%r14)
xor    %edi,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\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 %5
3: ; preds = %0
%4 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = tail call i8* @shmat(i32 %1, i32* null, i32 0)
%7 = icmp eq i8* %6, inttoptr (i64 -1 to i8*)
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = load i8, i8* %6
%12 = icmp eq i8 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %10, %13
%14 = phi i8 [ %18, %13 ], [ %11, %10 ]
%15 = phi i8* [ %17, %13 ], [ %6, %10 ]
%16 = tail call i32 @putchar(i8 signext %14)
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %13
20: ; preds = %13, %10
%21 = tail call i32 @putchar(i8 signext 10)
store i8 42, i8* %6
%22 = tail call i32 @exit(i32 0)
unreachable
}