#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 shm_segsz;
};

/* 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 *, ...);
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*/ sleep(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 address %p\n", shared_memory);
  shmctl(segment_id, IPC_STAT, &shmbuffer);
  segment_size = shmbuffer.shm_segsz;
  printf("segment size: %d\n", segment_size);
  sprintf(shared_memory, "Hello, world.");
  shmdt(shared_memory);
  shared_memory = (char *)shmat(segment_id, (void *)0x5000000, 0);
  printf("shared memory reattached at address %p\n", shared_memory);
  printf("%s\n", shared_memory);
  shmdt(shared_memory);
  sleep(15);
  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    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
mov    0x2e10(%rip),%rax
or     (%rax),%edx
mov    0x2e27(%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    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <shmctl@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe1d(%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    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    $0xf,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2d43(%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    0x2e36(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e54(%rip),%rax
mov    0x2e25(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
mov    $0x6400,%esi
call   1080 <shmget@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
lea    0xe3f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <shmctl@plt>
mov    (%rsp),%esi
lea    0xe3e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,(%rbx)
movabs $0x2e646c726f7720,%rax
mov    %rax,0x6(%rbx)
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    $0x5000000,%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
lea    0xe1c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    $0xf,%edi
call   1090 <sleep@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <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 = [38 x i8] c"shared memory attached at address %p\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [18 x i8] c"segment size: %d\0A\00"
@.str.2 = [14 x i8] c"Hello, world.\00"
@.str.3 = [40 x i8] c"shared memory reattached at address %p\0A\00"
@.str.4 = [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 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 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 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %3
%29 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %3
%31 = call i32 @shmdt(i8* %30)
%32 = load i32, i32* %2
%33 = call i64 @shmat(i32 %32, i8* inttoptr (i64 83886080 to i8*), i32 0)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %3
%35 = load i8*, i8** %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %3
%40 = call i32 @shmdt(i8* %39)
%41 = call i32 @sleep(i32 15)
%42 = load i32, i32* %2
%43 = load i32, i32* @IPC_RMID
%44 = call i32 @shmctl(i32 %42, i32 %43, %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 = [38 x i8] c"shared memory attached at address %p\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [18 x i8] c"segment size: %d\0A\00"
@.str.2 = [14 x i8] c"Hello, world.\00"
@.str.3 = [40 x i8] c"shared memory reattached at address %p\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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 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 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @shmdt(i8* %13)
%21 = call i64 @shmat(i32 %11, i8* inttoptr (i64 83886080 to i8*), i32 0)
%22 = inttoptr i64 %21 to i8*
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32 @puts(i8* dereferenceable(1) %22)
%25 = call i32 @shmdt(i8* %22)
%26 = call i32 @sleep(i32 15)
%27 = load i32, i32* @IPC_RMID
%28 = call i32 @shmctl(i32 %11, i32 %27, %struct.shmid_ds* null)
ret i32 0
}