#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*/ alloc_mem(unsigned long);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, unsigned long *);
int /*<<< orphan*/ stack_check(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid;
  unsigned long size;
  printf("pid : %d\n", getpid());
  while (1) {
    printf("alloc size(# of pages): ");
    scanf("%lu", &size);
    if (size > 0) {
      size *= 4096;
      printf(" -> alloc %lu bytes\n", size);
      alloc_mem(size);
    }
    if (size == 0)
      stack_check(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe91(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <scanf@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    11cd <main+0x7d>
mov    -0x20(%rbp),%rax
shl    $0xc,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <alloc_mem@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11df <main+0x8f>
xor    %edi,%edi
call   1060 <stack_check@plt>
jmp    117d <main+0x2d>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe9a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe94(%rip),%r14
lea    0xea6(%rip),%r15
mov    %rsp,%rbx
lea    0xea0(%rip),%r12
jmp    1197 <main+0x47>
nopl   0x0(%rax)
xor    %edi,%edi
call   1060 <stack_check@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
mov    (%rsp),%rsi
test   %rsi,%rsi
je     1190 <main+0x40>
shl    $0xc,%rsi
mov    %rsi,(%rsp)
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
call   1030 <alloc_mem@plt>
cmpq   $0x0,(%rsp)
jne    1197 <main+0x47>
jmp    1190 <main+0x40>

LLVM IR (-O0):
@.str = [10 x i8] c"pid : %d\0A\00"
@.str.1 = [25 x i8] c"alloc size(# of pages): \00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [21 x i8] c" -> alloc %lu bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %2, %27
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %7)
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %10
%16 = load i64, i64* %7
%17 = mul i64 %16, 4096
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i64 %18)
%20 = load i64, i64* %7
%21 = call i32 @alloc_mem(i64 %20)
br label %22
22: ; preds = %15, %10
%23 = load i64, i64* %7
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @stack_check(i32 0)
br label %27
27: ; preds = %25, %22
br label %10
}

LLVM IR (-O2):
@.str = [10 x i8] c"pid : %d\0A\00"
@.str.1 = [25 x i8] c"alloc size(# of pages): \00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [21 x i8] c" -> alloc %lu bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %21, %2
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%10 = load i64, i64* %3
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %7
%13 = shl i64 %10, 12
store i64 %13, i64* %3
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i64 %13)
%15 = load i64, i64* %3
%16 = call i32 @alloc_mem(i64 %15)
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %7, %12
%20 = call i32 @stack_check(i32 0)
br label %21
21: ; preds = %19, %12
br label %7
}