#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 /*<<< orphan*/ key_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_CREAT;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ ftok(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int main(void) {
  int shmid;
  char *pw, *pr;
  char str[500];
  key_t key;
  key = ftok(".", 1);
  shmid = shmget(key, 1024, IPC_CREAT);
  if (shmid < 0) {
    perror("shmget...");
    return -1;
  }
  pr = (char *)shmat(shmid, NULL, 0);
  pw = pr + 512;
  printf("pr: %p   pw: %p\n", pr, pw);
  while (1) {
    sleep(10);
    printf("%s\n", pr);
    printf("Input message:");
    fflush(stdout);
    scanf("%s", str);
    if (!strncmp(str, "88", 2))
      break;
    strcpy(pw, str);
  }
  shmdt(pr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3f(%rip),%rdi
mov    $0x1,%esi
call   10c0 <ftok@plt>
mov    %eax,-0x214(%rbp)
mov    -0x214(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   10b0 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1217 <main+0x67>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x133>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    -0x18(%rbp),%rsi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    -0x210(%rbp),%rsi
lea    0xd9b(%rip),%rdi
call   1080 <scanf@plt>
lea    -0x210(%rbp),%rdi
lea    0xd8b(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12be <main+0x10e>
jmp    12d3 <main+0x123>
mov    -0x10(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    124d <main+0x9d>
mov    -0x18(%rbp),%rdi
call   1050 <shmdt@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f8,%rsp
lea    0xe30(%rip),%rdi
mov    $0x1,%esi
call   10d0 <ftok@plt>
mov    0x2def(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
mov    $0x400,%esi
call   10c0 <shmget@plt>
test   %eax,%eax
js     12b0 <main+0xf0>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <shmat@plt>
mov    %rax,%r13
lea    0x200(%rax),%r14
lea    0xdef(%rip),%rdi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2db1(%rip),%r15
lea    0xdf4(%rip),%r12
mov    %rsp,%rbx
lea    0xded(%rip),%rbp
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %r13,%rdi
call   1050 <puts@plt>
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%r15),%edi
call   1080 <fflush@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1294 <main+0xd4>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
jmp    1240 <main+0x80>
mov    %r13,%rdi
call   1060 <shmdt@plt>
xor    %eax,%eax
add    $0x1f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd49(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    129e <main+0xde>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@.str.1 = [10 x i8] c"shmget...\00"
@.str.2 = [17 x i8] c"pr: %p pw: %p\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [15 x i8] c"Input message:\00"
@stdout = global i32 0
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [3 x i8] c"88\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [500 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = load i32, i32* @IPC_CREAT
%10 = call i32 @shmget(i32 %8, i32 1024, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i64 @shmat(i32 %16, i32* null, i32 0)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 512
store i8* %20, i8** %3
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %22)
br label %24
24: ; preds = %15, %37
%25 = call i32 @sleep(i32 10)
%26 = load i8*, i8** %4
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%32 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%34 = call i32 @strncmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %24
br label %41
37: ; preds = %24
%38 = load i8*, i8** %3
%39 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%40 = call i32 @strcpy(i8* %38, i8* %39)
br label %24
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = call i32 @shmdt(i8* %42)
store i32 0, i32* %1
br label %44
44: ; preds = %41, %13
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@.str.1 = [10 x i8] c"shmget...\00"
@.str.2 = [17 x i8] c"pr: %p pw: %p\0A\00"
@.str.4 = [15 x i8] c"Input message:\00"
@stdout = global i32 0
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [3 x i8] c"88\00"
define i32 @main() {
%1 = alloca [500 x i8]
%2 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%4 = load i32, i32* @IPC_CREAT
%5 = tail call i32 @shmget(i32 %3, i32 1024, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
9: ; preds = %0
%10 = tail call i64 @shmat(i32 %5, i32* null, i32 0)
%11 = inttoptr i64 %10 to i8*
%12 = getelementptr inbounds i8, i8* %11, i64 512
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %12)
br label %14
14: ; preds = %23, %9
%15 = call i32 @sleep(i32 10)
%16 = call i32 @puts(i8* dereferenceable(1) %11)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%21 = call i32 @strncmp(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %14
%24 = call i32 @strcpy(i8* %12, i8* %2)
br label %14
25: ; preds = %14
%26 = call i32 @shmdt(i8* %11)
br label %27
27: ; preds = %25, %7
%28 = phi i32 [ -1, %7 ], [ 0, %25 ]
ret i32 %28
}