#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);
int main(int argc, const char *argv[]) {
  int shmid;
  key_t key;
  char *shm;
  char *s;
  key = 9876;
  shmid = shmget(key, 100, 0666);
  if (shmid < 0) {
    perror("shmget");
    exit(1);
  }
  shm = shmat(shmid, NULL, 0);
  if (shm == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  for (s = shm; *s != 0; s++) {
    printf("%c", *s);
  }
  printf("\n");
  *shm = '*';
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2694,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x64,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a2 <main+0x52>
lea    0xe6d(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11db <main+0x8b>
lea    0xe3b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1219 <main+0xc9>
mov    -0x28(%rbp),%rax
movsbl (%rax),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11e3 <main+0x93>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x2a,(%rax)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x2694,%edi
mov    $0x64,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
test   %eax,%eax
js     11bb <main+0x6b>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11c4 <main+0x74>
mov    %rax,%r14
mov    (%rax),%al
test   %al,%al
je     11a3 <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    1190 <main+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x2a,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3e(%rip),%rdi
jmp    11cb <main+0x7b>
lea    0xe3c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @shmget(i32 9876, i32 100, i32 438)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%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 %25, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11, %17
%18 = phi i8 [ %23, %17 ], [ %12, %11 ]
%19 = phi i8* [ %22, %17 ], [ %9, %11 ]
%20 = sext i8 %18 to i32
%21 = tail call i32 @putchar(i32 %20)
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %17
25: ; preds = %17, %11
%26 = tail call i32 @putchar(i32 10)
store i8 42, i8* %9
ret i32 0
}