#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*/ LOG_CRIT;
int /*<<< orphan*/ _debug(char *, int, int /*<<< orphan*/, char *, size_t,
                          int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
void *realloc(void *, size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void *safe_realloc(void *ptr, size_t newsize) {
  void *retval = ((void *)0);
  retval = realloc(ptr, newsize);
  if (((void *)0) == retval) {
    _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/richardhmm/HMMCodeRepository/refs/heads/"
           "master/c_demo/src/safe.c",
           111, LOG_CRIT,
           "Failed to realloc buffer to %d bytes of memory: %s. Bailing out",
           newsize, strerror(errno));
    exit(1);
  }
  return retval;
}


x86 assembly (clang, -O0):
<safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11c3 <safe_realloc+0x83>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%r8
mov    %eax,%r9d
lea    0xe58(%rip),%rdi
mov    $0x6f,%esi
lea    0xee7(%rip),%rcx
call   1030 <_debug@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1040 <realloc@plt>
test   %rax,%rax
je     1157 <safe_realloc+0x17>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e8a(%rip),%rax
mov    (%rax),%ebp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe8b(%rip),%rdi
lea    0xf1f(%rip),%rcx
mov    $0x6f,%esi
mov    %ebp,%edx
mov    %rbx,%r8
mov    %eax,%r9d
call   1030 <_debug@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [155 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/richardhmm/HMMCodeRepository/refs/heads/master/c_demo/src/safe.c\00"
@LOG_CRIT = global i32 0
@.str.1 = [64 x i8] c"Failed to realloc buffer to %d bytes of memory: %s. Bailing out\00"
@errno = global i32 0
define i8* @safe_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
store i8* null, i8** %5
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = call i8* @realloc(i8* %6, i64 %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp eq i8* null, %9
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @LOG_CRIT
%13 = load i64, i64* %4
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @_debug(i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str, i64 0, i64 0), i32 111, i32 %12, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i64 %13, i32 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8*, i8** %5
ret i8* %19
}

LLVM IR (-O2):
@.str = [155 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/richardhmm/HMMCodeRepository/refs/heads/master/c_demo/src/safe.c\00"
@LOG_CRIT = global i32 0
@.str.1 = [64 x i8] c"Failed to realloc buffer to %d bytes of memory: %s. Bailing out\00"
@errno = global i32 0
define noalias i8* @safe_realloc(i8* nocapture %0, i64 %1) {
%3 = tail call i8* @realloc(i8* %0, i64 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* @LOG_CRIT
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @_debug(i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str, i64 0, i64 0), i32 111, i32 %6, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i64 %1, i32 %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
ret i8* %3
}