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


x86 assembly (clang, -O0):
<read_arg_float>:
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,-0x2c(%rbp)
mov    $0x40,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1199 <read_arg_float+0x59>
lea    0xe7e(%rip),%rdi
mov    $0x40,%esi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1218 <read_arg_float+0xd8>
movl   $0x10,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1207 <read_arg_float+0xc7>
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11e6 <read_arg_float+0xa6>
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
movss  -0x28(%rbp),%xmm0
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
movss  %xmm0,(%rax,%rcx,4)
jmp    11a0 <read_arg_float+0x60>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_arg_float>:
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    $0x40,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11f0 <read_arg_float+0xb0>
mov    %rax,%r15
lea    0xc(%rsp),%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <fscanf@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
jne    120b <read_arg_float+0xcb>
mov    %rbp,0x10(%rsp)
mov    $0x10,%ebp
xor    %r14d,%r14d
lea    0xc(%rsp),%rbx
jmp    11c3 <read_arg_float+0x83>
nopl   (%rax)
movss  0xc(%rsp),%xmm0
movss  %xmm0,(%r15,%r14,4)
add    $0x1,%r14
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1206 <read_arg_float+0xc6>
mov    %ebp,%eax
cmp    %rax,%r14
jne    11a0 <read_arg_float+0x60>
mov    %r15,%rdi
mov    %r12,%r15
mov    %r13,%r12
lea    0x0(%rbp,%rbp,1),%r13d
shl    $0x3,%ebp
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %r13d,%ebp
mov    %r12,%r13
mov    %r15,%r12
mov    %rax,%r15
jmp    11a0 <read_arg_float+0x60>
lea    0xe09(%rip),%rdi
mov    $0x40,%esi
call   1030 <printf@plt>
xor    %r15d,%r15d
jmp    120f <read_arg_float+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 float* @read_arg_float(i32* %0, i8* %1, i32* %2) {
%4 = alloca float*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca float
%10 = alloca i32
%11 = alloca float*
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 float* @malloc(i32 64)
store float* %12, float** %11
%13 = load float*, float** %11
%14 = icmp eq float* %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 64)
store float* null, float** %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, float* %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 float*, float** %11
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 4
%34 = trunc i64 %33 to i32
%35 = call float* @realloc(float* %30, i32 %34)
store float* %35, float** %11
br label %36
36: ; preds = %27, %23
%37 = load float, float* %9
%38 = load float*, float** %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 float, float* %38, i64 %41
store float %37, float* %42
br label %18
43: ; preds = %18
%44 = load i32, i32* %8
%45 = load i32*, i32** %7
store i32 %44, i32* %45
%46 = load float*, float** %11
store float* %46, float** %4
br label %47
47: ; preds = %43, %15
%48 = load float*, float** %4
ret float* %48
}

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