#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*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
scalar_t__ process_free(void *, int /*<<< orphan*/, size_t *);
void *realloc(void *buf_arg, size_t newsize) {
  size_t oldsize;
  void *buf;
  if (buf_arg == NULL)
    return (malloc(newsize));
  if (process_free(buf_arg, 0, &oldsize) == 0) {
    errno = EINVAL;
    return (NULL);
  }
  if (newsize == oldsize)
    return (buf_arg);
  buf = malloc(newsize);
  if (buf == NULL)
    return (NULL);
  (void)memcpy(buf, buf_arg, ((newsize) < (oldsize) ? (newsize) : (oldsize)));
  free(buf_arg);
  return (buf);
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    123a <realloc+0xfa>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
lea    -0x20(%rbp),%rdx
call   1040 <process_free@plt>
cmp    $0x0,%rax
jne    11a5 <realloc+0x65>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4a(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    123a <realloc+0xfa>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11c0 <realloc+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123a <realloc+0xfa>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <realloc+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    123a <realloc+0xfa>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1210 <realloc+0xd0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1218 <realloc+0xd8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     119d <realloc+0x5d>
mov    %rdi,%rbx
mov    %rsp,%rdx
xor    %esi,%esi
call   1040 <process_free@plt>
test   %rax,%rax
je     11b0 <realloc+0x70>
mov    (%rsp),%r15
cmp    %r14,%r15
je     11c4 <realloc+0x84>
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c2 <realloc+0x82>
mov    %rax,%r12
cmp    %r14,%r15
cmova  %r14,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rbx
jmp    11c4 <realloc+0x84>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <malloc@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i64, i64* %5
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %3
br label %47
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i64 @process_free(i8* %14, i32 0, i64* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
store i8* null, i8** %3
br label %47
19: ; preds = %13
%20 = load i64, i64* %5
%21 = load i64, i64* %6
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %47
25: ; preds = %19
%26 = load i64, i64* %5
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %25
store i8* null, i8** %3
br label %47
31: ; preds = %25
%32 = load i8*, i8** %7
%33 = load i8*, i8** %4
%34 = load i64, i64* %5
%35 = load i64, i64* %6
%36 = icmp ult i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = load i64, i64* %5
br label %41
39: ; preds = %31
%40 = load i64, i64* %6
br label %41
41: ; preds = %39, %37
%42 = phi i64 [ %38, %37 ], [ %40, %39 ]
%43 = call i32 @memcpy(i8* %32, i8* %33, i64 %42)
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %7
store i8* %46, i8** %3
br label %47
47: ; preds = %41, %30, %23, %17, %10
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @realloc(i8* %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i8* @malloc(i64 %1)
br label %24
8: ; preds = %2
%9 = call i64 @process_free(i8* %0, i32 0, i64* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %24
13: ; preds = %8
%14 = load i64, i64* %3
%15 = icmp eq i64 %14, %1
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = call i8* @malloc(i64 %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = icmp ugt i64 %14, %1
%21 = select i1 %20, i64 %1, i64 %14
%22 = call i32 @memcpy(i8* %17, i8* %0, i64 %21)
%23 = call i32 @free(i8* %0)
br label %24
24: ; preds = %16, %13, %19, %11, %6
%25 = phi i8* [ %7, %6 ], [ null, %11 ], [ %17, %19 ], [ %0, %13 ], [ null, %16 ]
ret i8* %25
}