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


x86 assembly (clang, -O0):
<read_arg_long>:
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_long+0x59>
lea    0xe7e(%rip),%rdi
mov    $0x80,%esi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1216 <read_arg_long+0xd6>
movl   $0x10,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1205 <read_arg_long+0xc5>
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11e6 <read_arg_long+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)
mov    -0x30(%rbp),%rdx
mov    -0x40(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    11a0 <read_arg_long+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_long>:
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     11ed <read_arg_long+0xad>
mov    %rax,%r15
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <fscanf@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
jne    1208 <read_arg_long+0xc8>
mov    %rbp,0x10(%rsp)
mov    $0x10,%ebp
xor    %r14d,%r14d
lea    0x8(%rsp),%rbx
jmp    11c0 <read_arg_long+0x80>
nopl   (%rax)
mov    0x8(%rsp),%rax
mov    %rax,(%r15,%r14,8)
add    $0x1,%r14
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1203 <read_arg_long+0xc3>
mov    %ebp,%eax
cmp    %rax,%r14
jne    11a0 <read_arg_long+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_long+0x60>
lea    0xe0c(%rip),%rdi
mov    $0x80,%esi
call   1030 <printf@plt>
xor    %r15d,%r15d
jmp    120c <read_arg_long+0xcc>
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 i64* @read_arg_long(i32* %0, i8* %1, i32* %2) {
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64*
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 i64* @malloc(i32 128)
store i64* %12, i64** %11
%13 = load i64*, i64** %11
%14 = icmp eq i64* %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 i64* null, i64** %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, i64* %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 i64*, i64** %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 i64* @realloc(i64* %30, i32 %34)
store i64* %35, i64** %11
br label %36
36: ; preds = %27, %23
%37 = load i64, i64* %9
%38 = load i64*, i64** %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 i64, i64* %38, i64 %41
store i64 %37, i64* %42
br label %18
43: ; preds = %18
%44 = load i32, i32* %8
%45 = load i32*, i32** %7
store i32 %44, i32* %45
%46 = load i64*, i64** %11
store i64* %46, i64** %4
br label %47
47: ; preds = %43, %15
%48 = load i64*, i64** %4
ret i64* %48
}

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