#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*/ fgets(char *, int, int /*<<< orphan*/);
void *malloc(unsigned long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void *sbrk(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
unsigned long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
main() {
  void *mem = NULL;
  unsigned long size;
  char line[256];
  do {
    printf("\n  sbrk(0) = %p\n", sbrk(0));
    printf("How much memory? ");
    fgets(line, 255, stdin);
    size = strtoul(line, NULL, 0);
    mem = malloc(size);
    printf("  mem     = %p\n", mem);
  } while (mem != NULL);
  perror("malloc");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %rax,%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1050 <fgets@plt>
lea    -0x120(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
jne    117a <main+0x1a>
lea    0xe32(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xe88(%rip),%r14
lea    0xe92(%rip),%r15
mov    0x2e62(%rip),%rbp
mov    %rsp,%r13
lea    0xe93(%rip),%r12
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x0(%rbp),%edx
mov    %r13,%rdi
mov    $0xff,%esi
call   1050 <fgets@plt>
mov    %r13,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
test   %rbx,%rbx
jne    1190 <main+0x30>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"\0A sbrk(0) = %p\0A\00"
@.str.1 = [18 x i8] c"How much memory? \00"
@stdin = global i32 0
@.str.2 = [16 x i8] c" mem = %p\0A\00"
@.str.3 = [7 x i8] c"malloc\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [256 x i8]
store i32 0, i32* %1
store i8* null, i8** %2
br label %5
5: ; preds = %18, %0
%6 = call i8* @sbrk(i32 0)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %9, i32 255, i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = call i64 @strtoul(i8* %12, i32* null, i32 0)
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %5
%19 = load i8*, i8** %2
%20 = icmp ne i8* %19, null
br i1 %20, label %5, label %21
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [17 x i8] c"\0A sbrk(0) = %p\0A\00"
@.str.1 = [18 x i8] c"How much memory? \00"
@stdin = global i32 0
@.str.2 = [16 x i8] c" mem = %p\0A\00"
@.str.3 = [7 x i8] c"malloc\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %3, %0
%4 = call i8* @sbrk(i32 0)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @stdin
%8 = call i32 @fgets(i8* %2, i32 255, i32 %7)
%9 = call i64 @strtoul(i8* nocapture %2, i32* null, i32 0)
%10 = call i8* @malloc(i64 %9)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = icmp eq i8* %10, null
br i1 %12, label %13, label %3
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}