#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 int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int rand();
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int const, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(time(NULL));
  int shmid;
  const int lenght = 250;
  key_t key = 15;
  char *buff;
  char c;
  shmid = shmget(key, lenght, IPC_CREAT | 0600);
  if (shmid == -1) {
    printf("Blad w shmget\n");
    exit(0);
  }
  buff = shmat(shmid, NULL, 0);
  if (buff == NULL) {
    printf("Blad w shmat\n");
    exit(0);
  }
  int i;
  while (1) {
    c = rand() % 250;
    buff[i % lenght] = c;
    i++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
movl   $0xfa,-0xc(%rbp)
movl   $0xf,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0xfa,%esi
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d9 <main+0x69>
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%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    1209 <main+0x99>
lea    0xe12(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    120e <main+0x9e>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xfa,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x25(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    $0xfa,%ecx
cltd
idiv   %ecx
mov    -0x30(%rbp),%rax
mov    %edx,%ecx
mov    -0x25(%rbp),%dl
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    %eax,-0x20(%rbp)
jmp    120e <main+0x9e>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    0x2e57(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0xf,%edi
mov    $0xfa,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     1212 <main+0xa2>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
test   %rax,%rax
je     121b <main+0xab>
mov    %rax,%r14
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <rand@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x24,%rcx
add    %edx,%ecx
imul   $0xfa,%ecx,%ecx
sub    %ecx,%eax
movslq %ebx,%rbx
imul   $0x10624dd3,%rbx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x24,%rcx
add    %edx,%ecx
imul   $0xfa,%ecx,%ecx
mov    %ebx,%edx
sub    %ecx,%edx
movslq %edx,%rcx
mov    %al,(%r14,%rcx,1)
add    $0x1,%ebx
jmp    11c0 <main+0x50>
lea    0xdf4(%rip),%rdi
jmp    1222 <main+0xb2>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [15 x i8] c"Blad w shmget\0A\00"
@.str.1 = [14 x i8] c"Blad w shmat\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @time(i32* null)
%9 = call i32 @srand(i32 %8)
store i32 250, i32* %3
store i32 15, i32* %4
%10 = load i32, i32* %4
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 384
%13 = call i32 @shmget(i32 %10, i32 250, i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %0
%20 = load i32, i32* %2
%21 = call i8* @shmat(i32 %20, i32* null, i32 0)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %19
br label %28
28: ; preds = %27, %28
%29 = call i32 (...) @rand()
%30 = srem i32 %29, 250
%31 = trunc i32 %30 to i8
store i8 %31, i8* %6
%32 = load i8, i8* %6
%33 = load i8*, i8** %5
%34 = load i32, i32* %7
%35 = srem i32 %34, 250
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 %32, i8* %37
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %28
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@str = [13 x i8] c"Blad w shmat\00"
@str.2 = [14 x i8] c"Blad w shmget\00"
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 @srand(i32 %1)
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 384
%5 = tail call i32 @shmget(i32 15, i32 250, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %10, %16
%17 = phi i32 [ %24, %16 ], [ undef, %10 ]
%18 = tail call i32 (...) @rand()
%19 = srem i32 %18, 250
%20 = trunc i32 %19 to i8
%21 = srem i32 %17, 250
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %11, i64 %22
store i8 %20, i8* %23
%24 = add nsw i32 %17, 1
br label %16
}