#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(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *resize_buffer(char *buffer, size_t oldsize, size_t newsize) {
  char *new = NULL;
  if (newsize < oldsize) {
    return NULL;
  }
  new = (char *)malloc(newsize);
  if (!new) {
    return NULL;
  }
  memcpy(new, buffer, oldsize);
  free(buffer);
  return new;
}


x86 assembly (clang, -O0):
<resize_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1167 <resize_buffer+0x37>
movq   $0x0,-0x8(%rbp)
jmp    11ae <resize_buffer+0x7e>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118c <resize_buffer+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    11ae <resize_buffer+0x7e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resize_buffer>:
push   %r15
push   %r14
push   %rbx
cmp    %rsi,%rdx
jb     1168 <resize_buffer+0x38>
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1168 <resize_buffer+0x38>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    116a <resize_buffer+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @resize_buffer(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
store i8* null, i8** %8
%9 = load i64, i64* %7
%10 = load i64, i64* %6
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* null, i8** %4
br label %28
13: ; preds = %3
%14 = load i64, i64* %7
%15 = call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %13
store i8* null, i8** %4
br label %28
20: ; preds = %13
%21 = load i8*, i8** %8
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = call i32 @memcpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %8
store i8* %27, i8** %4
br label %28
28: ; preds = %20, %19, %12
%29 = load i8*, i8** %4
ret i8* %29
}

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