#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 */
scalar_t__ alloca(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char const *);
int /*<<< orphan*/ stdout;
void *eat_stack(void *p) {
  const char *s = *((int *)p) == 1 ? "thread " : "";
  int n = 0;
  for (;;) {
    printf("\rAllocated %d bytes from %sstack ...", n, s);
    fflush(stdout);
    n += 128;
    *((volatile char *)alloca(128)) = 0;
  }
}


x86 assembly (clang, -O0):
<eat_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
lea    0xebe(%rip),%rax
lea    0xeb0(%rip),%rcx
cmp    $0x1,%edx
cmove  %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x14(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0x18(%rbp)
mov    %eax,-0x14(%rbp)
mov    $0x80,%edi
call   1040 <alloca@plt>
movb   $0x0,(%rax)
jmp    1162 <eat_stack+0x32>

x86 assembly (clang, -O2):
<eat_stack>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpl   $0x1,(%rdi)
lea    0xebf(%rip),%rax
lea    0xebf(%rip),%rbx
cmove  %rax,%rbx
xor    %ebp,%ebp
lea    0xeb3(%rip),%r14
mov    0x2e84(%rip),%r15
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
mov    (%r15),%edi
call   1050 <fflush@plt>
sub    $0xffffff80,%ebp
mov    $0x80,%edi
call   1040 <alloca@plt>
movb   $0x0,(%rax)
jmp    1160 <eat_stack+0x30>

LLVM IR (-O0):
@.str = [8 x i8] c"thread \00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [37 x i8] c"\0DAllocated %d bytes from %sstack ...\00"
@stdout = global i32 0
define i8* @eat_stack(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = bitcast i8* %5 to i32*
%7 = load i32, i32* %6
%8 = icmp eq i32 %7, 1
%9 = zext i1 %8 to i64
%10 = select i1 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0)
store i8* %10, i8** %3
store i32 0, i32* %4
br label %11
11: ; preds = %11, %1
%12 = load i32, i32* %4
%13 = load i8*, i8** %3
%14 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %12, i8* %13)
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 128
store i32 %18, i32* %4
%19 = call i64 @alloca(i32 128)
%20 = inttoptr i64 %19 to i8*
store volatile i8 0, i8* %20
br label %11
}

LLVM IR (-O2):
@.str = [8 x i8] c"thread \00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [37 x i8] c"\0DAllocated %d bytes from %sstack ...\00"
@stdout = global i32 0
define noalias i8* @eat_stack(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = icmp eq i32 %3, 1
%5 = select i1 %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0)
br label %6
6: ; preds = %6, %1
%7 = phi i32 [ 0, %1 ], [ %11, %6 ]
%8 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %7, i8* %5)
%9 = load i32, i32* @stdout
%10 = tail call i32 @fflush(i32 %9)
%11 = add nuw nsw i32 %7, 128
%12 = tail call i64 @alloca(i32 128)
%13 = inttoptr i64 %12 to i8*
store volatile i8 0, i8* %13
br label %6
}