#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 */
struct mempool {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bzero(void *, unsigned int);
int /*<<< orphan*/ mp_end(struct mempool *, void *);
void *mp_grow(struct mempool *, unsigned int);
unsigned int mp_open_fast(struct mempool *, void *);
void *mp_realloc_zero(struct mempool *pool, void *ptr, unsigned size) {
  unsigned old_size = mp_open_fast(pool, ptr);
  ptr = mp_grow(pool, size);
  if (size > old_size)
    bzero(ptr + old_size, size - old_size);
  mp_end(pool, ptr + size);
  return ptr;
}


x86 assembly (clang, -O0):
<mp_realloc_zero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mp_open_fast@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mp_grow@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jbe    1194 <mp_realloc_zero+0x54>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
call   1050 <bzero@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    %rax,%rsi
call   1060 <mp_end@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_realloc_zero>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rdi,%r14
call   1030 <mp_open_fast@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <mp_grow@plt>
mov    %rax,%r15
cmp    %ebp,%ebx
jae    1172 <mp_realloc_zero+0x32>
mov    %ebx,%edi
add    %r15,%rdi
mov    %ebp,%esi
sub    %ebx,%esi
call   1050 <bzero@plt>
mov    %ebp,%eax
mov    %r15,%rsi
add    %rax,%rsi
mov    %r14,%rdi
call   1060 <mp_end@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mempool = type { i32 }
define i8* @mp_realloc_zero(%struct.mempool* %0, i8* %1, i32 %2) {
%4 = alloca %struct.mempool*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store %struct.mempool* %0, %struct.mempool** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load %struct.mempool*, %struct.mempool** %4
%9 = load i8*, i8** %5
%10 = call i32 @mp_open_fast(%struct.mempool* %8, i8* %9)
store i32 %10, i32* %7
%11 = load %struct.mempool*, %struct.mempool** %4
%12 = load i32, i32* %6
%13 = call i8* @mp_grow(%struct.mempool* %11, i32 %12)
store i8* %13, i8** %5
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = icmp ugt i32 %14, %15
br i1 %16, label %17, label %26
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i32, i32* %7
%20 = zext i32 %19 to i64
%21 = getelementptr i8, i8* %18, i64 %20
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = sub i32 %22, %23
%25 = call i32 @bzero(i8* %21, i32 %24)
br label %26
26: ; preds = %17, %3
%27 = load %struct.mempool*, %struct.mempool** %4
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = zext i32 %29 to i64
%31 = getelementptr i8, i8* %28, i64 %30
%32 = call i32 @mp_end(%struct.mempool* %27, i8* %31)
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
%struct.mempool = type { i32 }
define i8* @mp_realloc_zero(%struct.mempool* %0, i8* %1, i32 %2) {
%4 = tail call i32 @mp_open_fast(%struct.mempool* %0, i8* %1)
%5 = tail call i8* @mp_grow(%struct.mempool* %0, i32 %2)
%6 = icmp ult i32 %4, %2
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = zext i32 %4 to i64
%9 = getelementptr i8, i8* %5, i64 %8
%10 = sub i32 %2, %4
%11 = tail call i32 @bzero(i8* %9, i32 %10)
br label %12
12: ; preds = %7, %3
%13 = zext i32 %2 to i64
%14 = getelementptr i8, i8* %5, i64 %13
%15 = tail call i32 @mp_end(%struct.mempool* %0, i8* %14)
ret i8* %5
}