#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 */
long atol(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(long);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  long howmuch = 1 << 20;
  if (argc >= 2) {
    howmuch = atol(argv[1]);
    if (howmuch < 1) {
      fprintf(stderr, "At least one byte please.\n");
      return 1;
    }
  }
  unsigned char *tibet = malloc(howmuch);
  if (tibet == NULL) {
    fprintf(stderr, "Oh no that didn't malloc.\n");
  }
  long i;
  for (i = 0; i < howmuch; ++i) {
    tibet[i] = howmuch & 0xFF;
  }
  free(tibet);
  printf("That worked.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x100000,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11ba <main+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atol@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jge    11b5 <main+0x65>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1241 <main+0xf1>
jmp    11ba <main+0x6a>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <main+0x97>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1225 <main+0xd5>
mov    -0x18(%rbp),%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ef <main+0x9f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x100000,%ebx
cmp    $0x2,%edi
jl     117f <main+0x1f>
mov    0x8(%rsi),%rdi
call   1080 <atol@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11c9 <main+0x69>
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a4 <main+0x44>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    11e3 <main+0x83>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"At least one byte please.\0A\00"
@.str.1 = [27 x i8] c"Oh no that didn't malloc.\0A\00"
@.str.2 = [14 x i8] c"That worked.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1048576, i64* %6
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 2
br i1 %10, label %11, label %22
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @atol(i8* %14)
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %49
21: ; preds = %11
br label %22
22: ; preds = %21, %2
%23 = load i64, i64* %6
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %27, %22
store i64 0, i64* %8
br label %31
31: ; preds = %42, %30
%32 = load i64, i64* %8
%33 = load i64, i64* %6
%34 = icmp slt i64 %32, %33
br i1 %34, label %35, label %45
35: ; preds = %31
%36 = load i64, i64* %6
%37 = and i64 %36, 255
%38 = trunc i64 %37 to i8
%39 = load i8*, i8** %7
%40 = load i64, i64* %8
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 %38, i8* %41
br label %42
42: ; preds = %35
%43 = load i64, i64* %8
%44 = add nsw i64 %43, 1
store i64 %44, i64* %8
br label %31
45: ; preds = %31
%46 = load i8*, i8** %7
%47 = call i32 @free(i8* %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %49
49: ; preds = %45, %18
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"At least one byte please.\0A\00"
@.str.1 = [27 x i8] c"Oh no that didn't malloc.\0A\00"
@str = [13 x i8] c"That worked.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @atol(i8* %6)
%8 = icmp slt i64 %7, 1
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %23
12: ; preds = %4, %2
%13 = phi i64 [ %7, %4 ], [ 1048576, %2 ]
%14 = tail call i8* @malloc(i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %16, %12
%20 = trunc i64 %13 to i8
%21 = tail call i32 @free(i8* %14)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %19, %9
%24 = phi i32 [ 1, %9 ], [ 0, %19 ]
ret i32 %24
}