#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int /*<<< orphan*/ shmdt(unsigned char *);
int shmget(int /*<<< orphan*/, int const, int);
int /*<<< orphan*/ sprintf(char *, char *);
int main(void) {
  int segment_id;
  unsigned char *shared_memory;
  struct shmid_ds shmbuff;
  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 = (unsigned char *)shmat(segment_id, 0, 0);
  printf("Shared memory attached at address: 0x%08X\n",
         (unsigned int)shared_memory);
  shmctl(segment_id, IPC_STAT, &shmbuff);
  segment_size = shmbuff.shm_segsz;
  printf("Segment size: %d\n", segment_size);
  sprintf((char *)shared_memory,
          "This is a string being written to the shared memory.");
  shmdt(shared_memory);
  shared_memory = (unsigned char *)shmat(segment_id, (void *)0x5000000, 0);
  printf("Shared memory reattached at address: 0x%08X\n",
         (unsigned int)shared_memory);
  printf("String in shared memory: \"%s\n\"", (char *)shared_memory);
  shmdt(shared_memory);
  shmctl(segment_id, IPC_RMID, 0);
  exit(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),%rax
mov    %eax,%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd8(%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    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe20(%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),%rax
mov    %eax,%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e64(%rip),%rax
mov    0x2e35(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%edi
mov    0x2e4a(%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    0xe4f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <shmctl@plt>
mov    (%rsp),%esi
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0x2e79726f6d656d,%rax
mov    %rax,0x2d(%rbx)
movups 0xe4a(%rip),%xmm0
movups %xmm0,(%rbx)
movups 0xe50(%rip),%xmm0
movups %xmm0,0x10(%rbx)
movups 0xe55(%rip),%xmm0
movups %xmm0,0x20(%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    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe63(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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 = [43 x i8] c"Shared memory attached at address: 0x%08X\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [18 x i8] c"Segment size: %d\0A\00"
@.str.2 = [53 x i8] c"This is a string being written to the shared memory.\00"
@.str.3 = [45 x i8] c"Shared memory reattached at address: 0x%08X\0A\00"
@.str.4 = [31 x i8] c"String in shared memory: \22%s\0A\22\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 = ptrtoint i8* %19 to i32
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %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 ([53 x i8], [53 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 = ptrtoint i8* %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = load i8*, i8** %3
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %3
%42 = call i32 @shmdt(i8* %41)
%43 = load i32, i32* %2
%44 = load i32, i32* @IPC_RMID
%45 = call i32 @shmctl(i32 %43, i32 %44, %struct.shmid_ds* null)
%46 = call i32 @exit(i32 0)
unreachable
}

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 = [43 x i8] c"Shared memory attached at address: 0x%08X\0A\00"
@IPC_STAT = global i32 0
@.str.1 = [18 x i8] c"Segment size: %d\0A\00"
@.str.2 = [53 x i8] c"This is a string being written to the shared memory.\00"
@.str.3 = [45 x i8] c"Shared memory reattached at address: 0x%08X\0A\00"
@.str.4 = [31 x i8] c"String in shared memory: \22%s\0A\22\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 = trunc i64 %12 to i32
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @IPC_STAT
%17 = call i32 @shmctl(i32 %11, i32 %16, %struct.shmid_ds* %1)
%18 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call i32 @shmdt(i8* %13)
%22 = call i64 @shmat(i32 %11, i8* inttoptr (i64 83886080 to i8*), i32 0)
%23 = inttoptr i64 %22 to i8*
%24 = trunc i64 %22 to i32
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%27 = call i32 @shmdt(i8* %23)
%28 = load i32, i32* @IPC_RMID
%29 = call i32 @shmctl(i32 %11, i32 %28, %struct.shmid_ds* null)
%30 = call i32 @exit(i32 0)
unreachable
}