#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*/ _SocketError();
int /*<<< orphan*/ free(char *);
char *realloc(char *, size_t);
char *__expandBuffer(char *buffer, size_t new_size, size_t *buff_size) {
  char *e;
  if (new_size >= *buff_size) {
    *buff_size = *buff_size * 2;
    e = realloc(buffer, *buff_size);
    if (e == NULL) {
      free(buffer);
      _SocketError();
      return NULL;
    } else {
      return e;
    }
  } else {
    return buffer;
  }
}


x86 assembly (clang, -O0):
<__expandBuffer>:
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    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jb     11b0 <__expandBuffer+0x80>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a3 <__expandBuffer+0x73>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1040 <_SocketError@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <__expandBuffer+0x88>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <__expandBuffer+0x88>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__expandBuffer>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r14
mov    (%rdx),%rsi
cmp    %rax,%rsi
jbe    1147 <__expandBuffer+0x17>
mov    %r14,%rbx
jmp    116e <__expandBuffer+0x3e>
add    %rsi,%rsi
mov    %rsi,(%rdx)
mov    %r14,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116e <__expandBuffer+0x3e>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <_SocketError@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @__expandBuffer(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%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** %7
%11 = load i64, i64* %10
%12 = icmp uge i64 %9, %11
br i1 %12, label %13, label %30
13: ; preds = %3
%14 = load i64*, i64** %7
%15 = load i64, i64* %14
%16 = mul i64 %15, 2
%17 = load i64*, i64** %7
store i64 %16, i64* %17
%18 = load i8*, i8** %5
%19 = load i64*, i64** %7
%20 = load i64, i64* %19
%21 = call i8* @realloc(i8* %18, i64 %20)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %13
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
%27 = call i32 (...) @_SocketError()
store i8* null, i8** %4
br label %32
28: ; preds = %13
%29 = load i8*, i8** %8
store i8* %29, i8** %4
br label %32
30: ; preds = %3
%31 = load i8*, i8** %5
store i8* %31, i8** %4
br label %32
32: ; preds = %30, %28, %24
%33 = load i8*, i8** %4
ret i8* %33
}

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