#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sharedMemoryAttach(int);
int sharedMemoryCreate(int, int);
int sharedMemoryDelete(int);
int sharedMemoryDetatch(char *);
int sharedMemoryGet(int, int);
int sharedMemoryLock(int);
int sharedMemoryUnlock(int);
unsigned int strlen(char *);
int main() {
  int ret, shmid;
  char *shmaddr = 0;
  char toWrite[3] = {'h', 'i', '\0'};
  unsigned int i;
  printf("Creating shared memory with key '%d' of size %d bytes.\n", 6, 10);
  shmid = sharedMemoryCreate(6, 10);
  printf("Got shmid: %d\n\n", shmid);
  printf("Getting shared memory with key '%d' of size %d bytes.\n", 6, 10);
  shmid = sharedMemoryGet(6, 10);
  printf("Got shmid: %d\n\n", shmid);
  printf("Attaching shared memory to address: %p\n", shmaddr);
  shmaddr = (char *)sharedMemoryAttach(shmid);
  printf("Which now points to: %p\n\n", shmaddr);
  printf("Locking shared memory...\n");
  ret = sharedMemoryLock(shmid);
  printf("returned %d\n\n", ret);
  printf("Writing '%s' to shared memory...\n", toWrite);
  for (i = 0; i < strlen(toWrite); i++)
    *(shmaddr + i) = toWrite[i];
  printf("Reading from shared memory: '%s'\n\n", shmaddr);
  printf("Unlocking shared memory...\n");
  ret = sharedMemoryUnlock(shmid);
  printf("returned %d\n\n", ret);
  printf("Detatching shared memory from address: %p\n", shmaddr);
  ret = sharedMemoryDetatch(shmaddr);
  printf("returned %d\n", ret);
  printf("Which now points to: %p\n\n", shmaddr);
  printf("Deleting shared memory... \n");
  ret = sharedMemoryDelete(shmid);
  printf("returned: %d\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0xe52(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe4a(%rip),%al
mov    %al,-0x19(%rbp)
lea    0xe41(%rip),%rdi
mov    $0x6,%esi
mov    $0xa,%edx
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x6,%edi
mov    $0xa,%esi
call   10a0 <sharedMemoryCreate@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xe4e(%rip),%rdi
mov    $0x6,%esi
mov    $0xa,%edx
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x6,%edi
mov    $0xa,%esi
call   10b0 <sharedMemoryGet@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <sharedMemoryAttach@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <sharedMemoryLock@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x1b(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x1b(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jae    12df <main+0x14f>
mov    -0x20(%rbp),%eax
mov    -0x1b(%rbp,%rax,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12a4 <main+0x114>
mov    -0x18(%rbp),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <sharedMemoryUnlock@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <sharedMemoryDetatch@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1030 <sharedMemoryDelete@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movb   $0x0,0x6(%rsp)
movw   $0x6968,0x4(%rsp)
lea    0xe49(%rip),%rdi
mov    $0x6,%esi
mov    $0xa,%edx
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x6,%edi
mov    $0xa,%esi
call   10b0 <sharedMemoryCreate@plt>
lea    0xe5a(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe57(%rip),%rdi
mov    $0x6,%esi
mov    $0xa,%edx
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x6,%edi
mov    $0xa,%esi
call   10c0 <sharedMemoryGet@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe58(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r14d,%edi
call   1040 <sharedMemoryAttach@plt>
mov    %rax,%r15
lea    0xe65(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xf07(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
call   10a0 <sharedMemoryLock@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe58(%rip),%rdi
lea    0x4(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbp,%rdi
call   1080 <strlen@plt>
test   %eax,%eax
je     12bc <main+0x11c>
xor    %ebx,%ebx
lea    0x4(%rsp),%rbp
nopl   0x0(%rax)
movzbl 0x4(%rsp,%rbx,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %rbp,%rdi
call   1080 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     12a0 <main+0x100>
lea    0xe31(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xea2(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
call   1070 <sharedMemoryUnlock@plt>
lea    0xddc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe1f(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r15,%rdi
call   1060 <sharedMemoryDetatch@plt>
lea    0xe31(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xd89(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe5f(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
call   1030 <sharedMemoryDelete@plt>
lea    0xe09(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.toWrite = [3 x i8] c"hi\00"
@.str = [56 x i8] c"Creating shared memory with key '%d' of size %d bytes.\0A\00"
@.str.1 = [16 x i8] c"Got shmid: %d\0A\0A\00"
@.str.2 = [55 x i8] c"Getting shared memory with key '%d' of size %d bytes.\0A\00"
@.str.3 = [40 x i8] c"Attaching shared memory to address: %p\0A\00"
@.str.4 = [26 x i8] c"Which now points to: %p\0A\0A\00"
@.str.5 = [26 x i8] c"Locking shared memory...\0A\00"
@.str.6 = [14 x i8] c"returned %d\0A\0A\00"
@.str.7 = [34 x i8] c"Writing '%s' to shared memory...\0A\00"
@.str.8 = [35 x i8] c"Reading from shared memory: '%s'\0A\0A\00"
@.str.9 = [28 x i8] c"Unlocking shared memory...\0A\00"
@.str.10 = [43 x i8] c"Detatching shared memory from address: %p\0A\00"
@.str.11 = [13 x i8] c"returned %d\0A\00"
@.str.12 = [28 x i8] c"Deleting shared memory... \0A\00"
@.str.13 = [14 x i8] c"returned: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [3 x i8]
%6 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %4
%7 = bitcast [3 x i8]* %5 to i8*
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 6, i32 10)
%9 = call i32 @sharedMemoryCreate(i32 6, i32 10)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 10)
%13 = call i32 @sharedMemoryGet(i32 6, i32 10)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %3
%19 = call i64 @sharedMemoryAttach(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%24 = load i32, i32* %3
%25 = call i32 @sharedMemoryLock(i32 %24)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %28)
store i32 0, i32* %6
br label %30
30: ; preds = %44, %0
%31 = load i32, i32* %6
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = icmp ult i32 %31, %33
br i1 %34, label %35, label %47
35: ; preds = %30
%36 = load i32, i32* %6
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %4
%41 = load i32, i32* %6
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 %39, i8* %43
br label %44
44: ; preds = %35
%45 = load i32, i32* %6
%46 = add i32 %45, 1
store i32 %46, i32* %6
br label %30
47: ; preds = %30
%48 = load i8*, i8** %4
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i8* %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
%51 = load i32, i32* %3
%52 = call i32 @sharedMemoryUnlock(i32 %51)
store i32 %52, i32* %2
%53 = load i32, i32* %2
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %53)
%55 = load i8*, i8** %4
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %4
%58 = call i32 @sharedMemoryDetatch(i8* %57)
store i32 %58, i32* %2
%59 = load i32, i32* %2
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), i32 %59)
%61 = load i8*, i8** %4
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.12, i64 0, i64 0))
%64 = load i32, i32* %3
%65 = call i32 @sharedMemoryDelete(i32 %64)
store i32 %65, i32* %2
%66 = load i32, i32* %2
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0), i32 %66)
ret i32 0
}

LLVM IR (-O2):
@__const.main.toWrite = [3 x i8] c"hi\00"
@.str = [56 x i8] c"Creating shared memory with key '%d' of size %d bytes.\0A\00"
@.str.1 = [16 x i8] c"Got shmid: %d\0A\0A\00"
@.str.2 = [55 x i8] c"Getting shared memory with key '%d' of size %d bytes.\0A\00"
@.str.3 = [40 x i8] c"Attaching shared memory to address: %p\0A\00"
@.str.4 = [26 x i8] c"Which now points to: %p\0A\0A\00"
@.str.6 = [14 x i8] c"returned %d\0A\0A\00"
@.str.7 = [34 x i8] c"Writing '%s' to shared memory...\0A\00"
@.str.8 = [35 x i8] c"Reading from shared memory: '%s'\0A\0A\00"
@.str.10 = [43 x i8] c"Detatching shared memory from address: %p\0A\00"
@.str.11 = [13 x i8] c"returned %d\0A\00"
@.str.13 = [14 x i8] c"returned: %d\0A\00"
@str = [25 x i8] c"Locking shared memory...\00"
@str.14 = [27 x i8] c"Unlocking shared memory...\00"
@str.15 = [27 x i8] c"Deleting shared memory... \00"
define i32 @main() {
%1 = alloca [3 x i8]
%2 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 6, i32 10)
%4 = tail call i32 @sharedMemoryCreate(i32 6, i32 10)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 10)
%7 = tail call i32 @sharedMemoryGet(i32 6, i32 10)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* null)
%10 = tail call i64 @sharedMemoryAttach(i32 %7)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @sharedMemoryLock(i32 %7)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%17 = call i32 @strlen(i8* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %0, %19
%20 = phi i64 [ %24, %19 ], [ 0, %0 ]
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %11, i64 %20
store i8 %22, i8* %23
%24 = add nuw nsw i64 %20, 1
%25 = call i32 @strlen(i8* %2)
%26 = zext i32 %25 to i64
%27 = icmp ult i64 %24, %26
br i1 %27, label %19, label %28
28: ; preds = %19, %0
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i8* %11)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.14, i64 0, i64 0))
%31 = call i32 @sharedMemoryUnlock(i32 %7)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0), i8* %11)
%34 = call i32 @sharedMemoryDetatch(i8* %11)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.15, i64 0, i64 0))
%38 = call i32 @sharedMemoryDelete(i32 %7)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0), i32 %38)
ret i32 0
}