#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(char *);
scalar_t__ malloc(int);
int check_buffer_size(char **buf, int *buf_size_bytes,
                      int requested_size_bytes) {
  int ret = 0x0;
  if (*buf_size_bytes >= requested_size_bytes) {
    return 0x0;
  }
  free(*buf);
  *buf = (char *)malloc(requested_size_bytes);
  if (*buf != NULL) {
    *buf_size_bytes = requested_size_bytes;
  } else {
    *buf_size_bytes = 0;
    ret = 0x2;
  }
  return ret;
}


x86 assembly (clang, -O0):
<check_buffer_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
jl     1155 <check_buffer_size+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <check_buffer_size+0x86>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     118f <check_buffer_size+0x6f>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11a0 <check_buffer_size+0x80>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x2,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_buffer_size>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
cmp    %edx,(%rsi)
jge    1159 <check_buffer_size+0x39>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    %rax,(%rbx)
xor    %edx,%edx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
cmovne %ebp,%edx
add    %eax,%eax
mov    %edx,(%r14)
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @check_buffer_size(i8** nocapture %0, i32* nocapture %1, i32 %2) {
%4 = load i32, i32* %1
%5 = icmp slt i32 %4, %2
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = load i8*, i8** %0
%8 = tail call i32 @free(i8* %7)
%9 = tail call i64 @malloc(i32 %2)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %0
%11 = icmp eq i64 %9, 0
%12 = select i1 %11, i32 0, i32 %2
%13 = select i1 %11, i32 2, i32 0
store i32 %12, i32* %1
br label %14
14: ; preds = %3, %6
%15 = phi i32 [ %13, %6 ], [ 0, %3 ]
ret i32 %15
}