#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);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *, ...);
void *sbrk(int);
int main(int argc, char *argv[]) {
  void *p;
  int i;
  extern char etext, edata, end;
  printf("etext: %p\n", &etext);
  printf("edata: %p\n", &edata);
  printf("  end: %p\n", &end);
  for (i = 1; i < 4096; ++i) {
    p = sbrk(i);
    if (p == (void *)-1) {
      perror("sbrk error");
      exit(1);
    }
    *((char *)p) = 0x0A;
    printf("0x%04X : %p\n", i, p);
  }
  p = malloc(0x04);
  printf("%p\n", p);
  p = malloc(0x04);
  printf("%p\n", p);
  p = malloc(0x04);
  printf("%p\n", p);
  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)
lea    0xe93(%rip),%rdi
mov    0x2e74(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe89(%rip),%rdi
mov    0x2e3f(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7f(%rip),%rdi
mov    0x2e3a(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x1000,-0x1c(%rbp)
jge    1217 <main+0xc7>
mov    -0x1c(%rbp),%edi
call   1040 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    11ec <main+0x9c>
lea    0xe44(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
movb   $0xa,(%rax)
movslq -0x1c(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ac <main+0x5c>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
mov    0x2e86(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe9b(%rip),%rdi
mov    0x2e51(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe91(%rip),%rdi
mov    0x2e4c(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
lea    0xe8d(%rip),%r14
nop
mov    %ebx,%edi
call   1040 <sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     1223 <main+0xd3>
movb   $0xa,(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    $0x1000,%rbx
jne    11a0 <main+0x50>
mov    $0x4,%edi
call   1050 <malloc@plt>
lea    0xe57(%rip),%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"etext: %p\0A\00"
@etext = external global i8
@.str.1 = [11 x i8] c"edata: %p\0A\00"
@edata = external global i8
@.str.2 = [11 x i8] c" end: %p\0A\00"
@end = external global i8
@.str.3 = [11 x i8] c"sbrk error\00"
@.str.4 = [13 x i8] c"0x%04X : %p\0A\00"
@.str.5 = [4 x i8] c"%p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* @etext)
%9 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* @edata)
%10 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* @end)
store i32 1, i32* %7
br label %11
11: ; preds = %29, %2
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 4096
br i1 %13, label %14, label %32
14: ; preds = %11
%15 = load i32, i32* %7
%16 = call i8* @sbrk(i32 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, inttoptr (i64 -1 to i8*)
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = load i8*, i8** %6
store i8 10, i8* %23
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = inttoptr i64 %25 to i8*
%27 = load i8*, i8** %6
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %26, i8* %27)
br label %29
29: ; preds = %22
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %11
32: ; preds = %11
%33 = call i8* @malloc(i32 4)
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %34)
%36 = call i8* @malloc(i32 4)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = call i8* @malloc(i32 4)
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %40)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"etext: %p\0A\00"
@etext = external global i8
@.str.1 = [11 x i8] c"edata: %p\0A\00"
@edata = external global i8
@.str.2 = [11 x i8] c" end: %p\0A\00"
@end = external global i8
@.str.3 = [11 x i8] c"sbrk error\00"
@.str.4 = [13 x i8] c"0x%04X : %p\0A\00"
@.str.5 = [4 x i8] c"%p\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* @etext)
%4 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* @edata)
%5 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* @end)
br label %6
6: ; preds = %2, %14
%7 = phi i64 [ 1, %2 ], [ %17, %14 ]
%8 = trunc i64 %7 to i32
%9 = tail call i8* @sbrk(i32 %8)
%10 = icmp eq i8* %9, inttoptr (i64 -1 to i8*)
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6
store i8 10, i8* %9
%15 = inttoptr i64 %7 to i8*
%16 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %15, i8* %9)
%17 = add nuw nsw i64 %7, 1
%18 = icmp eq i64 %17, 4096
br i1 %18, label %19, label %6
19: ; preds = %14
%20 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%21 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%22 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%23 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %22)
%24 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%25 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %24)
ret i32 0
}