void touch_stack(void) {
  int stack_size = 1024 * 64;
  char hepp[stack_size];
  fill(stack_size, hepp);
  int ret = 0;
  for (int i = stack_size - 1; i >= 0; i--) {
    ret += hepp[i];
    __asm__("");
  }
  printf("stack is hopefully touched enough: %d\n", ret);
}


x86 assembly (clang, -O0):
<touch_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x10000,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x28(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1040 <fill@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1199 <touch_stack+0x79>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    116f <touch_stack+0x4f>
mov    -0x1c(%rbp),%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<touch_stack>:
push   %rbx
sub    $0x10000,%rsp
xor    %ebx,%ebx
mov    %rsp,%rsi
mov    $0x10000,%edi
xor    %eax,%eax
call   1040 <fill@plt>
mov    $0x10000,%eax
xchg   %ax,%ax
movsbl -0x1(%rsp,%rax,1),%ecx
add    %ecx,%ebx
add    $0xffffffffffffffff,%rax
jne    1140 <touch_stack+0x20>
lea    0xeac(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x10000,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [39 x i8] c"stack is hopefully touched enough: %d\0A\00"
define void @touch_stack() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 65536, i32* %1
%6 = load i32, i32* %1
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %2
%9 = alloca i8, i64 %7
store i64 %7, i64* %3
%10 = load i32, i32* %1
%11 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @fill to i32 (i32, i8*, ...)*)(i32 %10, i8* %9)
store i32 0, i32* %4
%12 = load i32, i32* %1
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %5
br label %14
14: ; preds = %25, %0
%15 = load i32, i32* %5
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %9, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = load i32, i32* %4
%24 = add nsw i32 %23, %22
store i32 %24, i32* %4
br label %25
25: ; preds = %17
%26 = load i32, i32* %5
%27 = add nsw i32 %26, -1
store i32 %27, i32* %5
br label %14
28: ; preds = %14
%29 = load i32, i32* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = load i8*, i8** %2
ret void
}

LLVM IR (-O2):
@.str = [39 x i8] c"stack is hopefully touched enough: %d\0A\00"
define void @touch_stack() {
%1 = alloca [65536 x i8]
%2 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 0
%3 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @fill to i32 (i32, i8*, ...)*)(i32 65536, i8* %2)
br label %6
4: ; preds = %6
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %12)
ret void
6: ; preds = %0, %6
%7 = phi i64 [ 65535, %0 ], [ %13, %6 ]
%8 = phi i32 [ 0, %0 ], [ %12, %6 ]
%9 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 %7
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = add nsw i32 %8, %11
%13 = add nsw i64 %7, -1
%14 = icmp eq i64 %7, 0
br i1 %14, label %4, label %6
}