#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 */

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int rand();
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
main() {
  int shmid, i;
  int *buf;
  char znak;
  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++) {
    znak = rand() % 10;
    buf[i] = znak;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0xb0e1,%edi
mov    $0x28,%esi
call   1050 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a0 <main+0x50>
lea    0xe6f(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d3 <main+0x83>
lea    0xe67(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x28,%rax
jae    1219 <main+0xc9>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11da <main+0x8a>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0xb0e1,%edi
mov    $0x28,%esi
call   1050 <shmget@plt>
cmp    $0xffffffff,%eax
je     11cc <main+0x7c>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
test   %rax,%rax
je     11d5 <main+0x85>
mov    %rax,%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    $0x28,%rbx
jne    1190 <main+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2d(%rip),%rdi
jmp    11dc <main+0x8c>
lea    0xe4f(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <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"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8
store i32 0, i32* %1
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 %6, 384
%8 = call i32 @shmget(i32 45281, i32 40, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i64 @shmat(i32 %15, i32* null, i32 0)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
store i32 0, i32* %3
br label %24
24: ; preds = %38, %23
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %26, 40
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = call i32 (...) @rand()
%30 = srem i32 %29, 10
%31 = trunc i32 %30 to i8
store i8 %31, i8* %5
%32 = load i8, i8* %5
%33 = sext i8 %32 to i32
%34 = load i32*, i32** %4
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 %33, i32* %37
br label %38
38: ; preds = %28
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %24
41: ; preds = %24
%42 = load i32, i32* %1
ret i32 %42
}

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"
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 %15
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 = %8, %15
%16 = phi i64 [ %20, %15 ], [ 0, %8 ]
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 10
%19 = getelementptr inbounds i32, i32* %10, i64 %16
store i32 %18, i32* %19
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, 40
br i1 %21, label %22, label %15
22: ; preds = %15
ret i32 0
}