#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 EXIT_SUCCESS;
int atoi(char *);
double calcAverage(struct my_msgbuf *, int, int);
int /*<<< orphan*/ close_shm(int);
int getMemoryCorrectSize(int);
int *get_ptr_to_shm(int);
int get_time_correct_period(int);
int /*<<< orphan*/ incorect_param();
int init_shm(int, int);
int /*<<< orphan*/ print_result(double);
int /*<<< orphan*/ print_welcome_message(int, int);
int /*<<< orphan*/ setHandlers();
int /*<<< orphan*/ wait_for_data(int *, int, int, int);
int main(int argc, char **argv) {
  int shm_id = 0, ext_key = 0, shm_size = 0;
  double pai_res = 0;
  int *counter = NULL;
  int time_period = 0;
  struct my_msgbuf *data_base = NULL;
  setHandlers();
  if (argc != 4)
    incorect_param();
  time_period = get_time_correct_period(atoi(argv[2]));
  shm_size = getMemoryCorrectSize(atoi(argv[3]));
  ext_key = atoi(argv[1]);
  print_welcome_message(time_period, shm_size);
  shm_id = init_shm(ext_key, shm_size);
  counter = get_ptr_to_shm(shm_id);
  (*counter) = shm_size;
  data_base = (struct my_msgbuf *)(counter + 1);
  wait_for_data(counter, time_period, shm_id, shm_size);
  pai_res = calcAverage(data_base, (*counter), shm_size);
  if (pai_res)
    print_result(pai_res);
  close_shm(shm_id);
  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)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <setHandlers@plt>
cmpl   $0x4,-0x8(%rbp)
je     1222 <main+0x62>
mov    $0x0,%al
call   1090 <incorect_param@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1080 <get_time_correct_period@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   10e0 <getMemoryCorrectSize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   10b0 <print_welcome_message@plt>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <init_shm@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   10c0 <get_ptr_to_shm@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1050 <wait_for_data@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1060 <calcAverage@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    12e0 <main+0x120>
jp     12e0 <main+0x120>
jmp    12ea <main+0x12a>
movsd  -0x28(%rbp),%xmm0
call   1070 <print_result@plt>
mov    -0x14(%rbp),%edi
call   1040 <close_shm@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <setHandlers@plt>
cmp    $0x4,%ebp
je     11e0 <main+0x20>
xor    %eax,%eax
call   1090 <incorect_param@plt>
mov    0x10(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1080 <get_time_correct_period@plt>
mov    %eax,%r14d
mov    0x18(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   10e0 <getMemoryCorrectSize@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
mov    %r14d,%edi
mov    %ebp,%esi
call   10b0 <print_welcome_message@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <init_shm@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   10c0 <get_ptr_to_shm@plt>
mov    %rax,%rbx
mov    %ebp,(%rax)
lea    0x4(%rax),%r12
mov    %rax,%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
call   1050 <wait_for_data@plt>
mov    (%rbx),%esi
mov    %r12,%rdi
mov    %ebp,%edx
call   1060 <calcAverage@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    125e <main+0x9e>
jnp    1263 <main+0xa3>
call   1070 <print_result@plt>
mov    %r15d,%edi
call   1040 <close_shm@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.my_msgbuf = type { i32 }
@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 i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i32*
%11 = alloca i32
%12 = 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 i32 0, i32* %7
store i32 0, i32* %8
store double 0.000000e+00, double* %9
store i32* null, i32** %10
store i32 0, i32* %11
store %struct.my_msgbuf* null, %struct.my_msgbuf** %12
%13 = call i32 (...) @setHandlers()
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @incorect_param()
br label %18
18: ; preds = %16, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
%23 = call i32 @get_time_correct_period(i32 %22)
store i32 %23, i32* %11
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 3
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = call i32 @getMemoryCorrectSize(i32 %27)
store i32 %28, i32* %8
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %11
%34 = load i32, i32* %8
%35 = call i32 @print_welcome_message(i32 %33, i32 %34)
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = call i32 @init_shm(i32 %36, i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = call i32* @get_ptr_to_shm(i32 %39)
store i32* %40, i32** %10
%41 = load i32, i32* %8
%42 = load i32*, i32** %10
store i32 %41, i32* %42
%43 = load i32*, i32** %10
%44 = getelementptr inbounds i32, i32* %43, i64 1
%45 = bitcast i32* %44 to %struct.my_msgbuf*
store %struct.my_msgbuf* %45, %struct.my_msgbuf** %12
%46 = load i32*, i32** %10
%47 = load i32, i32* %11
%48 = load i32, i32* %6
%49 = load i32, i32* %8
%50 = call i32 @wait_for_data(i32* %46, i32 %47, i32 %48, i32 %49)
%51 = load %struct.my_msgbuf*, %struct.my_msgbuf** %12
%52 = load i32*, i32** %10
%53 = load i32, i32* %52
%54 = load i32, i32* %8
%55 = call double @calcAverage(%struct.my_msgbuf* %51, i32 %53, i32 %54)
store double %55, double* %9
%56 = load double, double* %9
%57 = fcmp une double %56, 0.000000e+00
br i1 %57, label %58, label %61
58: ; preds = %18
%59 = load double, double* %9
%60 = call i32 @print_result(double %59)
br label %61
61: ; preds = %58, %18
%62 = load i32, i32* %6
%63 = call i32 @close_shm(i32 %62)
%64 = load i32, i32* @EXIT_SUCCESS
ret i32 %64
}

LLVM IR (-O2):
%struct.my_msgbuf = type { i32 }
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @setHandlers()
%4 = icmp eq i32 %0, 4
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (...) @incorect_param()
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = tail call i32 @get_time_correct_period(i32 %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 3
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i32 @getMemoryCorrectSize(i32 %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = tail call i32 @print_welcome_message(i32 %11, i32 %15)
%20 = tail call i32 @init_shm(i32 %18, i32 %15)
%21 = tail call i32* @get_ptr_to_shm(i32 %20)
store i32 %15, i32* %21
%22 = getelementptr inbounds i32, i32* %21, i64 1
%23 = bitcast i32* %22 to %struct.my_msgbuf*
%24 = tail call i32 @wait_for_data(i32* %21, i32 %11, i32 %20, i32 %15)
%25 = load i32, i32* %21
%26 = tail call double @calcAverage(%struct.my_msgbuf* %23, i32 %25, i32 %15)
%27 = fcmp une double %26, 0.000000e+00
br i1 %27, label %28, label %30
28: ; preds = %7
%29 = tail call i32 @print_result(double %26)
br label %30
30: ; preds = %28, %7
%31 = tail call i32 @close_shm(i32 %20)
%32 = load i32, i32* @EXIT_SUCCESS
ret i32 %32
}