#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 */
struct shmid_ds {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ shmat(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int /*<<< orphan*/ shmdt(char *);
int /*<<< orphan*/ sprintf(char *, char *);
int main() {
  int segment_id;
  char *shared_memory;
  struct shmid_ds shmbuffer;
  int segment_size;
  const int shared_segment_size = 0x6400;
  shared_memory = (char *)shmat(segment_id, 0, 0);
  printf("shared memory attached at: %p\n", shared_memory);
  shmctl(segment_id, IPC_STAT, &shmbuffer);
  sprintf(shared_memory, "Hello World of Shared Memory");
  shmdt(shared_memory);
  shared_memory = (char *)shmat(segment_id, (void *)0x5000000, 0);
  printf("shared memory reattached at: %p\n", shared_memory);
  printf("%s\n", shared_memory);
  shmdt(shared_memory);
  shmctl(segment_id, IPC_RMID, 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x6400,-0x20(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <shmctl@plt>
mov    -0x10(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    $0x5000000,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
lea    0xea8(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1050 <shmctl@plt>
movups 0xea5(%rip),%xmm0
movups %xmm0,(%rbx)
movups 0xea8(%rip),%xmm0
movups %xmm0,0xd(%rbx)
mov    %rbx,%rdi
call   1030 <shmdt@plt>
mov    $0x5000000,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
lea    0xe96(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xea8(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <shmctl@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@.str = [31 x i8] c"shared memory attached at: %p\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [29 x i8] c"Hello World of Shared Memory\00"
@.str.2 = [33 x i8] c"shared memory reattached at: %p\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.shmid_ds
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 25600, i32* %6
%7 = load i32, i32* %2
%8 = call i64 @shmat(i32 %7, i8* null, i32 0)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* %2
%13 = load i32, i32* @IPC_STAT
%14 = call i32 @shmctl(i32 %12, i32 %13, %struct.shmid_ds* %4)
%15 = load i8*, i8** %3
%16 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %3
%18 = call i32 @shmdt(i8* %17)
%19 = load i32, i32* %2
%20 = call i64 @shmat(i32 %19, i8* inttoptr (i64 83886080 to i8*), i32 0)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %3
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %3
%27 = call i32 @shmdt(i8* %26)
%28 = load i32, i32* %2
%29 = load i32, i32* @IPC_RMID
%30 = call i32 @shmctl(i32 %28, i32 %29, %struct.shmid_ds* null)
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@.str = [31 x i8] c"shared memory attached at: %p\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [29 x i8] c"Hello World of Shared Memory\00"
@.str.2 = [33 x i8] c"shared memory reattached at: %p\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca %struct.shmid_ds
%2 = bitcast %struct.shmid_ds* %1 to i8*
%3 = tail call i64 @shmat(i32 undef, i8* null, i32 0)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i32, i32* @IPC_STAT
%7 = call i32 @shmctl(i32 undef, i32 %6, %struct.shmid_ds* %1)
%8 = call i32 @shmdt(i8* %4)
%9 = call i64 @shmat(i32 undef, i8* inttoptr (i64 83886080 to i8*), i32 0)
%10 = inttoptr i64 %9 to i8*
%11 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%13 = call i32 @shmdt(i8* %10)
%14 = load i32, i32* @IPC_RMID
%15 = call i32 @shmctl(i32 undef, i32 %14, %struct.shmid_ds* null)
ret i32 0
}