#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memset(void *, int, int);
void *realloc(void *, int);
int /*<<< orphan*/ stderr;
void *thread_func(void *arg) {
  void *ptr = NULL;
  int i;
  for (i = 8; i < 128; i++) {
    int size = (1 << (i / 8)) - 1;
    ptr = realloc(ptr, size);
    if (ptr == NULL) {
      fprintf(stderr, "realloc failed\n");
      exit(1);
    }
    memset(ptr, 42, size);
  }
  free(ptr);
  return 0;
}


x86 assembly (clang, -O0):
<thread_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x8,-0x14(%rbp)
cmpl   $0x80,-0x14(%rbp)
jge    11eb <thread_func+0x9b>
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cc <thread_func+0x7c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x2a,%esi
call   1040 <memset@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116b <thread_func+0x1b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_func>:
push   %rbp
push   %r14
push   %rbx
mov    $0x8,%ebx
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %ebx,%ecx
shr    $0x3,%ecx
mov    $0xffffffff,%ebp
shl    %cl,%ebp
not    %ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11a9 <thread_func+0x59>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2a,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
add    $0x1,%ebx
cmp    $0x80,%ebx
jne    1160 <thread_func+0x10>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"realloc failed\0A\00"
define i8* @thread_func(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
store i32 8, i32* %4
br label %6
6: ; preds = %27, %1
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 128
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = load i32, i32* %4
%11 = sdiv i32 %10, 8
%12 = shl i32 1, %11
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %5
%14 = load i8*, i8** %3
%15 = load i32, i32* %5
%16 = call i8* @realloc(i8* %14, i32 %15)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %9
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %9
%24 = load i8*, i8** %3
%25 = load i32, i32* %5
%26 = call i32 @memset(i8* %24, i32 42, i32 %25)
br label %27
27: ; preds = %23
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %6
30: ; preds = %6
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
ret i8* null
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"realloc failed\0A\00"
define noalias i8* @thread_func(i8* nocapture readnone %0) {
br label %2
2: ; preds = %1, %14
%3 = phi i8* [ null, %1 ], [ %8, %14 ]
%4 = phi i32 [ 8, %1 ], [ %16, %14 ]
%5 = lshr i32 %4, 3
%6 = shl nsw i32 -1, %5
%7 = xor i32 %6, -1
%8 = tail call i8* @realloc(i8* %3, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 @memset(i8* %8, i32 42, i32 %7)
%16 = add nuw nsw i32 %4, 1
%17 = icmp eq i32 %16, 128
br i1 %17, label %18, label %2
18: ; preds = %14
%19 = tail call i32 @free(i8* %8)
ret i8* null
}