#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 timeval {
  int tv_sec;
  int tv_usec;
};
typedef int key_t;

/* Variables and functions */
int EXIT_SUCCESS;
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, float);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(time(NULL));
  key_t key = 2137;
  struct timeval startTime, stopTime;
  float timeMeasurement;
  int shmid;
  int *sharedMemory;
  int i;
  gettimeofday(&startTime, NULL);
  shmid = shmget(key, 30 * sizeof(char), IPC_CREAT | 0666);
  if (shmid < 0) {
    perror("Sharing memory error");
    exit(1);
  }
  sharedMemory = (int *)shmat(shmid, NULL, 0);
  if (sharedMemory == NULL) {
    perror("Sharing memory segment error");
    exit(1);
  }
  for (i = 0; i < 30; i++) {
    printf("%d\n", sharedMemory[i]);
  }
  gettimeofday(&stopTime, NULL);
  timeMeasurement = (stopTime.tv_sec - startTime.tv_sec) * 1000 +
                    (stopTime.tv_usec - startTime.tv_usec) / 1000;
  printf("\nDone! Elapsed time: %.0f ms.\n", timeMeasurement);
  exit(1);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x859,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x8(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1e,%esi
call   1090 <shmget@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f2 <main+0x72>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1225 <main+0xa5>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1e,-0x2c(%rbp)
jge    125d <main+0xdd>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cvtsi2ssl (%rax,%rcx,4),%xmm0
lea    0xde8(%rip),%rdi
call   1030 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    122c <main+0xac>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
lea    0xd93(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x2e36(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x859,%edi
mov    $0x1e,%esi
call   1090 <shmget@plt>
test   %eax,%eax
jns    11cd <main+0x4d>
lea    0xe35(%rip),%rdi
jmp    11e4 <main+0x64>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
test   %rax,%rax
jne    11f3 <main+0x73>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0xe33(%rip),%r14
nop
xorps  %xmm0,%xmm0
cvtsi2ssl (%r15,%rbx,4),%xmm0
mov    %r14,%rdi
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    $0x1e,%rbx
jne    1200 <main+0x80>
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x8(%rsp),%eax
imul   $0x3e8,%eax,%eax
sub    0xc(%rsp),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2ss %ecx,%xmm0
lea    0xdd5(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@IPC_CREAT = global i32 0
@.str = [21 x i8] c"Sharing memory error\00"
@.str.1 = [29 x i8] c"Sharing memory segment error\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [31 x i8] c"\0ADone! Elapsed time: %.0f ms.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca float
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 @time(i32* null)
%10 = call i32 @srand(i32 %9)
store i32 2137, i32* %2
%11 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%12 = load i32, i32* %2
%13 = load i32, i32* @IPC_CREAT
%14 = or i32 %13, 438
%15 = call i32 @shmget(i32 %12, i32 30, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = load i32, i32* %6
%23 = call i64 @shmat(i32 %22, i32* null, i32 0)
%24 = inttoptr i64 %23 to i32*
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
store i32 0, i32* %8
br label %31
31: ; preds = %42, %30
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 30
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = load i32*, i32** %7
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = sitofp i32 %39 to float
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), float %40)
br label %42
42: ; preds = %34
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %31
45: ; preds = %31
%46 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sub nsw i32 %48, %50
%52 = mul nsw i32 %51, 1000
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%56 = load i32, i32* %55
%57 = sub nsw i32 %54, %56
%58 = sdiv i32 %57, 1000
%59 = add nsw i32 %52, %58
%60 = sitofp i32 %59 to float
store float %60, float* %5
%61 = load float, float* %5
%62 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), float %61)
%63 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@IPC_CREAT = global i32 0
@.str = [21 x i8] c"Sharing memory error\00"
@.str.1 = [29 x i8] c"Sharing memory segment error\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [31 x i8] c"\0ADone! Elapsed time: %.0f ms.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 @srand(i32 %3)
%5 = bitcast %struct.timeval* %1 to i8*
%6 = bitcast %struct.timeval* %2 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 438
%10 = tail call i32 @shmget(i32 2137, i32 30, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = tail call i64 @shmat(i32 %10, i32* null, i32 0)
%17 = inttoptr i64 %16 to i32*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %15, %22
%23 = phi i64 [ %28, %22 ], [ 0, %15 ]
%24 = getelementptr inbounds i32, i32* %17, i64 %23
%25 = load i32, i32* %24
%26 = sitofp i32 %25 to float
%27 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), float %26)
%28 = add nuw nsw i64 %23, 1
%29 = icmp eq i64 %28, 30
br i1 %29, label %30, label %22
30: ; preds = %22
%31 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%35 = load i32, i32* %34
%36 = sub nsw i32 %33, %35
%37 = mul nsw i32 %36, 1000
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%41 = load i32, i32* %40
%42 = sub nsw i32 %39, %41
%43 = sdiv i32 %42, 1000
%44 = add nsw i32 %43, %37
%45 = sitofp i32 %44 to float
%46 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), float %45)
%47 = tail call i32 @exit(i32 1)
unreachable
}