#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));
  const int bufflen = 255;
  key_t key = 0xdeadc0de;
  int shmid;
  char znak;
  char *buffer;
  shmid = shmget(key, bufflen, IPC_CREAT | 0600);
  if (shmid == -1) {
    printf("Error shmget\n");
    exit(0);
  }
  buffer = shmat(shmid, NULL, 0);
  if (buffer == NULL) {
    printf("Error shmat\n");
    exit(0);
  }
  int i;
  while (1) {
    znak = rand() % 255;
    buffer[i % bufflen] = znak;
    i++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
movl   $0xff,-0x8(%rbp)
movl   $0xdeadc0de,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0xff,%esi
call   1070 <shmget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d9 <main+0x69>
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <main+0x99>
lea    0xe11(%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    $0xff,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%rax
mov    %edx,%ecx
mov    -0x29(%rbp),%dl
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    %eax,-0x24(%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    $0xdeadc0de,%edi
mov    $0xff,%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>
movslq %eax,%rcx
imul   $0xffffffff80808081,%rcx,%rax
shr    $0x20,%rax
add    %ecx,%eax
mov    %eax,%edx
shr    $0x1f,%edx
shr    $0x7,%eax
add    %edx,%eax
add    %ecx,%eax
movslq %ebx,%rbx
imul   $0xffffffff80808081,%rbx,%rcx
shr    $0x20,%rcx
add    %ebx,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x7,%ecx
add    %edx,%ecx
mov    %ecx,%edx
shl    $0x8,%edx
sub    %edx,%ecx
add    %ebx,%ecx
movslq %ecx,%rcx
mov    %al,(%r14,%rcx,1)
add    $0x1,%ebx
jmp    11c0 <main+0x50>
lea    0xdf3(%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 = [14 x i8] c"Error shmget\0A\00"
@.str.1 = [13 x i8] c"Error 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 255, i32* %2
store i32 -559038242, i32* %3
%10 = load i32, i32* %3
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 384
%13 = call i32 @shmget(i32 %10, i32 255, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %0
%20 = load i32, i32* %4
%21 = call i8* @shmat(i32 %20, i32* null, i32 0)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 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, 255
%31 = trunc i32 %30 to i8
store i8 %31, i8* %5
%32 = load i8, i8* %5
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = srem i32 %34, 255
%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 = [12 x i8] c"Error shmat\00"
@str.2 = [13 x i8] c"Error 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 -559038242, i32 255, 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 ([13 x i8], [13 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 ([12 x i8], [12 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, 255
%20 = trunc i32 %19 to i8
%21 = srem i32 %17, 255
%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
}