#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*/ disable_interrupts();
int /*<<< orphan*/ enable_interrupts();
int /*<<< orphan*/ errno;
void *malloc(int const);
void *realloc(char *, int const);
int /*<<< orphan*/ set_error_msg(char *);
int /*<<< orphan*/ show_strerror(int /*<<< orphan*/, int /*<<< orphan*/);
char resize_buffer(char **buf, int *size, int min_size) {
  if (*size < min_size) {
    const int new_size = (min_size < 512 ? 512 : (min_size / 512) * 1024);
    void *new_buf = 0;
    disable_interrupts();
    if (*buf)
      new_buf = realloc(*buf, new_size);
    else
      new_buf = malloc(new_size);
    if (!new_buf) {
      show_strerror(0, errno);
      set_error_msg("Memory exhausted");
      enable_interrupts();
      return 0;
    }
    *size = new_size;
    *buf = (char *)new_buf;
    enable_interrupts();
  }
  return 1;
}


x86 assembly (clang, -O0):
<resize_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
jge    1246 <resize_buffer+0xe6>
cmpl   $0x200,-0x1c(%rbp)
jge    119c <resize_buffer+0x3c>
mov    $0x200,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ad <resize_buffer+0x4d>
mov    -0x1c(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
shl    $0xa,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1060 <disable_interrupts@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11e8 <resize_buffer+0x88>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    11f4 <resize_buffer+0x94>
mov    -0x20(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122b <resize_buffer+0xcb>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1080 <show_strerror@plt>
lea    0xdea(%rip),%rdi
call   1030 <set_error_msg@plt>
mov    $0x0,%al
call   1070 <enable_interrupts@plt>
movb   $0x0,-0x1(%rbp)
jmp    124a <resize_buffer+0xea>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x0,%al
call   1070 <enable_interrupts@plt>
movb   $0x1,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resize_buffer>:
push   %rbp
push   %r14
push   %rbx
mov    $0x1,%bpl
cmp    %edx,(%rsi)
jge    11dd <resize_buffer+0x7d>
mov    %rsi,%rbx
mov    %rdi,%r14
lea    (%rdx,%rdx,1),%eax
and    $0xfffffc00,%eax
cmp    $0x200,%edx
mov    $0x200,%ebp
cmovge %eax,%ebp
xor    %eax,%eax
call   1060 <disable_interrupts@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     11ac <resize_buffer+0x4c>
mov    %ebp,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11b8 <resize_buffer+0x58>
mov    %ebp,(%rbx)
mov    %rax,(%r14)
mov    $0x1,%bpl
jmp    11d6 <resize_buffer+0x76>
mov    %ebp,%edi
call   1040 <malloc@plt>
test   %rax,%rax
jne    11a2 <resize_buffer+0x42>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edi,%edi
call   1080 <show_strerror@plt>
lea    0xe2f(%rip),%rdi
call   1030 <set_error_msg@plt>
xor    %eax,%eax
call   1070 <enable_interrupts@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [17 x i8] c"Memory exhausted\00"
define signext i8 @resize_buffer(i8** %0, i32* %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i32*, i32** %6
%11 = load i32, i32* %10
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %50
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 512
br i1 %16, label %17, label %18
17: ; preds = %14
br label %22
18: ; preds = %14
%19 = load i32, i32* %7
%20 = sdiv i32 %19, 512
%21 = mul nsw i32 %20, 1024
br label %22
22: ; preds = %18, %17
%23 = phi i32 [ 512, %17 ], [ %21, %18 ]
store i32 %23, i32* %8
store i8* null, i8** %9
%24 = call i32 (...) @disable_interrupts()
%25 = load i8**, i8*** %5
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i8**, i8*** %5
%30 = load i8*, i8** %29
%31 = load i32, i32* %8
%32 = call i8* @realloc(i8* %30, i32 %31)
store i8* %32, i8** %9
br label %36
33: ; preds = %22
%34 = load i32, i32* %8
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %9
br label %36
36: ; preds = %33, %28
%37 = load i8*, i8** %9
%38 = icmp ne i8* %37, null
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = load i32, i32* @errno
%41 = call i32 @show_strerror(i32 0, i32 %40)
%42 = call i32 @set_error_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%43 = call i32 (...) @enable_interrupts()
store i8 0, i8* %4
br label %51
44: ; preds = %36
%45 = load i32, i32* %8
%46 = load i32*, i32** %6
store i32 %45, i32* %46
%47 = load i8*, i8** %9
%48 = load i8**, i8*** %5
store i8* %47, i8** %48
%49 = call i32 (...) @enable_interrupts()
br label %50
50: ; preds = %44, %3
store i8 1, i8* %4
br label %51
51: ; preds = %50, %39
%52 = load i8, i8* %4
ret i8 %52
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [17 x i8] c"Memory exhausted\00"
define signext i8 @resize_buffer(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 %29
6: ; preds = %3
%7 = icmp slt i32 %2, 512
%8 = shl nuw nsw i32 %2, 1
%9 = and i32 %8, -1024
%10 = select i1 %7, i32 512, i32 %9
%11 = tail call i32 (...) @disable_interrupts()
%12 = load i8*, i8** %0
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = tail call i8* @realloc(i8* %12, i32 %10)
br label %18
16: ; preds = %6
%17 = tail call i8* @malloc(i32 %10)
br label %18
18: ; preds = %16, %14
%19 = phi i8* [ %15, %14 ], [ %17, %16 ]
%20 = icmp eq i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %18
store i32 %10, i32* %1
store i8* %19, i8** %0
br label %26
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = tail call i32 @show_strerror(i32 0, i32 %23)
%25 = tail call i32 @set_error_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %22, %21
%27 = phi i8 [ 1, %21 ], [ 0, %22 ]
%28 = tail call i32 (...) @enable_interrupts()
br label %29
29: ; preds = %26, %3
%30 = phi i8 [ 1, %3 ], [ %27, %26 ]
ret i8 %30
}