#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 with id %d attached at address %p\n", segment_id,
         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);
  printf("Sleep for 10s\n");
  sleep(10);
  shmdt(shared_memory);
  shmctl(segment_id, IPC_RMID, 0);
  printf("Completely remove shared memory\n");
  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    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd7(%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    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe25(%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    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@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    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <shmctl@plt>
mov    (%rsp),%esi
lea    0xe47(%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    0xe25(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
lea    0xe12(%rip),%rdi
call   1030 <puts@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 = [49 x i8] c"shared memory with id %d 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"
@.str.5 = [15 x i8] c"Sleep for 10s\0A\00"
@IPC_RMID = global i32 0
@.str.6 = [33 x i8] c"Completely remove shared memory\0A\00"
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 i32, i32* %2
%20 = load i8*, i8** %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %19, i8* %20)
%22 = load i32, i32* %2
%23 = load i32, i32* @IPC_STAT
%24 = call i32 @shmctl(i32 %22, i32 %23, %struct.shmid_ds* %4)
%25 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** %3
%30 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%31 = load i8*, i8** %3
%32 = call i32 @shmdt(i8* %31)
%33 = load i32, i32* %2
%34 = call i64 @shmat(i32 %33, i8* inttoptr (i64 83886080 to i8*), i32 0)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %3
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %3
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @sleep(i32 10)
%42 = load i8*, i8** %3
%43 = call i32 @shmdt(i8* %42)
%44 = load i32, i32* %2
%45 = load i32, i32* @IPC_RMID
%46 = call i32 @shmctl(i32 %44, i32 %45, %struct.shmid_ds* null)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
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 = [49 x i8] c"shared memory with id %d 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
@str = [14 x i8] c"Sleep for 10s\00"
@str.7 = [32 x i8] c"Completely remove shared memory\00"
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 ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %11, 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%26 = call i32 @sleep(i32 10)
%27 = call i32 @shmdt(i8* %22)
%28 = load i32, i32* @IPC_RMID
%29 = call i32 @shmctl(i32 %11, i32 %28, %struct.shmid_ds* null)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}