#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 */
struct my_msgbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ atoi(char *);
double culcPai(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int *get_ptr_to_shm(int);
int /*<<< orphan*/ incorect_param();
int init_msg(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_to_memory(struct my_msgbuf *, int /*<<< orphan*/,
                                   double, int);
int main(int argc, char **argv) {
  int shm_id = 0;
  double pai_calculated = 0;
  int *counter = NULL;
  struct my_msgbuf *shm_ptr = NULL;
  if (argc != 3)
    incorect_param();
  shm_id = init_msg(atoi(argv[1]));
  counter = get_ptr_to_shm(shm_id);
  shm_ptr = (struct my_msgbuf *)(counter + 1);
  if (!(*counter)) {
    fprintf(stderr, "Shered memory - Access blocked by server!\n");
    exit(EXIT_FAILURE);
  }
  pai_calculated = culcPai(atoi(argv[2]));
  (*counter)--;
  write_to_memory(shm_ptr, atoi(argv[2]), pai_calculated, (*counter));
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c6 <main+0x46>
mov    $0x0,%al
call   1050 <incorect_param@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1030 <init_msg@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <get_ptr_to_shm@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1225 <main+0xa5>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   10a0 <culcPai@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <write_to_memory@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x3,%edi
je     1196 <main+0x16>
xor    %eax,%eax
call   1050 <incorect_param@plt>
mov    0x8(%r14),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1030 <init_msg@plt>
mov    %eax,%edi
call   1060 <get_ptr_to_shm@plt>
cmpl   $0x0,(%rax)
je     1206 <main+0x86>
mov    %rax,%rbx
mov    %rax,%r15
add    $0x4,%r15
mov    0x10(%r14),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   10a0 <culcPai@plt>
movsd  %xmm0,(%rsp)
mov    (%rbx),%ebp
add    $0xffffffff,%ebp
mov    %ebp,(%rbx)
mov    0x10(%r14),%rdi
call   1080 <atoi@plt>
mov    %r15,%rdi
mov    %eax,%esi
movsd  (%rsp),%xmm0
mov    %ebp,%edx
call   1070 <write_to_memory@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.my_msgbuf = type { i32 }
@stderr = global i32 0
@.str = [43 x i8] c"Shered memory - Access blocked by server!\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca double
%8 = alloca i32*
%9 = alloca %struct.my_msgbuf*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store double 0.000000e+00, double* %7
store i32* null, i32** %8
store %struct.my_msgbuf* null, %struct.my_msgbuf** %9
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (...) @incorect_param()
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
%19 = call i32 @init_msg(i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = call i32* @get_ptr_to_shm(i32 %20)
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = getelementptr inbounds i32, i32* %22, i64 1
%24 = bitcast i32* %23 to %struct.my_msgbuf*
store %struct.my_msgbuf* %24, %struct.my_msgbuf** %9
%25 = load i32*, i32** %8
%26 = load i32, i32* %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %14
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = call double @culcPai(i32 %37)
store double %38, double* %7
%39 = load i32*, i32** %8
%40 = load i32, i32* %39
%41 = add nsw i32 %40, -1
store i32 %41, i32* %39
%42 = load %struct.my_msgbuf*, %struct.my_msgbuf** %9
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = call i32 @atoi(i8* %45)
%47 = load double, double* %7
%48 = load i32*, i32** %8
%49 = load i32, i32* %48
%50 = call i32 @write_to_memory(%struct.my_msgbuf* %42, i32 %46, double %47, i32 %49)
%51 = load i32, i32* @EXIT_SUCCESS
ret i32 %51
}

LLVM IR (-O2):
%struct.my_msgbuf = type { i32 }
@stderr = global i32 0
@.str = [43 x i8] c"Shered memory - Access blocked by server!\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (...) @incorect_param()
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 @init_msg(i32 %9)
%11 = tail call i32* @get_ptr_to_shm(i32 %10)
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %6
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %6
%20 = getelementptr inbounds i32, i32* %11, i64 1
%21 = bitcast i32* %20 to %struct.my_msgbuf*
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @atoi(i8* %23)
%25 = tail call double @culcPai(i32 %24)
%26 = load i32, i32* %11
%27 = add nsw i32 %26, -1
store i32 %27, i32* %11
%28 = load i8*, i8** %22
%29 = tail call i32 @atoi(i8* %28)
%30 = tail call i32 @write_to_memory(%struct.my_msgbuf* %21, i32 %29, double %25, i32 %27)
%31 = load i32, i32* @EXIT_SUCCESS
ret i32 %31
}