#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 *, size_t);
int high_water_alloc(void **buf, size_t *bufsize, size_t newsize) {
  if (*bufsize < newsize) {
    void *newbuf;
    newsize = (newsize + 256 - 1) & ~(256 - 1);
    newbuf = realloc(*buf, newsize);
    if (!newbuf)
      return 1;
    *buf = newbuf;
    *bufsize = newsize;
  }
  return 0;
}


x86 assembly (clang, -O0):
<high_water_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jae    118e <high_water_alloc+0x7e>
mov    -0x20(%rbp),%rax
add    $0x100,%rax
sub    $0x1,%rax
and    $0xffffffffffffff00,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <high_water_alloc+0x68>
movl   $0x1,-0x4(%rbp)
jmp    1195 <high_water_alloc+0x85>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<high_water_alloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
cmp    %rdx,(%rsi)
jae    1155 <high_water_alloc+0x45>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r15
add    $0xff,%rbx
and    $0xffffffffffffff00,%rbx
mov    (%rdi),%rdi
mov    %rbx,%rsi
call   1030 <realloc@plt>
test   %rax,%rax
je     114f <high_water_alloc+0x3f>
mov    %rax,(%r15)
mov    %rbx,0x0(%rbp)
jmp    1155 <high_water_alloc+0x45>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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