#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 */
int IPC_CREAT;
scalar_t__ clock();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
main() {
  int shmid, i;
  int *buf;
  int licznik;
  shmid = shmget(45281, 40 * sizeof(char), IPC_CREAT | 0600);
  if (shmid == -1) {
    perror("Utworzenie segmentu pamieci wspoldzielonej");
    exit(1);
  }
  buf = (int *)shmat(shmid, NULL, 0);
  if (buf == NULL) {
    perror("Przylaczenie segmentu pamieci wspoldzielonej");
    exit(1);
  }
  for (i = 0; i < 40 * sizeof(char); i++) {
    printf("Numer: %5d Wartosc: %5d\n", i, buf[i]);
    clock_t start = clock();
    if (buf[i] != buf[i - 1])
      licznik = 0;
    else
      licznik++;
    if (licznik == 5) {
      printf("wykonywanie trwalo: %lu ms\n", clock() - start);
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0xb0e1,%edi
mov    $0x28,%esi
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b0 <main+0x50>
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <main+0x83>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x28,%rax
jae    1297 <main+0x137>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x18(%rbp),%rcx
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
je     124c <main+0xec>
movl   $0x0,-0x1c(%rbp)
jmp    1255 <main+0xf5>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jne    1284 <main+0x124>
mov    $0x0,%al
call   1030 <clock@plt>
sub    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1289 <main+0x129>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ea <main+0x8a>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0xb0e1,%edi
mov    $0x28,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     122e <main+0xce>
xor    %r13d,%r13d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
test   %rax,%rax
je     1237 <main+0xd7>
mov    %rax,%r15
lea    0xea6(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15,%rbx,4),%edx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    (%r15,%rbx,4),%ecx
add    $0x1,%ebp
cmp    -0x4(%r15,%rbx,4),%ecx
cmovne %r13d,%ebp
cmp    $0x5,%ebp
je     1207 <main+0xa7>
add    $0x1,%rbx
cmp    $0x28,%rbx
jne    11c0 <main+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r12
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r12d,%eax
lea    0xe56(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdcb(%rip),%rdi
jmp    123e <main+0xde>
lea    0xded(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [43 x i8] c"Utworzenie segmentu pamieci wspoldzielonej\00"
@.str.1 = [45 x i8] c"Przylaczenie segmentu pamieci wspoldzielonej\00"
@.str.2 = [25 x i8] c"Numer: %5d Wartosc: %5d\0A\00"
@.str.3 = [28 x i8] c"wykonywanie trwalo: %lu ms\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
%7 = load i32, i32* @IPC_CREAT
%8 = or i32 %7, 384
%9 = call i32 @shmget(i32 45281, i32 40, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i64 @shmat(i32 %16, i32* null, i32 0)
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %4
%19 = load i32*, i32** %4
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
store i32 0, i32* %3
br label %25
25: ; preds = %65, %24
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = icmp ult i64 %27, 40
br i1 %28, label %29, label %68
29: ; preds = %25
%30 = load i32, i32* %3
%31 = load i32*, i32** %4
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %35)
%37 = call i64 (...) @clock()
store i64 %37, i64* %6
%38 = load i32*, i32** %4
%39 = load i32, i32* %3
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = load i32*, i32** %4
%44 = load i32, i32* %3
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
%48 = load i32, i32* %47
%49 = icmp ne i32 %42, %48
br i1 %49, label %50, label %51
50: ; preds = %29
store i32 0, i32* %5
br label %54
51: ; preds = %29
%52 = load i32, i32* %5
%53 = add nsw i32 %52, 1
store i32 %53, i32* %5
br label %54
54: ; preds = %51, %50
%55 = load i32, i32* %5
%56 = icmp eq i32 %55, 5
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = call i64 (...) @clock()
%59 = load i64, i64* %6
%60 = sub nsw i64 %58, %59
%61 = trunc i64 %60 to i32
%62 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %54
br label %65
65: ; preds = %64
%66 = load i32, i32* %3
%67 = add nsw i32 %66, 1
store i32 %67, i32* %3
br label %25
68: ; preds = %25
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [43 x i8] c"Utworzenie segmentu pamieci wspoldzielonej\00"
@.str.1 = [45 x i8] c"Przylaczenie segmentu pamieci wspoldzielonej\00"
@.str.2 = [25 x i8] c"Numer: %5d Wartosc: %5d\0A\00"
@.str.3 = [28 x i8] c"wykonywanie trwalo: %lu ms\0A\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 384
%3 = tail call i32 @shmget(i32 45281, i32 40, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i64 @shmat(i32 %3, i32* null, i32 0)
%10 = inttoptr i64 %9 to i32*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %18
%16 = add nuw nsw i64 %19, 1
%17 = icmp eq i64 %16, 40
br i1 %17, label %40, label %18
18: ; preds = %8, %15
%19 = phi i64 [ %16, %15 ], [ 0, %8 ]
%20 = phi i32 [ %32, %15 ], [ undef, %8 ]
%21 = getelementptr inbounds i32, i32* %10, i64 %19
%22 = load i32, i32* %21
%23 = trunc i64 %19 to i32
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %22)
%25 = tail call i64 (...) @clock()
%26 = load i32, i32* %21
%27 = add nsw i64 %19, -1
%28 = getelementptr inbounds i32, i32* %10, i64 %27
%29 = load i32, i32* %28
%30 = icmp eq i32 %26, %29
%31 = add nsw i32 %20, 1
%32 = select i1 %30, i32 %31, i32 0
%33 = icmp eq i32 %32, 5
br i1 %33, label %34, label %15
34: ; preds = %18
%35 = tail call i64 (...) @clock()
%36 = sub nsw i64 %35, %25
%37 = trunc i64 %36 to i32
%38 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %15
ret i32 0
}