#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ fresnl(double, double *, double *);
scalar_t__ malloc(unsigned int const);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write_data_as_json(int /*<<< orphan*/ *, double *, double *,
                                      double *, unsigned int const);
void generate(double *x, const unsigned int len, const char *name) {
  unsigned int i;
  double *S;
  double *C;
  FILE *f;
  S = (double *)malloc(len * sizeof(double));
  if (S == NULL) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  C = (double *)malloc(len * sizeof(double));
  if (C == NULL) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  for (i = 0; i < len; i++) {
    fresnl(x[i], S + i, C + i);
  }
  f = fopen(name, "w");
  if (f == NULL) {
    printf("Error opening file.\n");
    exit(1);
  }
  write_data_as_json(f, x, S, C, len);
  fclose(f);
  free(S);
  free(C);
}


x86 assembly (clang, -O0):
<generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <generate+0x46>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <generate+0x79>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1247 <generate+0xc7>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x3,%rax
add    %rax,%rsi
call   1060 <fresnl@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1200 <generate+0x80>
mov    -0x18(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    127c <generate+0xfc>
lea    0xdaf(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0xc(%rbp),%r8d
call   1070 <write_data_as_json@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x0(,%r14,8),%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     124e <generate+0xce>
mov    %rax,%r12
mov    %rbp,(%rsp)
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     124e <generate+0xce>
mov    %rax,%rbx
test   %r14d,%r14d
je     11fc <generate+0x7c>
mov    %r14d,%ebp
shl    $0x3,%rbp
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%r15,%r13,1),%xmm0
lea    (%r12,%r13,1),%rdi
lea    (%rbx,%r13,1),%rsi
call   1060 <fresnl@plt>
add    $0x8,%r13
cmp    %r13,%rbp
jne    11e0 <generate+0x60>
lea    0xdfd(%rip),%rsi
mov    (%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1257 <generate+0xd7>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
mov    %r14d,%r8d
call   1070 <write_data_as_json@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xdc1(%rip),%rdi
jmp    125e <generate+0xde>
lea    0xda4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Error allocating memory.\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [21 x i8] c"Error opening file.\0A\00"
define void @generate(double* %0, i32 %1, i8* %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca double*
%9 = alloca double*
%10 = alloca i32*
store double* %0, double** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i32, i32* %5
%12 = zext i32 %11 to i64
%13 = mul i64 %12, 8
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to double*
store double* %16, double** %8
%17 = load double*, double** %8
%18 = icmp eq double* %17, null
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = load i32, i32* %5
%24 = zext i32 %23 to i64
%25 = mul i64 %24, 8
%26 = trunc i64 %25 to i32
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to double*
store double* %28, double** %9
%29 = load double*, double** %9
%30 = icmp eq double* %29, null
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %22
store i32 0, i32* %7
br label %35
35: ; preds = %54, %34
%36 = load i32, i32* %7
%37 = load i32, i32* %5
%38 = icmp ult i32 %36, %37
br i1 %38, label %39, label %57
39: ; preds = %35
%40 = load double*, double** %4
%41 = load i32, i32* %7
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds double, double* %40, i64 %42
%44 = load double, double* %43
%45 = load double*, double** %8
%46 = load i32, i32* %7
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
%49 = load double*, double** %9
%50 = load i32, i32* %7
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds double, double* %49, i64 %51
%53 = call i32 @fresnl(double %44, double* %48, double* %52)
br label %54
54: ; preds = %39
%55 = load i32, i32* %7
%56 = add i32 %55, 1
store i32 %56, i32* %7
br label %35
57: ; preds = %35
%58 = load i8*, i8** %6
%59 = call i32* @fopen(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %59, i32** %10
%60 = load i32*, i32** %10
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %57
%66 = load i32*, i32** %10
%67 = load double*, double** %4
%68 = load double*, double** %8
%69 = load double*, double** %9
%70 = load i32, i32* %5
%71 = call i32 @write_data_as_json(i32* %66, double* %67, double* %68, double* %69, i32 %70)
%72 = load i32*, i32** %10
%73 = call i32 @fclose(i32* %72)
%74 = load double*, double** %8
%75 = call i32 @free(double* %74)
%76 = load double*, double** %9
%77 = call i32 @free(double* %76)
ret void
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@str = [20 x i8] c"Error opening file.\00"
@str.4 = [25 x i8] c"Error allocating memory.\00"
define void @generate(double* %0, i32 %1, i8* nocapture %2) {
%4 = shl i32 %1, 3
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to double*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i64 @malloc(i32 %4)
%13 = inttoptr i64 %12 to double*
%14 = icmp eq i64 %12, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = icmp eq i32 %1, 0
br i1 %16, label %31, label %17
17: ; preds = %15
%18 = zext i32 %1 to i64
br label %22
19: ; preds = %11
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %17, %22
%23 = phi i64 [ 0, %17 ], [ %29, %22 ]
%24 = getelementptr inbounds double, double* %0, i64 %23
%25 = load double, double* %24
%26 = getelementptr inbounds double, double* %6, i64 %23
%27 = getelementptr inbounds double, double* %13, i64 %23
%28 = tail call i32 @fresnl(double %25, double* %26, double* %27)
%29 = add nuw nsw i64 %23, 1
%30 = icmp eq i64 %29, %18
br i1 %30, label %31, label %22
31: ; preds = %22, %15
%32 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = tail call i32 @write_data_as_json(i32* %32, double* %0, double* %6, double* %13, i32 %1)
%39 = tail call i32 @fclose(i32* %32)
%40 = tail call i32 @free(double* %6)
%41 = tail call i32 @free(double* %13)
ret void
}