#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 */
typedef scalar_t__ clock_t;

/* Variables and functions */
float CLOCKS_PER_SEC;
int IPC_CREAT;
scalar_t__ clock();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, float, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int main(void) {
  int size = 40;
  int shmid, i, counter = 0;
  long *buf;
  clock_t start = clock();
  shmid = shmget(45281, size * sizeof(char), IPC_CREAT | 0600);
  if (shmid == -1) {
    perror("Creating shared memory segment");
    exit(1);
  }
  buf = (long *)shmat(shmid, NULL, 0);
  if (buf == NULL) {
    perror("Attaching shared memory segment");
    exit(1);
  }
  for (i = 0; i < size; i++)
    printf("Character number: %d Value: %li\n", i, buf[i]);
  clock_t end = clock();
  float seconds = (float)(end - start) / CLOCKS_PER_SEC;
  printf("Done in: %f seconds\n", seconds);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x28,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x28(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0xb0e1,%edi
call   1070 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ce <main+0x6e>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1201 <main+0xa1>
lea    0xe2d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1241 <main+0xe1>
cvtsi2ssl -0x10(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe13(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1208 <main+0xa8>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2ss %rax,%xmm0
mov    0x2d88(%rip),%rax
divss  (%rax),%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
lea    0xdeb(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r14
mov    0x2e5f(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0xb0e1,%edi
mov    $0x28,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     1208 <main+0xa8>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
test   %rax,%rax
je     1211 <main+0xb1>
mov    %rax,%r12
xor    %ebx,%ebx
lea    0xe8f(%rip),%r15
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
mov    (%r12,%rbx,8),%rsi
mov    %r15,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x28,%rbx
jne    11b0 <main+0x50>
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
mov    0x2e00(%rip),%rax
divss  (%rax),%xmm0
lea    0xe6d(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf1(%rip),%rdi
jmp    1218 <main+0xb8>
lea    0xe07(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [31 x i8] c"Creating shared memory segment\00"
@.str.1 = [32 x i8] c"Attaching shared memory segment\00"
@.str.2 = [33 x i8] c"Character number: %d Value: %li\0A\00"
@CLOCKS_PER_SEC = global float 0.000000e+00
@.str.3 = [21 x i8] c"Done in: %f seconds\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
%9 = alloca float
store i32 0, i32* %1
store i32 40, i32* %2
store i32 0, i32* %5
%10 = call i64 (...) @clock()
store i64 %10, i64* %7
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 1
%14 = trunc i64 %13 to i32
%15 = load i32, i32* @IPC_CREAT
%16 = or i32 %15, 384
%17 = call i32 @shmget(i32 45281, i32 %14, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = load i32, i32* %3
%25 = call i64 @shmat(i32 %24, i32* null, i32 0)
%26 = inttoptr i64 %25 to i64*
store i64* %26, i64** %6
%27 = load i64*, i64** %6
%28 = icmp eq i64* %27, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
store i32 0, i32* %4
br label %33
33: ; preds = %46, %32
%34 = load i32, i32* %4
%35 = load i32, i32* %2
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %49
37: ; preds = %33
%38 = load i32, i32* %4
%39 = sitofp i32 %38 to float
%40 = load i64*, i64** %6
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i64, i64* %40, i64 %42
%44 = load i64, i64* %43
%45 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), float %39, i64 %44)
br label %46
46: ; preds = %37
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %33
49: ; preds = %33
%50 = call i64 (...) @clock()
store i64 %50, i64* %8
%51 = load i64, i64* %8
%52 = load i64, i64* %7
%53 = sub nsw i64 %51, %52
%54 = sitofp i64 %53 to float
%55 = load float, float* @CLOCKS_PER_SEC
%56 = fdiv float %54, %55
store float %56, float* %9
%57 = load float, float* %9
%58 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), float %57)
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [31 x i8] c"Creating shared memory segment\00"
@.str.1 = [32 x i8] c"Attaching shared memory segment\00"
@.str.2 = [33 x i8] c"Character number: %d Value: %li\0A\00"
@CLOCKS_PER_SEC = global float 0.000000e+00
@.str.3 = [21 x i8] c"Done in: %f seconds\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @clock()
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 384
%4 = tail call i32 @shmget(i32 45281, i32 40, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i64 @shmat(i32 %4, i32* null, i32 0)
%11 = inttoptr i64 %10 to i64*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9, %16
%17 = phi i64 [ %23, %16 ], [ 0, %9 ]
%18 = trunc i64 %17 to i32
%19 = sitofp i32 %18 to float
%20 = getelementptr inbounds i64, i64* %11, i64 %17
%21 = load i64, i64* %20
%22 = tail call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), float %19, i64 %21)
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, 40
br i1 %24, label %25, label %16
25: ; preds = %16
%26 = tail call i64 (...) @clock()
%27 = sub nsw i64 %26, %1
%28 = sitofp i64 %27 to float
%29 = load float, float* @CLOCKS_PER_SEC
%30 = fdiv float %28, %29
%31 = tail call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), float %30)
ret i32 0
}