#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ *alloc_locked(size_t);
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ munlockall();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ test(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  int megs;
  size_t size;
  uint8_t *test_ptr;
  if (argc < 2) {
    usage();
    return 1;
  }
  megs = atoi(argv[1]);
  if (megs <= 0) {
    fprintf(stderr, "invalid size specified\n");
    return 2;
  }
  size = 1048576UL * megs;
  test_ptr = alloc_locked(size);
  if (test_ptr == NULL) {
    return 3;
  }
  test(test_ptr, size);
  munlockall();
  free(test_ptr);
  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)
cmpl   $0x2,-0x8(%rbp)
jge    11a3 <main+0x33>
mov    $0x0,%al
call   1060 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    1232 <main+0xc2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11de <main+0x6e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1232 <main+0xc2>
movslq -0x14(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <alloc_locked@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120e <main+0x9e>
movl   $0x3,-0x4(%rbp)
jmp    1232 <main+0xc2>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <test@plt>
mov    $0x0,%al
call   1080 <munlockall@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@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
cmp    $0x1,%edi
jg     1187 <main+0x17>
xor    %eax,%eax
call   1060 <usage@plt>
mov    $0x1,%ebx
jmp    11e9 <main+0x79>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
test   %eax,%eax
jle    11c8 <main+0x58>
mov    %eax,%ebx
shl    $0x14,%rbx
mov    %rbx,%rdi
call   1040 <alloc_locked@plt>
test   %rax,%rax
je     11e4 <main+0x74>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1090 <test@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <munlockall@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11e9 <main+0x79>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%ebx
jmp    11e9 <main+0x79>
mov    $0x3,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"invalid size specified\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @usage()
store i32 1, i32* %3
br label %39
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %39
23: ; preds = %13
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = mul i64 1048576, %25
store i64 %26, i64* %7
%27 = load i64, i64* %7
%28 = call i32* @alloc_locked(i64 %27)
store i32* %28, i32** %8
%29 = load i32*, i32** %8
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %23
store i32 3, i32* %3
br label %39
32: ; preds = %23
%33 = load i32*, i32** %8
%34 = load i64, i64* %7
%35 = call i32 @test(i32* %33, i64 %34)
%36 = call i32 (...) @munlockall()
%37 = load i32*, i32** %8
%38 = call i32 @free(i32* %37)
store i32 0, i32* %3
br label %39
39: ; preds = %32, %31, %20, %11
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"invalid size specified\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (...) @usage()
br label %23
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %23
14: ; preds = %6
%15 = zext i32 %9 to i64
%16 = shl nuw nsw i64 %15, 20
%17 = tail call i32* @alloc_locked(i64 %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = tail call i32 @test(i32* %17, i64 %16)
%21 = tail call i32 (...) @munlockall()
%22 = tail call i32 @free(i32* %17)
br label %23
23: ; preds = %14, %19, %11, %4
%24 = phi i32 [ 1, %4 ], [ 2, %11 ], [ 0, %19 ], [ 3, %14 ]
ret i32 %24
}