#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 fscanf(int /*<<< orphan*/ *, char *, double *);
double *malloc(int);
int /*<<< orphan*/ printf(char *, int);
double *realloc(double *, int);
double *read_arg_double(FILE *fp, char *fmt_str, int *ptr) {
  int count = 0;
  double arg;
  int array_size = 0;
  double *array = malloc(sizeof(double) * 16);
  if (array == NULL) {
    printf("Failed to allocate memory for %zd bytes.\n", 16 * sizeof(double));
    return NULL;
  }
  array_size = 16;
  while (fscanf(fp, fmt_str, &arg) == 1) {
    if (array_size == count) {
      array_size *= 2;
      array = realloc(array, array_size * sizeof(double));
    }
    array[count++] = arg;
  }
  *ptr = count;
  return array;
}


x86 assembly (clang, -O0):
<read_arg_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1199 <read_arg_double+0x59>
lea    0xe7e(%rip),%rdi
mov    $0x80,%esi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1218 <read_arg_double+0xd8>
movl   $0x10,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1207 <read_arg_double+0xc7>
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11e6 <read_arg_double+0xa6>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
mov    -0x40(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    11a0 <read_arg_double+0x60>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_arg_double>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r12
mov    $0x80,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11f0 <read_arg_double+0xb0>
mov    %rax,%r15
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <fscanf@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
jne    120b <read_arg_double+0xcb>
mov    %rbp,0x10(%rsp)
mov    $0x10,%ebp
xor    %r14d,%r14d
lea    0x8(%rsp),%rbx
jmp    11c3 <read_arg_double+0x83>
nopl   (%rax)
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r15,%r14,8)
add    $0x1,%r14
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1206 <read_arg_double+0xc6>
mov    %ebp,%eax
cmp    %rax,%r14
jne    11a0 <read_arg_double+0x60>
mov    %r15,%rdi
mov    %r12,%r15
mov    %r13,%r12
lea    0x0(%rbp,%rbp,1),%r13d
shl    $0x4,%ebp
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %r13d,%ebp
mov    %r12,%r13
mov    %r15,%r12
mov    %rax,%r15
jmp    11a0 <read_arg_double+0x60>
lea    0xe09(%rip),%rdi
mov    $0x80,%esi
call   1030 <printf@plt>
xor    %r15d,%r15d
jmp    120f <read_arg_double+0xcf>
mov    0x10(%rsp),%rbp
mov    %r14d,0x0(%rbp)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Failed to allocate memory for %zd bytes.\0A\00"
define double* @read_arg_double(i32* %0, i8* %1, i32* %2) {
%4 = alloca double*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca double*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %8
store i32 0, i32* %10
%12 = call double* @malloc(i32 128)
store double* %12, double** %11
%13 = load double*, double** %11
%14 = icmp eq double* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 128)
store double* null, double** %4
br label %47
17: ; preds = %3
store i32 16, i32* %10
br label %18
18: ; preds = %36, %17
%19 = load i32*, i32** %5
%20 = load i8*, i8** %6
%21 = call i32 @fscanf(i32* %19, i8* %20, double* %9)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %43
23: ; preds = %18
%24 = load i32, i32* %10
%25 = load i32, i32* %8
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* %10
%29 = mul nsw i32 %28, 2
store i32 %29, i32* %10
%30 = load double*, double** %11
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 8
%34 = trunc i64 %33 to i32
%35 = call double* @realloc(double* %30, i32 %34)
store double* %35, double** %11
br label %36
36: ; preds = %27, %23
%37 = load double, double* %9
%38 = load double*, double** %11
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds double, double* %38, i64 %41
store double %37, double* %42
br label %18
43: ; preds = %18
%44 = load i32, i32* %8
%45 = load i32*, i32** %7
store i32 %44, i32* %45
%46 = load double*, double** %11
store double* %46, double** %4
br label %47
47: ; preds = %43, %15
%48 = load double*, double** %4
ret double* %48
}

LLVM IR (-O2):
@.str = [42 x i8] c"Failed to allocate memory for %zd bytes.\0A\00"
define double* @read_arg_double(i32* nocapture %0, i8* nocapture %1, i32* nocapture writeonly %2) {
%4 = alloca double
%5 = bitcast double* %4 to i8*
%6 = tail call double* @malloc(i32 128)
%7 = icmp eq double* %6, null
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = call i32 @fscanf(i32* %0, i8* %1, double* %4)
%10 = icmp eq i32 %9, 1
br i1 %10, label %13, label %33
11: ; preds = %3
%12 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 128)
br label %36
13: ; preds = %8, %23
%14 = phi i64 [ %27, %23 ], [ 0, %8 ]
%15 = phi double* [ %25, %23 ], [ %6, %8 ]
%16 = phi i32 [ %24, %23 ], [ 16, %8 ]
%17 = zext i32 %16 to i64
%18 = icmp eq i64 %14, %17
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = shl nsw i32 %16, 1
%21 = shl i32 %16, 4
%22 = call double* @realloc(double* %15, i32 %21)
br label %23
23: ; preds = %19, %13
%24 = phi i32 [ %20, %19 ], [ %16, %13 ]
%25 = phi double* [ %22, %19 ], [ %15, %13 ]
%26 = load double, double* %4
%27 = add nuw i64 %14, 1
%28 = getelementptr inbounds double, double* %25, i64 %14
store double %26, double* %28
%29 = call i32 @fscanf(i32* %0, i8* %1, double* %4)
%30 = icmp eq i32 %29, 1
br i1 %30, label %13, label %31
31: ; preds = %23
%32 = trunc i64 %27 to i32
br label %33
33: ; preds = %31, %8
%34 = phi i32 [ 0, %8 ], [ %32, %31 ]
%35 = phi double* [ %6, %8 ], [ %25, %31 ]
store i32 %34, i32* %2
br label %36
36: ; preds = %33, %11
%37 = phi double* [ null, %11 ], [ %35, %33 ]
ret double* %37
}