#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(void *);
int /*<<< orphan*/ perror(char *);
void *realloc(void *, size_t);
void *rar_realloc2(void *ptr, size_t size) {
  void *alloc;
  if (!size || size > 1024 * 1024 * 32) {
    return NULL;
  }
  alloc = realloc(ptr, size);
  if (!alloc) {
    perror("rar_realloc2");
    if (ptr)
      free(ptr);
    return NULL;
  } else
    return alloc;
}


x86 assembly (clang, -O0):
<rar_realloc2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1159 <rar_realloc2+0x29>
cmpq   $0x2000000,-0x18(%rbp)
02
jbe    1166 <rar_realloc2+0x36>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <rar_realloc2+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11af <rar_realloc2+0x7f>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
cmpq   $0x0,-0x10(%rbp)
je     11a2 <rar_realloc2+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <rar_realloc2+0x87>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rar_realloc2>:
push   %rbx
lea    -0x2000001(%rsi),%rax
cmp    $0xfffffffffe000000,%rax
jae    1144 <rar_realloc2+0x14>
xor    %eax,%eax
jmp    1151 <rar_realloc2+0x21>
mov    %rdi,%rbx
call   1040 <realloc@plt>
test   %rax,%rax
je     1153 <rar_realloc2+0x23>
pop    %rbx
ret
lea    0xea6(%rip),%rdi
call   1050 <perror@plt>
test   %rbx,%rbx
je     1140 <rar_realloc2+0x10>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1140 <rar_realloc2+0x10>

LLVM IR (-O0):
@.str = [13 x i8] c"rar_realloc2\00"
define i8* @rar_realloc2(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = icmp ugt i64 %10, 33554432
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i8* null, i8** %3
br label %29
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = call i8* @realloc(i8* %14, i64 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %27, label %19
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = call i32 @free(i8* %24)
br label %26
26: ; preds = %23, %19
store i8* null, i8** %3
br label %29
27: ; preds = %13
%28 = load i8*, i8** %6
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %26, %12
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@.str = [13 x i8] c"rar_realloc2\00"
define noalias i8* @rar_realloc2(i8* %0, i64 %1) {
%3 = add i64 %1, -33554433
%4 = icmp ult i64 %3, -33554432
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = tail call i8* @realloc(i8* %0, i64 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %0, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @free(i8* %0)
br label %13
13: ; preds = %5, %8, %11, %2
%14 = phi i8* [ null, %2 ], [ null, %11 ], [ null, %8 ], [ %6, %5 ]
ret i8* %14
}