#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*/ memcpy(void *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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) {
      memcpy(share_memory, contents, 12);
      printf("save successed\n");
      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    0xe8a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe88(%rip),%eax
mov    %eax,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x8707,%edi
mov    $0x7fe,%esi
call   1080 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1216 <main+0xb6>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
je     1205 <main+0xa5>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1050 <memcpy@plt>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
jmp    1211 <main+0xb1>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
jmp    1222 <main+0xc2>
lea    0xe06(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0x7573206f6c6c6568,%rax
20 73 75
mov    %rax,(%rsp)
movl   $0x79646e,0x8(%rsp)
mov    0x2e5e(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x8707,%edi
mov    $0x7fe,%esi
call   1080 <shmget@plt>
cmp    $0xffffffff,%eax
je     11dd <main+0x7d>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11e6 <main+0x86>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0xc,%edx
call   1050 <memcpy@plt>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe2f(%rip),%rdi
jmp    11ed <main+0x8d>
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
jmp    11d5 <main+0x75>

LLVM IR (-O0):
@__const.main.contents = [12 x i8] c"hello sundy\00"
@IPC_CREAT = global i32 0
@.str = [16 x i8] c"save successed\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 %26
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 %23
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%19 = call i32 @memcpy(i8* %17, i8* %18, i32 12)
%20 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%21 = load i8*, i8** %3
%22 = call i32 @shmdt(i8* %21)
br label %25
23: ; preds = %11
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
br label %28
26: ; preds = %0
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %25
ret i32 0
}

LLVM IR (-O2):
@__const.main.contents = [12 x i8] c"hello sundy\00"
@IPC_CREAT = global i32 0
@.str.1 = [7 x i8] c"shmat:\00"
@.str.2 = [8 x i8] c"shmget:\00"
@str = [15 x i8] c"save successed\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 438
%5 = tail call i32 @shmget(i32 34567, i32 2046, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %16, label %7
7: ; preds = %0
%8 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = call i32 @memcpy(i8* %8, i8* %2, i32 12)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%13 = call i32 @shmdt(i8* %8)
br label %18
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %10, %14, %16
ret i32 0
}