#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*/ ellipse_fit_with_gradients(double *, double *, int,
                                              double **, int *, double *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int i;
  double *pts;
  double *grad;
  double param[5];
  char *fname = argv[1];
  FILE *f = fopen(fname, "r");
  if (f == NULL)
    error("invalid input file");
  int pts_size;
  fscanf(f, "%d\n", &pts_size);
  if (pts_size <= 0)
    error("invalid points size");
  pts = (double *)malloc(2 * pts_size * sizeof(double));
  if (pts == NULL)
    error("not enough memory for pts");
  grad = (double *)malloc(2 * pts_size * sizeof(double));
  if (grad == NULL)
    error("not enough memory for grad");
  for (i = 0; i < pts_size, !feof(f); ++i) {
    fscanf(f, "%lf %lf %lf %lf\n", &(pts[2 * i]), &(pts[2 * i + 1]),
           &(grad[2 * i]), &(grad[2 * i + 1]));
  }
  double *buff = (double *)malloc(sizeof(double));
  int size_buff_max = 1;
  ellipse_fit_with_gradients(pts, grad, pts_size, &buff, &size_buff_max, param);
  free(buff);
  printf("Ellipse parameters: ");
  for (i = 0; i < 5; ++i)
    printf("%lf ", param[i]);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    11d0 <main+0x50>
lea    0xe37(%rip),%rdi
call   1080 <error@plt>
mov    -0x60(%rbp),%rdi
lea    0xe3a(%rip),%rsi
lea    -0x64(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
cmpl   $0x0,-0x64(%rbp)
jg     11fc <main+0x7c>
lea    0xe22(%rip),%rdi
call   1080 <error@plt>
mov    -0x64(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122a <main+0xaa>
lea    0xe08(%rip),%rdi
call   1080 <error@plt>
mov    -0x64(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1258 <main+0xd8>
lea    0xdf4(%rip),%rdi
call   1080 <error@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x60(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    127d <main+0xfd>
jmp    12ef <main+0x16f>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%rdx
mov    -0x20(%rbp),%rcx
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%rcx
mov    -0x28(%rbp),%r8
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%r8
mov    -0x28(%rbp),%r9
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%r9
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    125f <main+0xdf>
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x70(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x64(%rbp),%edx
lea    -0x50(%rbp),%r9
lea    -0x70(%rbp),%rcx
lea    -0x74(%rbp),%r8
call   10a0 <ellipse_fit_with_gradients@plt>
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    136e <main+0x1ee>
movslq -0x14(%rbp),%rax
movsd  -0x50(%rbp,%rax,8),%xmm0
lea    0xd2f(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    133e <main+0x1be>
lea    0xca2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x8(%rsi),%rdi
lea    0xe57(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11c2 <main+0x32>
lea    0xe45(%rip),%rdi
call   1090 <error@plt>
lea    0xe4c(%rip),%rsi
mov    %rsp,%rdx
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <fscanf@plt>
mov    (%rsp),%edi
test   %edi,%edi
jg     11ec <main+0x5c>
lea    0xe35(%rip),%rdi
call   1090 <error@plt>
mov    (%rsp),%edi
shl    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1208 <main+0x78>
lea    0xe2a(%rip),%rdi
call   1090 <error@plt>
mov    (%rsp),%edi
shl    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1227 <main+0x97>
lea    0xe25(%rip),%rdi
call   1090 <error@plt>
mov    %r13,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    126f <main+0xdf>
lea    0xe28(%rip),%r12
mov    %r14,%rbx
mov    %r15,%rbp
lea    0x8(%rbx),%rcx
lea    0x8(%rbp),%r9
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %rbp,%r8
xor    %eax,%eax
call   1060 <fscanf@plt>
mov    %r13,%rdi
call   1070 <feof@plt>
add    $0x10,%rbp
add    $0x10,%rbx
test   %eax,%eax
je     1240 <main+0xb0>
mov    $0x8,%edi
call   1080 <malloc@plt>
mov    %rax,0x8(%rsp)
movl   $0x1,0x4(%rsp)
mov    (%rsp),%edx
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %r14,%rdi
mov    %r15,%rsi
call   10b0 <ellipse_fit_with_gradients@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0xdbf(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movsd  0x10(%rsp),%xmm0
lea    0xdc0(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movsd  0x18(%rsp),%xmm0
mov    %rbx,%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movsd  0x20(%rsp),%xmm0
mov    %rbx,%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movsd  0x28(%rsp),%xmm0
mov    %rbx,%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movsd  0x30(%rsp),%xmm0
mov    %rbx,%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"invalid input file\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [20 x i8] c"invalid points size\00"
@.str.4 = [26 x i8] c"not enough memory for pts\00"
@.str.5 = [27 x i8] c"not enough memory for grad\00"
@.str.6 = [17 x i8] c"%lf %lf %lf %lf\0A\00"
@.str.7 = [21 x i8] c"Ellipse parameters: \00"
@.str.8 = [5 x i8] c"%lf \00"
@.str.9 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca [5 x double]
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca double*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %11
%20 = load i32*, i32** %11
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %2
%25 = load i32*, i32** %11
%26 = call i32 (i32*, i8*, ...) @fscanf(i32* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %12)
%27 = load i32, i32* %12
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = load i32, i32* %12
%33 = mul nsw i32 2, %32
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 8
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to double*
store double* %38, double** %7
%39 = load double*, double** %7
%40 = icmp eq double* %39, null
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %31
%44 = load i32, i32* %12
%45 = mul nsw i32 2, %44
%46 = sext i32 %45 to i64
%47 = mul i64 %46, 8
%48 = trunc i64 %47 to i32
%49 = call i64 @malloc(i32 %48)
%50 = inttoptr i64 %49 to double*
store double* %50, double** %8
%51 = load double*, double** %8
%52 = icmp eq double* %51, null
br i1 %52, label %53, label %55
53: ; preds = %43
%54 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %43
store i32 0, i32* %6
br label %56
56: ; preds = %90, %55
%57 = load i32, i32* %6
%58 = load i32, i32* %12
%59 = icmp slt i32 %57, %58
%60 = zext i1 %59 to i32
%61 = load i32*, i32** %11
%62 = call i32 @feof(i32* %61)
%63 = icmp ne i32 %62, 0
%64 = xor i1 %63, true
br i1 %64, label %65, label %93
65: ; preds = %56
%66 = load i32*, i32** %11
%67 = load double*, double** %7
%68 = load i32, i32* %6
%69 = mul nsw i32 2, %68
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds double, double* %67, i64 %70
%72 = load double*, double** %7
%73 = load i32, i32* %6
%74 = mul nsw i32 2, %73
%75 = add nsw i32 %74, 1
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds double, double* %72, i64 %76
%78 = load double*, double** %8
%79 = load i32, i32* %6
%80 = mul nsw i32 2, %79
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds double, double* %78, i64 %81
%83 = load double*, double** %8
%84 = load i32, i32* %6
%85 = mul nsw i32 2, %84
%86 = add nsw i32 %85, 1
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds double, double* %83, i64 %87
%89 = call i32 (i32*, i8*, ...) @fscanf(i32* %66, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), double* %71, double* %77, double* %82, double* %88)
br label %90
90: ; preds = %65
%91 = load i32, i32* %6
%92 = add nsw i32 %91, 1
store i32 %92, i32* %6
br label %56
93: ; preds = %56
%94 = call i64 @malloc(i32 8)
%95 = inttoptr i64 %94 to double*
store double* %95, double** %13
store i32 1, i32* %14
%96 = load double*, double** %7
%97 = load double*, double** %8
%98 = load i32, i32* %12
%99 = getelementptr inbounds [5 x double], [5 x double]* %9, i64 0, i64 0
%100 = call i32 @ellipse_fit_with_gradients(double* %96, double* %97, i32 %98, double** %13, i32* %14, double* %99)
%101 = load double*, double** %13
%102 = call i32 @free(double* %101)
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %6
br label %104
104: ; preds = %113, %93
%105 = load i32, i32* %6
%106 = icmp slt i32 %105, 5
br i1 %106, label %107, label %116
107: ; preds = %104
%108 = load i32, i32* %6
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds [5 x double], [5 x double]* %9, i64 0, i64 %109
%111 = load double, double* %110
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %111)
br label %113
113: ; preds = %107
%114 = load i32, i32* %6
%115 = add nsw i32 %114, 1
store i32 %115, i32* %6
br label %104
116: ; preds = %104
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"invalid input file\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [20 x i8] c"invalid points size\00"
@.str.4 = [26 x i8] c"not enough memory for pts\00"
@.str.5 = [27 x i8] c"not enough memory for grad\00"
@.str.6 = [17 x i8] c"%lf %lf %lf %lf\0A\00"
@.str.7 = [21 x i8] c"Ellipse parameters: \00"
@.str.8 = [5 x i8] c"%lf \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5 x double]
%4 = alloca i32
%5 = alloca double*
%6 = alloca i32
%7 = bitcast [5 x double]* %3 to i8*
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = bitcast i32* %4 to i8*
%16 = call i32 (i32*, i8*, ...) @fscanf(i32* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %4
br label %22
22: ; preds = %19, %14
%23 = phi i32 [ %21, %19 ], [ %17, %14 ]
%24 = shl i32 %23, 4
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to double*
%27 = icmp eq i64 %25, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %28, %22
%31 = load i32, i32* %4
%32 = shl i32 %31, 4
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to double*
%35 = icmp eq i64 %33, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = call i32 @feof(i32* %10)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %38, %41
%42 = phi i64 [ %50, %41 ], [ 0, %38 ]
%43 = shl nuw nsw i64 %42, 1
%44 = getelementptr inbounds double, double* %26, i64 %43
%45 = or i64 %43, 1
%46 = getelementptr inbounds double, double* %26, i64 %45
%47 = getelementptr inbounds double, double* %34, i64 %43
%48 = getelementptr inbounds double, double* %34, i64 %45
%49 = call i32 (i32*, i8*, ...) @fscanf(i32* %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), double* %44, double* %46, double* %47, double* %48)
%50 = add nuw i64 %42, 1
%51 = call i32 @feof(i32* %10)
%52 = icmp eq i32 %51, 0
br i1 %52, label %41, label %53
53: ; preds = %41, %38
%54 = bitcast double** %5 to i8*
%55 = call i64 @malloc(i32 8)
%56 = inttoptr i64 %55 to double*
store double* %56, double** %5
%57 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%58 = load i32, i32* %4
%59 = getelementptr inbounds [5 x double], [5 x double]* %3, i64 0, i64 0
%60 = call i32 @ellipse_fit_with_gradients(double* %26, double* %34, i32 %58, double** %5, i32* %6, double* %59)
%61 = load double*, double** %5
%62 = call i32 @free(double* %61)
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%64 = load double, double* %59
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %64)
%66 = getelementptr inbounds [5 x double], [5 x double]* %3, i64 0, i64 1
%67 = load double, double* %66
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %67)
%69 = getelementptr inbounds [5 x double], [5 x double]* %3, i64 0, i64 2
%70 = load double, double* %69
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %70)
%72 = getelementptr inbounds [5 x double], [5 x double]* %3, i64 0, i64 3
%73 = load double, double* %72
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %73)
%75 = getelementptr inbounds [5 x double], [5 x double]* %3, i64 0, i64 4
%76 = load double, double* %75
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %76)
%78 = call i32 @putchar(i32 10)
ret i32 0
}