#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 IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
int S_IRUSR;
int S_IWUSR;
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 shmget(int /*<<< orphan*/, int const, int);
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;
  segment_id = shmget(IPC_PRIVATE, shared_segment_size,
                      IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
  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 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    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    0x2e61(%rip),%rax
or     (%rax),%edx
mov    0x2e20(%rip),%rax
or     (%rax),%edx
mov    0x2e37(%rip),%rax
or     (%rax),%edx
mov    $0x6400,%esi
call   1070 <shmget@plt>
mov    %eax,-0x8(%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    0xe33(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <shmctl@plt>
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <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    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d7a(%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   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e74(%rip),%rax
mov    0x2e45(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
or     (%rax),%edx
mov    $0x6400,%esi
call   1070 <shmget@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
lea    0xe5f(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <shmctl@plt>
movups 0xe5c(%rip),%xmm0
movups %xmm0,(%rbx)
movl   $0x79726f,0x10(%rbx)
mov    %rbx,%rdi
call   1030 <shmdt@plt>
mov    $0x5000000,%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
lea    0xe46(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [31 x i8] c"shared memory attached at: %p\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [20 x i8] c"Hello 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* @IPC_PRIVATE
%8 = load i32, i32* @IPC_CREAT
%9 = load i32, i32* @IPC_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @shmget(i32 %7, i32 25600, i32 %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = call i64 @shmat(i32 %16, i8* null, i32 0)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %2
%22 = load i32, i32* @IPC_STAT
%23 = call i32 @shmctl(i32 %21, i32 %22, %struct.shmid_ds* %4)
%24 = load i8*, i8** %3
%25 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%26 = load i8*, i8** %3
%27 = call i32 @shmdt(i8* %26)
%28 = load i32, i32* %2
%29 = call i64 @shmat(i32 %28, i8* inttoptr (i64 83886080 to i8*), i32 0)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %3
%31 = load i8*, i8** %3
%32 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %3
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = load i8*, i8** %3
%36 = call i32 @shmdt(i8* %35)
%37 = load i32, i32* %2
%38 = load i32, i32* @IPC_RMID
%39 = call i32 @shmctl(i32 %37, i32 %38, %struct.shmid_ds* null)
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [31 x i8] c"shared memory attached at: %p\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [20 x i8] c"Hello 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 = load i32, i32* @IPC_PRIVATE
%4 = load i32, i32* @IPC_CREAT
%5 = load i32, i32* @IPC_EXCL
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = tail call i32 @shmget(i32 %3, i32 25600, i32 %10)
%12 = tail call i64 @shmat(i32 %11, i8* null, i32 0)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @IPC_STAT
%16 = call i32 @shmctl(i32 %11, i32 %15, %struct.shmid_ds* %1)
%17 = call i32 @shmdt(i8* %13)
%18 = call i64 @shmat(i32 %11, i8* inttoptr (i64 83886080 to i8*), i32 0)
%19 = inttoptr i64 %18 to i8*
%20 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%22 = call i32 @shmdt(i8* %19)
%23 = load i32, i32* @IPC_RMID
%24 = call i32 @shmctl(i32 %11, i32 %23, %struct.shmid_ds* null)
ret i32 0
}