#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ assert(char **);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
size_t oml_malloc_usable_size(char *);
char *oml_realloc(char *, size_t);
ssize_t resize_buffer(char **str, size_t new_sz) {
  assert(str);
  ssize_t str_sz = -1;
  if (NULL == *str || oml_malloc_usable_size(*str) < new_sz) {
    char *new = oml_realloc(*str, new_sz);
    if (new) {
      *str = new;
      str_sz = new_sz;
      memset(*str, 0, str_sz);
    }
  } else {
    str_sz = oml_malloc_usable_size(*str);
    memset(*str, 0, str_sz);
  }
  return str_sz;
}


x86 assembly (clang, -O0):
<resize_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <assert@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
je     1185 <resize_buffer+0x45>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <oml_malloc_usable_size@plt>
cmp    -0x10(%rbp),%rax
jae    11cc <resize_buffer+0x8c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <oml_realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c7 <resize_buffer+0x87>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
jmp    11ec <resize_buffer+0xac>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <oml_malloc_usable_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resize_buffer>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <assert@plt>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     1171 <resize_buffer+0x31>
call   1060 <oml_malloc_usable_size@plt>
mov    (%rbx),%rdi
cmp    %r14,%rax
jb     1173 <resize_buffer+0x33>
call   1060 <oml_malloc_usable_size@plt>
mov    %rax,%r14
mov    (%rbx),%rdi
jmp    1186 <resize_buffer+0x46>
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <oml_realloc@plt>
test   %rax,%rax
je     1192 <resize_buffer+0x52>
mov    %rax,(%rbx)
mov    %rax,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1030 <memset@plt>
jmp    1198 <resize_buffer+0x58>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @resize_buffer(i8** %0, i64 %1) {
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i8** %0, i8*** %3
store i64 %1, i64* %4
%7 = load i8**, i8*** %3
%8 = call i32 @assert(i8** %7)
store i32 -1, i32* %5
%9 = load i8**, i8*** %3
%10 = load i8*, i8** %9
%11 = icmp eq i8* null, %10
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i8**, i8*** %3
%14 = load i8*, i8** %13
%15 = call i64 @oml_malloc_usable_size(i8* %14)
%16 = load i64, i64* %4
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %35
18: ; preds = %12, %2
%19 = load i8**, i8*** %3
%20 = load i8*, i8** %19
%21 = load i64, i64* %4
%22 = call i8* @oml_realloc(i8* %20, i64 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %34
25: ; preds = %18
%26 = load i8*, i8** %6
%27 = load i8**, i8*** %3
store i8* %26, i8** %27
%28 = load i64, i64* %4
%29 = trunc i64 %28 to i32
store i32 %29, i32* %5
%30 = load i8**, i8*** %3
%31 = load i8*, i8** %30
%32 = load i32, i32* %5
%33 = call i32 @memset(i8* %31, i32 0, i32 %32)
br label %34
34: ; preds = %25, %18
br label %44
35: ; preds = %12
%36 = load i8**, i8*** %3
%37 = load i8*, i8** %36
%38 = call i64 @oml_malloc_usable_size(i8* %37)
%39 = trunc i64 %38 to i32
store i32 %39, i32* %5
%40 = load i8**, i8*** %3
%41 = load i8*, i8** %40
%42 = load i32, i32* %5
%43 = call i32 @memset(i8* %41, i32 0, i32 %42)
br label %44
44: ; preds = %35, %34
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
define i32 @resize_buffer(i8** %0, i64 %1) {
%3 = tail call i32 @assert(i8** %0)
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = tail call i64 @oml_malloc_usable_size(i8* %4)
%8 = icmp ult i64 %7, %1
%9 = load i8*, i8** %0
br i1 %8, label %10, label %17
10: ; preds = %6, %2
%11 = phi i8* [ null, %2 ], [ %9, %6 ]
%12 = tail call i8* @oml_realloc(i8* %11, i64 %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %10
store i8* %12, i8** %0
%15 = trunc i64 %1 to i32
%16 = tail call i32 @memset(i8* %12, i32 0, i32 %15)
br label %22
17: ; preds = %6
%18 = tail call i64 @oml_malloc_usable_size(i8* %9)
%19 = trunc i64 %18 to i32
%20 = load i8*, i8** %0
%21 = tail call i32 @memset(i8* %20, i32 0, i32 %19)
br label %22
22: ; preds = %10, %14, %17
%23 = phi i32 [ %19, %17 ], [ %15, %14 ], [ -1, %10 ]
ret i32 %23
}