#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 */
size_t SIZE_MAX;
void *checked_realloc(void *, size_t);
int /*<<< orphan*/ fail(char *);
void *checked_realloc2(void *ptr, size_t a, size_t b) {
  if (a != 0 && b > SIZE_MAX / a) {
    fail("Could not allocate memory: size does not fit in size_t.");
  }
  return checked_realloc(ptr, a * b);
}


x86 assembly (clang, -O0):
<checked_realloc2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1175 <checked_realloc2+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jbe    1175 <checked_realloc2+0x55>
lea    0xe90(%rip),%rdi
call   1030 <fail@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1040 <checked_realloc@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_realloc2>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
test   %rsi,%rsi
je     115f <checked_realloc2+0x3f>
mov    0x2e9e(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
je     1173 <checked_realloc2+0x53>
xor    %edx,%edx
div    %r15
cmp    %rbx,%rax
jae    115f <checked_realloc2+0x3f>
lea    0xea6(%rip),%rdi
call   1030 <fail@plt>
imul   %r15,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <checked_realloc@plt>
xor    %edx,%edx
div    %r15d
cmp    %rbx,%rax
jb     1153 <checked_realloc2+0x33>
jmp    115f <checked_realloc2+0x3f>

LLVM IR (-O0):
@SIZE_MAX = global i64 0
@.str = [56 x i8] c"Could not allocate memory: size does not fit in size_t.\00"
define i8* @checked_realloc2(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%7 = load i64, i64* %5
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %3
%10 = load i64, i64* %6
%11 = load i64, i64* @SIZE_MAX
%12 = load i64, i64* %5
%13 = udiv i64 %11, %12
%14 = icmp ugt i64 %10, %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @fail(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %9, %3
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = load i64, i64* %6
%21 = mul i64 %19, %20
%22 = call i8* @checked_realloc(i8* %18, i64 %21)
ret i8* %22
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@.str = [56 x i8] c"Could not allocate memory: size does not fit in size_t.\00"
define i8* @checked_realloc2(i8* %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = load i64, i64* @SIZE_MAX
%7 = udiv i64 %6, %1
%8 = icmp ult i64 %7, %2
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @fail(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5, %3
%12 = mul i64 %2, %1
%13 = tail call i8* @checked_realloc(i8* %0, i64 %12)
ret i8* %13
}