#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*/ chain_free(void *);
void *chain_malloc(long, short);
int /*<<< orphan*/ memcpy(void *, void *, long);
int /*<<< orphan*/ nixtrace(char *);
int /*<<< orphan*/ printf(char *);
void *chain_realloc(void *palt, long newsize, short mempool) {
  void *p;
  long oldsize;
  char *ph;
  nixtrace("Start chain_realloc");
  p = chain_malloc(newsize, mempool);
  if (p != NULL) {
    if (palt != NULL) {
      ph = palt;
      oldsize = *((long *)(ph - 4));
      if (oldsize != 0)
        memcpy(p, palt, oldsize);
      chain_free(palt);
    }
  } else
    printf("schrecklicher Fehler: chain_realloc gescheitert!\n");
  nixtrace("Ende chain_realloc");
  return p;
}


x86 assembly (clang, -O0):
<chain_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    0xe92(%rip),%rdi
call   1070 <nixtrace@plt>
mov    -0x10(%rbp),%rdi
movswl -0x12(%rbp),%esi
call   1030 <chain_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d8 <chain_realloc+0x88>
cmpq   $0x0,-0x8(%rbp)
je     11d3 <chain_realloc+0x83>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x4(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ca <chain_realloc+0x7a>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1050 <chain_free@plt>
jmp    11e4 <chain_realloc+0x94>
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
lea    0xe5b(%rip),%rdi
call   1070 <nixtrace@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chain_realloc>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe9d(%rip),%rdi
call   1070 <nixtrace@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <chain_malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119d <chain_realloc+0x4d>
test   %r14,%r14
je     11a9 <chain_realloc+0x59>
mov    -0x4(%r14),%rdx
test   %rdx,%rdx
je     1193 <chain_realloc+0x43>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <memcpy@plt>
mov    %r14,%rdi
call   1050 <chain_free@plt>
jmp    11a9 <chain_realloc+0x59>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
lea    0xe64(%rip),%rdi
call   1070 <nixtrace@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Start chain_realloc\00"
@.str.1 = [50 x i8] c"schrecklicher Fehler: chain_realloc gescheitert!\0A\00"
@.str.2 = [19 x i8] c"Ende chain_realloc\00"
define i8* @chain_realloc(i8* %0, i64 %1, i16 signext %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i16 %2, i16* %6
%10 = call i32 @nixtrace(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = load i64, i64* %5
%12 = load i16, i16* %6
%13 = call i8* @chain_malloc(i64 %11, i16 signext %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %36
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i8*, i8** %4
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = getelementptr inbounds i8, i8* %21, i64 -4
%23 = bitcast i8* %22 to i64*
%24 = load i64, i64* %23
store i64 %24, i64* %8
%25 = load i64, i64* %8
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i8*, i8** %7
%29 = load i8*, i8** %4
%30 = load i64, i64* %8
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
br label %32
32: ; preds = %27, %19
%33 = load i8*, i8** %4
%34 = call i32 @chain_free(i8* %33)
br label %35
35: ; preds = %32, %16
br label %38
36: ; preds = %3
%37 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %35
%39 = call i32 @nixtrace(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** %7
ret i8* %40
}

LLVM IR (-O2):
@.str = [20 x i8] c"Start chain_realloc\00"
@.str.2 = [19 x i8] c"Ende chain_realloc\00"
@str = [49 x i8] c"schrecklicher Fehler: chain_realloc gescheitert!\00"
define i8* @chain_realloc(i8* %0, i64 %1, i16 signext %2) {
%4 = tail call i32 @nixtrace(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%5 = tail call i8* @chain_malloc(i64 %1, i16 signext %2)
%6 = icmp eq i8* %5, null
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = icmp eq i8* %0, null
br i1 %8, label %20, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8, i8* %0, i64 -4
%11 = bitcast i8* %10 to i64*
%12 = load i64, i64* %11
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = tail call i32 @memcpy(i8* %5, i8* %0, i64 %12)
br label %16
16: ; preds = %14, %9
%17 = tail call i32 @chain_free(i8* %0)
br label %20
18: ; preds = %3
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %7, %16, %18
%21 = tail call i32 @nixtrace(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
ret i8* %5
}