#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 *mem_alloc(int);
int /*<<< orphan*/ mem_free(int *);
scalar_t__ mem_realloc(int *, int);
void test_realloc() {
  int i, size = 173;
  int *p = mem_alloc(size);
  for (i = 0; i < 100; i++) {
    p = (int *)mem_realloc(p, size);
    *(p + size / sizeof(int) - 1) = 345;
    size += 173;
  }
  mem_free(p);
}


x86 assembly (clang, -O0):
<test_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0xad,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <mem_alloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x64,-0x4(%rbp)
jge    1199 <test_realloc+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <mem_realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
shr    $0x2,%rcx
movl   $0x159,-0x4(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0xad,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1152 <test_realloc+0x22>
mov    -0x10(%rbp),%rdi
call   1030 <mem_free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_realloc>:
push   %rbx
mov    $0xad,%ebx
mov    $0xad,%edi
call   1050 <mem_alloc@plt>
mov    %rax,%rdi
mov    %ebx,%esi
call   1040 <mem_realloc@plt>
mov    %ebx,%ecx
and    $0xfffffffc,%ecx
movl   $0x159,-0x4(%rax,%rcx,1)
add    $0xad,%ebx
cmp    $0x4441,%ebx
jne    1140 <test_realloc+0x10>
mov    %rax,%rdi
pop    %rbx
jmp    1030 <mem_free@plt>

LLVM IR (-O0):
define void @test_realloc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
store i32 173, i32* %2
%4 = load i32, i32* %2
%5 = call i32* @mem_alloc(i32 %4)
store i32* %5, i32** %3
store i32 0, i32* %1
br label %6
6: ; preds = %22, %0
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 100
br i1 %8, label %9, label %25
9: ; preds = %6
%10 = load i32*, i32** %3
%11 = load i32, i32* %2
%12 = call i64 @mem_realloc(i32* %10, i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %3
%14 = load i32*, i32** %3
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = udiv i64 %16, 4
%18 = getelementptr inbounds i32, i32* %14, i64 %17
%19 = getelementptr inbounds i32, i32* %18, i64 -1
store i32 345, i32* %19
%20 = load i32, i32* %2
%21 = add nsw i32 %20, 173
store i32 %21, i32* %2
br label %22
22: ; preds = %9
%23 = load i32, i32* %1
%24 = add nsw i32 %23, 1
store i32 %24, i32* %1
br label %6
25: ; preds = %6
%26 = load i32*, i32** %3
%27 = call i32 @mem_free(i32* %26)
ret void
}

LLVM IR (-O2):
define void @test_realloc() {
%1 = tail call i32* @mem_alloc(i32 173)
br label %2
2: ; preds = %0, %2
%3 = phi i32* [ %1, %0 ], [ %7, %2 ]
%4 = phi i32 [ 173, %0 ], [ %12, %2 ]
%5 = phi i32 [ 0, %0 ], [ %13, %2 ]
%6 = tail call i64 @mem_realloc(i32* %3, i32 %4)
%7 = inttoptr i64 %6 to i32*
%8 = lshr i32 %4, 2
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %7, i64 -1
%11 = getelementptr inbounds i32, i32* %10, i64 %9
store i32 345, i32* %11
%12 = add nuw nsw i32 %4, 173
%13 = add nuw nsw i32 %5, 1
%14 = icmp eq i32 %13, 100
br i1 %14, label %15, label %2
15: ; preds = %2
%16 = inttoptr i64 %6 to i32*
%17 = tail call i32 @mem_free(i32* %16)
ret void
}