#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 */
void *realloc(void *, int);
void *resize_block_if_needed(void *in_ptr, unsigned int end_pos,
                             unsigned int *capacity) {
  unsigned int new_capacity = end_pos * 2;
  void *ptr_resize = NULL;
  if (end_pos >= *capacity) {
    ptr_resize = realloc(in_ptr, sizeof(char) * new_capacity);
    if (ptr_resize != NULL) {
      *capacity = new_capacity;
    }
  } else {
    ptr_resize = in_ptr;
  }
  return ptr_resize;
}


x86 assembly (clang, -O0):
<resize_block_if_needed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jb     1172 <resize_block_if_needed+0x62>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     116d <resize_block_if_needed+0x5d>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    117a <resize_block_if_needed+0x6a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resize_block_if_needed>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rax
cmp    %esi,(%rdx)
ja     1136 <resize_block_if_needed+0x26>
mov    %rdx,%rbx
mov    %esi,%ebp
add    %esi,%ebp
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <realloc@plt>
test   %rax,%rax
je     1134 <resize_block_if_needed+0x24>
mov    %ebp,(%rbx)
jmp    1136 <resize_block_if_needed+0x26>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @resize_block_if_needed(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i32, i32* %5
%10 = mul i32 %9, 2
store i32 %10, i32* %7
store i8* null, i8** %8
%11 = load i32, i32* %5
%12 = load i32*, i32** %6
%13 = load i32, i32* %12
%14 = icmp uge i32 %11, %13
br i1 %14, label %15, label %28
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i32, i32* %7
%18 = zext i32 %17 to i64
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @realloc(i8* %16, i32 %20)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* %7
%26 = load i32*, i32** %6
store i32 %25, i32* %26
br label %27
27: ; preds = %24, %15
br label %30
28: ; preds = %3
%29 = load i8*, i8** %4
store i8* %29, i8** %8
br label %30
30: ; preds = %28, %27
%31 = load i8*, i8** %8
ret i8* %31
}

LLVM IR (-O2):
define i8* @resize_block_if_needed(i8* %0, i32 %1, i32* nocapture %2) {
%4 = shl i32 %1, 1
%5 = load i32, i32* %2
%6 = icmp ugt i32 %5, %1
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = tail call i8* @realloc(i8* %0, i32 %4)
%9 = icmp eq i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %7
store i32 %4, i32* %2
br label %11
11: ; preds = %3, %7, %10
%12 = phi i8* [ %8, %10 ], [ null, %7 ], [ %0, %3 ]
ret i8* %12
}