#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 */
scalar_t__ function(double);
double calculate_trapez(double N) {
  double result = 0;
  double a = -400;
  double b = 600;
  double h = (b - a) / N;
  int i;
  for (i = 2; i < N; i++) {
    double x_i = a + (i - 1) * h;
    result += function(x_i);
  }
  result *= 2;
  result += function(a);
  result += function(b);
  result *= h / 2;
  return result;
}


x86 assembly (clang, -O0):
<calculate_trapez>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xedb(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xec6(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
divsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x2,-0x2c(%rbp)
cvtsi2sdl -0x2c(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11b6 <calculate_trapez+0xa6>
movsd  -0x18(%rbp),%xmm1
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  -0x28(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1030 <function@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    115a <calculate_trapez+0x4a>
movsd  0xe52(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <function@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <function@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe09(%rip),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_trapez>:
push   %rbx
sub    $0x20,%rsp
movsd  0xee3(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
xorpd  %xmm2,%xmm2
ucomisd 0xed5(%rip),%xmm0
jbe    11ab <calculate_trapez+0x9b>
xorpd  %xmm2,%xmm2
mov    $0x3,%ebx
movsd  %xmm0,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm2,0x8(%rsp)
lea    -0x2(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  0xea2(%rip),%xmm0
call   1030 <function@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
movsd  0x8(%rsp),%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
movsd  0x8(%rsp),%xmm2
add    $0x1,%ebx
movsd  0x18(%rsp),%xmm0
ucomisd %xmm1,%xmm0
ja     1150 <calculate_trapez+0x40>
addsd  %xmm2,%xmm2
movsd  %xmm2,0x8(%rsp)
movsd  0xe57(%rip),%xmm0
call   1030 <function@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  0xe3e(%rip),%xmm0
call   1030 <function@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
addsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm0
mulsd  0xe25(%rip),%xmm0
mulsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @calculate_trapez(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store double %0, double* %2
store double 0.000000e+00, double* %3
store double -4.000000e+02, double* %4
store double 6.000000e+02, double* %5
%9 = load double, double* %5
%10 = load double, double* %4
%11 = fsub double %9, %10
%12 = load double, double* %2
%13 = fdiv double %11, %12
store double %13, double* %6
store i32 2, i32* %7
br label %14
14: ; preds = %31, %1
%15 = load i32, i32* %7
%16 = sitofp i32 %15 to double
%17 = load double, double* %2
%18 = fcmp olt double %16, %17
br i1 %18, label %19, label %34
19: ; preds = %14
%20 = load double, double* %4
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 1
%23 = sitofp i32 %22 to double
%24 = load double, double* %6
%25 = call double @llvm.fmuladd.f64(double %23, double %24, double %20)
store double %25, double* %8
%26 = load double, double* %8
%27 = call i64 @function(double %26)
%28 = sitofp i64 %27 to double
%29 = load double, double* %3
%30 = fadd double %29, %28
store double %30, double* %3
br label %31
31: ; preds = %19
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %14
34: ; preds = %14
%35 = load double, double* %3
%36 = fmul double %35, 2.000000e+00
store double %36, double* %3
%37 = load double, double* %4
%38 = call i64 @function(double %37)
%39 = sitofp i64 %38 to double
%40 = load double, double* %3
%41 = fadd double %40, %39
store double %41, double* %3
%42 = load double, double* %5
%43 = call i64 @function(double %42)
%44 = sitofp i64 %43 to double
%45 = load double, double* %3
%46 = fadd double %45, %44
store double %46, double* %3
%47 = load double, double* %6
%48 = fdiv double %47, 2.000000e+00
%49 = load double, double* %3
%50 = fmul double %49, %48
store double %50, double* %3
%51 = load double, double* %3
ret double %51
}

LLVM IR (-O2):
define double @calculate_trapez(double %0) {
%2 = fdiv double 1.000000e+03, %0
%3 = fcmp ogt double %0, 2.000000e+00
br i1 %3, label %4, label %18
4: ; preds = %1, %4
%5 = phi double [ %12, %4 ], [ 0.000000e+00, %1 ]
%6 = phi i32 [ %13, %4 ], [ 2, %1 ]
%7 = add nsw i32 %6, -1
%8 = sitofp i32 %7 to double
%9 = tail call double @llvm.fmuladd.f64(double %8, double %2, double -4.000000e+02)
%10 = tail call i64 @function(double %9)
%11 = sitofp i64 %10 to double
%12 = fadd double %5, %11
%13 = add nuw nsw i32 %6, 1
%14 = sitofp i32 %13 to double
%15 = fcmp olt double %14, %0
br i1 %15, label %4, label %16
16: ; preds = %4
%17 = fmul double %12, 2.000000e+00
br label %18
18: ; preds = %16, %1
%19 = phi double [ 0.000000e+00, %1 ], [ %17, %16 ]
%20 = tail call i64 @function(double -4.000000e+02)
%21 = sitofp i64 %20 to double
%22 = fadd double %19, %21
%23 = tail call i64 @function(double 6.000000e+02)
%24 = sitofp i64 %23 to double
%25 = fadd double %22, %24
%26 = fmul double %2, 5.000000e-01
%27 = fmul double %26, %25
ret double %27
}