#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 EXIT_SUCCESS;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ shmdt(char *);
int shmget(int, int, int);
int main() {
  int shmid;
  key_t key;
  char *shm, *s;
  key = 5679;
  if ((shmid = shmget(key, 27, 0700)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  for (s = shm; *s; s++)
    putchar(*s);
  putchar('\n');
  *shm = '*';
  if (shmdt(shm) < 0) {
    perror("shmdt");
    exit(1);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x162f,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1b,%esi
mov    $0x1c0,%edx
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11aa <main+0x4a>
lea    0xe65(%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    11e2 <main+0x82>
lea    0xe34(%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
cmpb   $0x0,(%rax)
je     1214 <main+0xb4>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11ea <main+0x8a>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
movb   $0x2a,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0x0,%rax
jge    124e <main+0xee>
lea    0xdce(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x162f,%edi
mov    $0x1b,%esi
mov    $0x1c0,%edx
call   1070 <shmget@plt>
test   %eax,%eax
js     11df <main+0x7f>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11e8 <main+0x88>
mov    %rax,%r14
mov    (%rax),%al
test   %al,%al
je     11b3 <main+0x53>
mov    %r14,%rbx
add    $0x1,%rbx
nopl   (%rax)
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11a0 <main+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x2a,(%r14)
mov    %r14,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     11f1 <main+0x91>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1a(%rip),%rdi
jmp    11f8 <main+0x98>
lea    0xe18(%rip),%rdi
jmp    11f8 <main+0x98>
lea    0xe15(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%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"
@EXIT_SUCCESS = global i32 0
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 5679, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @shmget(i32 %6, i32 27, i32 448)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i8* @shmat(i32 %13, i32* null, i32 0)
store i8* %14, i8** %4
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = load i8*, i8** %4
store i8* %20, i8** %5
br label %21
21: ; preds = %29, %19
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = load i8, i8* %26
%28 = call i32 @putchar(i8 signext %27)
br label %29
29: ; preds = %25
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
br label %21
32: ; preds = %21
%33 = call i32 @putchar(i8 signext 10)
%34 = load i8*, i8** %4
store i8 42, i8* %34
%35 = load i8*, i8** %4
%36 = call i64 @shmdt(i8* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}

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"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32 @shmget(i32 5679, i32 27, i32 448)
%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 i64 @shmdt(i8* %7)
%25 = icmp slt i64 %24, 0
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 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}