#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 *, ...);
int /*<<< orphan*/ scanf(char *, int *);
void *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int shmid;
  void *shared;
  printf("enter shmid: ");
  scanf("%d", &shmid);
  shared = shmat(shmid, 0, 0);
  printf("shared=%d\n", (unsigned)shared);
  printf("value=%d\n", *(unsigned *)shared);
  while (!*(unsigned *)shared)
    ;
  printf("shared value=%d\n", *(unsigned *)shared);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeba(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ae <main+0x7e>
jmp    11b3 <main+0x83>
jmp    1195 <main+0x65>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xec4(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <scanf@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%rbx
lea    0xea6(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx),%esi
lea    0xea6(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx),%esi
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"enter shmid: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [11 x i8] c"shared=%d\0A\00"
@.str.3 = [10 x i8] c"value=%d\0A\00"
@.str.4 = [17 x i8] c"shared value=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%6 = load i32, i32* %2
%7 = call i8* @shmat(i32 %6, i32 0, i32 0)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i32
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %3
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %21, %0
%16 = load i8*, i8** %3
%17 = bitcast i8* %16 to i32*
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %22
21: ; preds = %15
br label %15
22: ; preds = %15
%23 = load i8*, i8** %3
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [14 x i8] c"enter shmid: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [11 x i8] c"shared=%d\0A\00"
@.str.3 = [10 x i8] c"value=%d\0A\00"
@.str.4 = [17 x i8] c"shared value=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = call i8* @shmat(i32 %5, i32 0, i32 0)
%7 = ptrtoint i8* %6 to i64
%8 = trunc i64 %7 to i32
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = bitcast i8* %6 to i32*
%11 = load i32, i32* %10
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %10
%14 = icmp ne i32 %13, 0
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %13)
ret i32 0
}