#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*/ exit(int);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
void *realloc(void *, int);
void *reduce(void *ptr, int size) {
  if (size > (1 * 1024)) {
    void *ptr1 = realloc(ptr, size / 2);
    void *ptr2 = malloc(size / 2);
    if (ptr1 == NULL || ptr2 == NULL) {
      printf("Memory failed to allocate!\n");
      exit(1);
    }
    ptr1 = reduce(ptr1, size / 2);
    ptr2 = reduce(ptr2, size / 2);
    if (*((int *)ptr1) != size / 2 || *((int *)ptr2) != size / 2) {
      printf("Memory failed to contain correct data after many allocations!\n");
      exit(2);
    }
    free(ptr2);
    ptr1 = realloc(ptr1, size);
    if (*((int *)ptr1) != size / 2) {
      printf("Memory failed to contain correct data after realloc()!\n");
      exit(3);
    }
    *((int *)ptr1) = size;
    return ptr1;
  } else {
    *((int *)ptr) = size;
    return ptr;
  }
}


x86 assembly (clang, -O0):
<reduce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jle    12ca <reduce+0x16a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c2 <reduce+0x62>
cmpq   $0x0,-0x28(%rbp)
jne    11d8 <reduce+0x78>
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1040 <reduce@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1040 <reduce@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jne    124e <reduce+0xee>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
je     1264 <reduce+0x104>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     12b4 <reduce+0x154>
lea    0xdb6(%rip),%rdi
call   1050 <printf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12db <reduce+0x17b>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reduce>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
cmp    $0x401,%esi
jl     11cc <reduce+0x6c>
mov    %ebx,%ebp
shr    %ebp
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %r14,%r14
je     11f2 <reduce+0x92>
mov    %rax,%r15
test   %rax,%rax
je     11f2 <reduce+0x92>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <reduce@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <reduce@plt>
cmp    %ebp,(%r14)
jne    11dc <reduce+0x7c>
cmp    %ebp,(%rax)
jne    11dc <reduce+0x7c>
mov    %rax,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
cmp    %ebp,(%rax)
jne    1208 <reduce+0xa8>
mov    %rax,%rdi
mov    %ebx,(%rdi)
mov    %rdi,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe7c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Memory failed to allocate!\0A\00"
@.str.1 = [63 x i8] c"Memory failed to contain correct data after many allocations!\0A\00"
@.str.2 = [56 x i8] c"Memory failed to contain correct data after realloc()!\0A\00"
define i8* @reduce(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 1024
br i1 %9, label %10, label %71
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = sdiv i32 %12, 2
%14 = call i8* @realloc(i8* %11, i32 %13)
store i8* %14, i8** %6
%15 = load i32, i32* %5
%16 = sdiv i32 %15, 2
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %10
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20, %10
%24 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = load i8*, i8** %6
%28 = load i32, i32* %5
%29 = sdiv i32 %28, 2
%30 = call i8* @reduce(i8* %27, i32 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %7
%32 = load i32, i32* %5
%33 = sdiv i32 %32, 2
%34 = call i8* @reduce(i8* %31, i32 %33)
store i8* %34, i8** %7
%35 = load i8*, i8** %6
%36 = bitcast i8* %35 to i32*
%37 = load i32, i32* %36
%38 = load i32, i32* %5
%39 = sdiv i32 %38, 2
%40 = icmp ne i32 %37, %39
br i1 %40, label %48, label %41
41: ; preds = %26
%42 = load i8*, i8** %7
%43 = bitcast i8* %42 to i32*
%44 = load i32, i32* %43
%45 = load i32, i32* %5
%46 = sdiv i32 %45, 2
%47 = icmp ne i32 %44, %46
br i1 %47, label %48, label %51
48: ; preds = %41, %26
%49 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 2)
unreachable
51: ; preds = %41
%52 = load i8*, i8** %7
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %6
%55 = load i32, i32* %5
%56 = call i8* @realloc(i8* %54, i32 %55)
store i8* %56, i8** %6
%57 = load i8*, i8** %6
%58 = bitcast i8* %57 to i32*
%59 = load i32, i32* %58
%60 = load i32, i32* %5
%61 = sdiv i32 %60, 2
%62 = icmp ne i32 %59, %61
br i1 %62, label %63, label %66
63: ; preds = %51
%64 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
%65 = call i32 @exit(i32 3)
unreachable
66: ; preds = %51
%67 = load i32, i32* %5
%68 = load i8*, i8** %6
%69 = bitcast i8* %68 to i32*
store i32 %67, i32* %69
%70 = load i8*, i8** %6
store i8* %70, i8** %3
br label %76
71: ; preds = %2
%72 = load i32, i32* %5
%73 = load i8*, i8** %4
%74 = bitcast i8* %73 to i32*
store i32 %72, i32* %74
%75 = load i8*, i8** %4
store i8* %75, i8** %3
br label %76
76: ; preds = %71, %66
%77 = load i8*, i8** %3
ret i8* %77
}

LLVM IR (-O2):
@str = [55 x i8] c"Memory failed to contain correct data after realloc()!\00"
@str.3 = [62 x i8] c"Memory failed to contain correct data after many allocations!\00"
@str.4 = [27 x i8] c"Memory failed to allocate!\00"
define i8* @reduce(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1024
br i1 %3, label %4, label %36
4: ; preds = %2
%5 = lshr i32 %1, 1
%6 = tail call i8* @realloc(i8* %0, i32 %5)
%7 = tail call i8* @malloc(i32 %5)
%8 = icmp eq i8* %6, null
%9 = icmp eq i8* %7, null
%10 = or i1 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %4
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.4, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %4
%15 = tail call i8* @reduce(i8* %6, i32 %5)
%16 = tail call i8* @reduce(i8* %7, i32 %5)
%17 = bitcast i8* %15 to i32*
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, %5
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = bitcast i8* %16 to i32*
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, %5
br i1 %23, label %27, label %24
24: ; preds = %20, %14
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.3, i64 0, i64 0))
%26 = tail call i32 @exit(i32 2)
unreachable
27: ; preds = %20
%28 = tail call i32 @free(i8* %16)
%29 = tail call i8* @realloc(i8* %15, i32 %1)
%30 = bitcast i8* %29 to i32*
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, %5
br i1 %32, label %38, label %33
33: ; preds = %27
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%35 = tail call i32 @exit(i32 3)
unreachable
36: ; preds = %2
%37 = bitcast i8* %0 to i32*
br label %38
38: ; preds = %27, %36
%39 = phi i32* [ %37, %36 ], [ %30, %27 ]
%40 = phi i8* [ %0, %36 ], [ %29, %27 ]
store i32 %1, i32* %39
ret i8* %40
}