#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 IPC_CREAT;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(void *);
int shmget(int, int, int);
int main() {
  char contents[] = "hello sundy";
  void *share_memory = (void *)0;
  int shmid = shmget(34567, 2046, IPC_CREAT | 0666);
  if (shmid != -1) {
    share_memory = shmat(shmid, NULL, 0);
    if (share_memory != (void *)-1) {
      printf("get share memeory value:%s\n", (char *)share_memory);
      shmdt(share_memory);
    } else {
      perror("shmat:");
    }
  } else {
    perror("shmget:");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe98(%rip),%eax
mov    %eax,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x8707,%edi
mov    $0x7fe,%esi
call   1070 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11f8 <main+0xa8>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
je     11e7 <main+0x97>
mov    -0x18(%rbp),%rsi
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
jmp    11f3 <main+0xa3>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
jmp    1204 <main+0xb4>
lea    0xe30(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x8707,%edi
mov    $0x7fe,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     11a2 <main+0x52>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11ab <main+0x5b>
mov    %rax,%rbx
lea    0xe72(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe7a(%rip),%rdi
jmp    11b2 <main+0x62>
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
jmp    119e <main+0x4e>

LLVM IR (-O0):
@__const.main.contents = [12 x i8] c"hello sundy\00"
@IPC_CREAT = global i32 0
@.str = [28 x i8] c"get share memeory value:%s\0A\00"
@.str.1 = [7 x i8] c"shmat:\00"
@.str.2 = [8 x i8] c"shmget:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8]
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast [12 x i8]* %2 to i8*
store i8* null, i8** %3
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 %6, 438
%8 = call i32 @shmget(i32 34567, i32 2046, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %24
11: ; preds = %0
%12 = load i32, i32* %4
%13 = call i8* @shmat(i32 %12, i32* null, i32 0)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %3
%20 = call i32 @shmdt(i8* %19)
br label %23
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
br label %26
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %23
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [28 x i8] c"get share memeory value:%s\0A\00"
@.str.1 = [7 x i8] c"shmat:\00"
@.str.2 = [8 x i8] c"shmget:\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 438
%3 = tail call i32 @shmget(i32 34567, i32 2046, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %13, label %5
5: ; preds = %0
%6 = tail call i8* @shmat(i32 %3, i32* null, i32 0)
%7 = icmp eq i8* %6, inttoptr (i64 -1 to i8*)
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %6)
%10 = tail call i32 @shmdt(i8* %6)
br label %15
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %8, %11, %13
ret i32 0
}