#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
unsigned char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  printf("SHM Client\n");
  int shmid = shmget(12345, 65536, 0666);
  printf("shmget() returned %d.\n", shmid);
  if (shmid != -1) {
    unsigned char *m = shmat(shmid, 0, 0);
    printf("shmat() returned %p.\n", m);
    unsigned char c = 0;
    while (c != 0xFF) {
      if (*m != c) {
        printf("New value (%x != %x).\n", *m, c);
        c = *m;
      }
      sleep(1);
    }
  }
  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)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3039,%edi
mov    $0x10000,%esi
mov    $0x1b6,%edx
call   1050 <shmget@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     1214 <main+0xd4>
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1040 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movb   $0x0,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
cmp    $0xff,%eax
je     120f <main+0xcf>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
movzbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
je     1200 <main+0xc0>
mov    -0x20(%rbp),%rax
movzbl (%rax),%esi
movzbl -0x21(%rbp),%edx
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11bc <main+0x7c>
jmp    1214 <main+0xd4>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xee9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3039,%edi
mov    $0x10000,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
mov    %eax,%ebx
lea    0xe83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%ebx
je     11ea <main+0x9a>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%rbx
lea    0xe77(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe7c(%rip),%r14
xor    %edx,%edx
xor    %ebp,%ebp
jmp    11d3 <main+0x83>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1070 <sleep@plt>
movzbl %bpl,%edx
cmp    $0xff,%dl
je     11ea <main+0x9a>
movzbl (%rbx),%esi
cmp    %bpl,%sil
je     11c0 <main+0x70>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl (%rbx),%ebp
jmp    11c0 <main+0x70>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"SHM Client\0A\00"
@.str.1 = [23 x i8] c"shmget() returned %d.\0A\00"
@.str.2 = [22 x i8] c"shmat() returned %p.\0A\00"
@.str.3 = [23 x i8] c"New value (%x != %x).\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @shmget(i32 12345, i32 65536, i32 438)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %43
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i8* @shmat(i32 %16, i32 0, i32 0)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i8 0, i8* %8
br label %20
20: ; preds = %40, %15
%21 = load i8, i8* %8
%22 = zext i8 %21 to i32
%23 = icmp ne i32 %22, 255
br i1 %23, label %24, label %42
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = load i8, i8* %8
%29 = zext i8 %28 to i32
%30 = icmp ne i32 %27, %29
br i1 %30, label %31, label %40
31: ; preds = %24
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = load i8, i8* %8
%36 = zext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %36)
%38 = load i8*, i8** %7
%39 = load i8, i8* %38
store i8 %39, i8* %8
br label %40
40: ; preds = %31, %24
%41 = call i32 @sleep(i32 1)
br label %20
42: ; preds = %20
br label %43
43: ; preds = %42, %2
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"shmget() returned %d.\0A\00"
@.str.2 = [22 x i8] c"shmat() returned %p.\0A\00"
@.str.3 = [23 x i8] c"New value (%x != %x).\0A\00"
@str = [11 x i8] c"SHM Client\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @shmget(i32 12345, i32 65536, i32 438)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %4, -1
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = tail call i8* @shmat(i32 %4, i32 0, i32 0)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %7, %19
%11 = phi i32 [ 0, %7 ], [ %22, %19 ]
%12 = phi i8 [ 0, %7 ], [ %20, %19 ]
%13 = load i8, i8* %8
%14 = icmp eq i8 %13, %12
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = zext i8 %13 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %11)
%18 = load i8, i8* %8
br label %19
19: ; preds = %15, %10
%20 = phi i8 [ %18, %15 ], [ %12, %10 ]
%21 = tail call i32 @sleep(i32 1)
%22 = zext i8 %20 to i32
%23 = icmp eq i8 %20, -1
br i1 %23, label %24, label %10
24: ; preds = %19, %2
ret i32 0
}