#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*/ printf(char *, ...);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
main() {
  int shmid;
  key_t key;
  char *shm;
  char msg[27];
  key = 5678;
  if ((shmid = shmget(key, 27, 0666)) < 0) {
    perror("shmget failed");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat failed");
    exit(1);
  }
  int i;
  for (i = 0; shm[i] != 0; i++) {
    msg[i] = shm[i];
  }
  msg[i] = 0;
  printf("Message read: %s\n", msg);
  printf("Client done reading memory\n");
  *shm = '*';
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    119a <main+0x4a>
lea    0xe75(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@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    11d2 <main+0x82>
lea    0xe4b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     120f <main+0xbf>
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
movslq -0x44(%rbp),%rax
mov    %cl,-0x40(%rbp,%rax,1)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11d9 <main+0x89>
movslq -0x44(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
movb   $0x2a,(%rax)
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    $0x162e,%edi
mov    $0x1b,%esi
mov    $0x1b6,%edx
call   1070 <shmget@plt>
test   %eax,%eax
jns    1186 <main+0x26>
lea    0xe7c(%rip),%rdi
jmp    119e <main+0x3e>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11ad <main+0x4d>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
mov    (%rax),%al
test   %al,%al
jne    11ba <main+0x5a>
xor    %eax,%eax
jmp    11d2 <main+0x72>
xor    %ecx,%ecx
nopl   0x0(%rax)
mov    %al,(%rsp,%rcx,1)
movzbl 0x1(%rbx,%rcx,1),%eax
add    $0x1,%rcx
test   %al,%al
jne    11c0 <main+0x60>
mov    %ecx,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe3e(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
movb   $0x2a,(%rbx)
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [14 x i8] c"shmget failed\00"
@.str.1 = [13 x i8] c"shmat failed\00"
@.str.2 = [18 x i8] c"Message read: %s\0A\00"
@str = [27 x i8] c"Client done reading memory\00"
define i32 @main() {
%1 = alloca [27 x i8]
%2 = getelementptr inbounds [27 x i8], [27 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @shmget(i32 5678, i32 27, i32 438)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i8* @shmat(i32 %3, i32* null, i32 0)
%10 = icmp eq i8* %9, inttoptr (i64 -1 to i8*)
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i8, i8* %9
%13 = icmp eq i8 %12, 0
br i1 %13, label %27, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11, %17
%18 = phi i64 [ %21, %17 ], [ 0, %11 ]
%19 = phi i8 [ %23, %17 ], [ %12, %11 ]
%20 = getelementptr inbounds [27 x i8], [27 x i8]* %1, i64 0, i64 %18
store i8 %19, i8* %20
%21 = add nuw nsw i64 %18, 1
%22 = getelementptr inbounds i8, i8* %9, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %17
25: ; preds = %17
%26 = and i64 %21, 4294967295
br label %27
27: ; preds = %25, %11
%28 = phi i64 [ 0, %11 ], [ %26, %25 ]
%29 = getelementptr inbounds [27 x i8], [27 x i8]* %1, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
store i8 42, i8* %9
%32 = call i32 @exit(i32 0)
unreachable
}