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


x86 assembly (clang, -O0):
<read_arg_int>:
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_int+0x59>
lea    0xe7e(%rip),%rdi
mov    $0x40,%esi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1214 <read_arg_int+0xd4>
movl   $0x10,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1203 <read_arg_int+0xc3>
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11e6 <read_arg_int+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)
mov    -0x28(%rbp),%edx
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11a0 <read_arg_int+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_int>:
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     11ec <read_arg_int+0xac>
mov    %rax,%r15
lea    0xc(%rsp),%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <fscanf@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
jne    1207 <read_arg_int+0xc7>
mov    %rbp,0x10(%rsp)
mov    $0x10,%ebp
xor    %r14d,%r14d
lea    0xc(%rsp),%rbx
jmp    11bf <read_arg_int+0x7f>
nopl   (%rax)
mov    0xc(%rsp),%eax
mov    %eax,(%r15,%r14,4)
add    $0x1,%r14
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1202 <read_arg_int+0xc2>
mov    %ebp,%eax
cmp    %rax,%r14
jne    11a0 <read_arg_int+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_int+0x60>
lea    0xe0d(%rip),%rdi
mov    $0x40,%esi
call   1030 <printf@plt>
xor    %r15d,%r15d
jmp    120b <read_arg_int+0xcb>
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 i32* @read_arg_int(i32* %0, i8* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
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 i32* @malloc(i32 64)
store i32* %12, i32** %11
%13 = load i32*, i32** %11
%14 = icmp eq i32* %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 i32* null, i32** %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, i32* %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 i32*, i32** %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 i32* @realloc(i32* %30, i32 %34)
store i32* %35, i32** %11
br label %36
36: ; preds = %27, %23
%37 = load i32, i32* %9
%38 = load i32*, i32** %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 i32, i32* %38, i64 %41
store i32 %37, i32* %42
br label %18
43: ; preds = %18
%44 = load i32, i32* %8
%45 = load i32*, i32** %7
store i32 %44, i32* %45
%46 = load i32*, i32** %11
store i32* %46, i32** %4
br label %47
47: ; preds = %43, %15
%48 = load i32*, i32** %4
ret i32* %48
}

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