#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 */

/* Variables and functions */
int /*<<< orphan*/ free(void *);
void *realloc(void *, size_t);
void *mgrow(void *ptr, size_t *size) {
  if (*size <= 16)
    *size += 4;
  else if (*size < 4084)
    *size *= 2;
  else
    *size += 2048;
  void *new_ptr = realloc(ptr, *size);
  if (!new_ptr) {
    free(ptr);
    *size = 0;
  }
  return new_ptr;
}


x86 assembly (clang, -O0):
<mgrow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x10,(%rax)
ja     1151 <mgrow+0x31>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x4,%rcx
mov    %rcx,(%rax)
jmp    118b <mgrow+0x6b>
mov    -0x10(%rbp),%rax
cmpq   $0xff4,(%rax)
jae    1175 <mgrow+0x55>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
jmp    1186 <mgrow+0x66>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x800,%rcx
mov    %rcx,(%rax)
jmp    118b <mgrow+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11be <mgrow+0x9e>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgrow>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%rsi
cmp    $0x10,%rsi
ja     113a <mgrow+0x1a>
add    $0x4,%rsi
jmp    114f <mgrow+0x2f>
cmp    $0xff3,%rsi
ja     1148 <mgrow+0x28>
add    %rsi,%rsi
jmp    114f <mgrow+0x2f>
add    $0x800,%rsi
mov    %rsi,(%r15)
mov    %r14,%rdi
call   1040 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1171 <mgrow+0x51>
mov    %r14,%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mgrow(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%6 = load i64*, i64** %4
%7 = load i64, i64* %6
%8 = icmp ule i64 %7, 16
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i64*, i64** %4
%11 = load i64, i64* %10
%12 = add i64 %11, 4
store i64 %12, i64* %10
br label %26
13: ; preds = %2
%14 = load i64*, i64** %4
%15 = load i64, i64* %14
%16 = icmp ult i64 %15, 4084
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i64*, i64** %4
%19 = load i64, i64* %18
%20 = mul i64 %19, 2
store i64 %20, i64* %18
br label %25
21: ; preds = %13
%22 = load i64*, i64** %4
%23 = load i64, i64* %22
%24 = add i64 %23, 2048
store i64 %24, i64* %22
br label %25
25: ; preds = %21, %17
br label %26
26: ; preds = %25, %9
%27 = load i8*, i8** %3
%28 = load i64*, i64** %4
%29 = load i64, i64* %28
%30 = call i8* @realloc(i8* %27, i64 %29)
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = icmp ne i8* %31, null
br i1 %32, label %37, label %33
33: ; preds = %26
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
%36 = load i64*, i64** %4
store i64 0, i64* %36
br label %37
37: ; preds = %33, %26
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
define noalias i8* @mgrow(i8* nocapture %0, i64* nocapture %1) {
%3 = load i64, i64* %1
%4 = icmp ult i64 %3, 17
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = add nuw nsw i64 %3, 4
br label %13
7: ; preds = %2
%8 = icmp ult i64 %3, 4084
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = shl nuw nsw i64 %3, 1
br label %13
11: ; preds = %7
%12 = add i64 %3, 2048
br label %13
13: ; preds = %9, %11, %5
%14 = phi i64 [ %10, %9 ], [ %12, %11 ], [ %6, %5 ]
store i64 %14, i64* %1
%15 = tail call i8* @realloc(i8* %0, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @free(i8* %0)
store i64 0, i64* %1
br label %19
19: ; preds = %17, %13
ret i8* %15
}