#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 */
scalar_t__ checkblock(unsigned int volatile *, size_t, int /*<<< orphan*/, int);
int /*<<< orphan*/ free(void *);
unsigned int *malloc(size_t);
int /*<<< orphan*/ markblock(unsigned int volatile *, size_t,
                             int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
void test2(void) {
  volatile unsigned *x;
  size_t size;
  printf("Entering malloc test 2.\n");
  printf("Make sure you read and understand the comment in malloctest.c "
         "that\nexplains the conditions this test assumes.\n\n");
  printf("Testing how much memory we can allocate:\n");
  for (size = (1024 * 1024 * 1024); (x = malloc(size)) == NULL;
       size = size / 2) {
    printf("  %9lu bytes: failed\n", (unsigned long)size);
  }
  printf("  %9lu bytes: succeeded\n", (unsigned long)size);
  printf("Passed part 1\n");
  printf("Touching all the words in the block.\n");
  markblock(x, size, 0, 1);
  printf("Validating the words in the block.\n");
  if (checkblock(x, size, 0, 1)) {
    printf("FAILED: data corrupt\n");
    return;
  }
  printf("Passed part 2\n");
  printf("Freeing the block\n");
  free((void *)x);
  printf("Passed part 3\n");
  printf("Allocating another block\n");
  x = malloc(size);
  if (x == NULL) {
    printf("FAILED: free didn't return the memory?\n");
    return;
  }
  free((void *)x);
  printf("Passed malloc test 2.\n");
}


x86 assembly (clang, -O0):
<test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x40000000,-0x10(%rbp)
40
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11c4 <test2+0x74>
mov    -0x10(%rbp),%rsi
lea    0xf07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    118a <test2+0x3a>
mov    -0x10(%rbp),%rsi
lea    0xefa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1060 <markblock@plt>
lea    0xf0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1070 <checkblock@plt>
cmp    $0x0,%rax
je     1245 <test2+0xf5>
lea    0xf02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <test2+0x178>
lea    0xf05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
lea    0xf02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    12b1 <test2+0x161>
lea    0xef7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <test2+0x178>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
lea    0xf03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test2>:
push   %r15
push   %r14
push   %rbx
lea    0xec3(%rip),%rdi
call   1040 <puts@plt>
lea    0xecf(%rip),%rdi
call   1040 <puts@plt>
lea    0xf32(%rip),%rdi
call   1040 <puts@plt>
mov    $0x40000000,%ebx
mov    $0x40000000,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d0 <test2+0x70>
mov    $0x40000000,%ebx
lea    0xe54(%rip),%r15
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
shr    %rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b0 <test2+0x50>
mov    %rax,%r14
lea    0xe3f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xef7(%rip),%rdi
call   1040 <puts@plt>
lea    0xef9(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1070 <markblock@plt>
lea    0xf00(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1080 <checkblock@plt>
test   %rax,%rax
je     1237 <test2+0xd7>
lea    0xf84(%rip),%rdi
jmp    1294 <test2+0x134>
lea    0xef7(%rip),%rdi
call   1040 <puts@plt>
lea    0xef9(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xef7(%rip),%rdi
call   1040 <puts@plt>
lea    0xef9(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     128d <test2+0x12d>
mov    %rax,%rdi
call   1030 <free@plt>
lea    0xef1(%rip),%rdi
jmp    1294 <test2+0x134>
lea    0xefe(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <puts@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Entering malloc test 2.\0A\00"
@.str.1 = [112 x i8] c"Make sure you read and understand the comment in malloctest.c that\0Aexplains the conditions this test assumes.\0A\0A\00"
@.str.2 = [42 x i8] c"Testing how much memory we can allocate:\0A\00"
@.str.3 = [22 x i8] c" %9lu bytes: failed\0A\00"
@.str.4 = [25 x i8] c" %9lu bytes: succeeded\0A\00"
@.str.5 = [15 x i8] c"Passed part 1\0A\00"
@.str.6 = [38 x i8] c"Touching all the words in the block.\0A\00"
@.str.7 = [36 x i8] c"Validating the words in the block.\0A\00"
@.str.8 = [22 x i8] c"FAILED: data corrupt\0A\00"
@.str.9 = [15 x i8] c"Passed part 2\0A\00"
@.str.10 = [19 x i8] c"Freeing the block\0A\00"
@.str.11 = [15 x i8] c"Passed part 3\0A\00"
@.str.12 = [26 x i8] c"Allocating another block\0A\00"
@.str.13 = [40 x i8] c"FAILED: free didn't return the memory?\0A\00"
@.str.14 = [23 x i8] c"Passed malloc test 2.\0A\00"
define void @test2() {
%1 = alloca i32*
%2 = alloca i64
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
store i64 1073741824, i64* %2
br label %6
6: ; preds = %13, %0
%7 = load i64, i64* %2
%8 = call i32* @malloc(i64 %7)
store i32* %8, i32** %1
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = load i64, i64* %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %11)
br label %13
13: ; preds = %10
%14 = load i64, i64* %2
%15 = udiv i64 %14, 2
store i64 %15, i64* %2
br label %6
16: ; preds = %6
%17 = load i64, i64* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i64 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
%21 = load i32*, i32** %1
%22 = load i64, i64* %2
%23 = call i32 @markblock(i32* %21, i64 %22, i32 0, i32 1)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%25 = load i32*, i32** %1
%26 = load i64, i64* %2
%27 = call i64 @checkblock(i32* %25, i64 %26, i32 0, i32 1)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
br label %50
31: ; preds = %16
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0))
%34 = load i32*, i32** %1
%35 = bitcast i32* %34 to i8*
%36 = call i32 @free(i8* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.12, i64 0, i64 0))
%39 = load i64, i64* %2
%40 = call i32* @malloc(i64 %39)
store i32* %40, i32** %1
%41 = load i32*, i32** %1
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0))
br label %50
45: ; preds = %31
%46 = load i32*, i32** %1
%47 = bitcast i32* %46 to i8*
%48 = call i32 @free(i8* %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0))
br label %50
50: ; preds = %45, %43, %29
ret void
}

LLVM IR (-O2):
@.str.3 = [22 x i8] c" %9lu bytes: failed\0A\00"
@.str.4 = [25 x i8] c" %9lu bytes: succeeded\0A\00"
@str = [24 x i8] c"Entering malloc test 2.\00"
@str.15 = [111 x i8] c"Make sure you read and understand the comment in malloctest.c that\0Aexplains the conditions this test assumes.\0A\00"
@str.16 = [41 x i8] c"Testing how much memory we can allocate:\00"
@str.17 = [14 x i8] c"Passed part 1\00"
@str.18 = [37 x i8] c"Touching all the words in the block.\00"
@str.19 = [35 x i8] c"Validating the words in the block.\00"
@str.20 = [14 x i8] c"Passed part 2\00"
@str.21 = [18 x i8] c"Freeing the block\00"
@str.22 = [14 x i8] c"Passed part 3\00"
@str.23 = [25 x i8] c"Allocating another block\00"
@str.24 = [22 x i8] c"Passed malloc test 2.\00"
@str.25 = [39 x i8] c"FAILED: free didn't return the memory?\00"
@str.26 = [21 x i8] c"FAILED: data corrupt\00"
define void @test2() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([111 x i8], [111 x i8]* @str.15, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.16, i64 0, i64 0))
%4 = tail call i32* @malloc(i64 1073741824)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %0, %6
%7 = phi i64 [ %9, %6 ], [ 1073741824, %0 ]
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %7)
%9 = lshr i64 %7, 1
%10 = tail call i32* @malloc(i64 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %6, label %12
12: ; preds = %6, %0
%13 = phi i64 [ 1073741824, %0 ], [ %9, %6 ]
%14 = phi i32* [ %4, %0 ], [ %10, %6 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i64 %13)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.17, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.18, i64 0, i64 0))
%18 = tail call i32 @markblock(i32* %14, i64 %13, i32 0, i32 1)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.19, i64 0, i64 0))
%20 = tail call i64 @checkblock(i32* %14, i64 %13, i32 0, i32 1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %12
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.20, i64 0, i64 0))
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.21, i64 0, i64 0))
%25 = bitcast i32* %14 to i8*
%26 = tail call i32 @free(i8* %25)
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.22, i64 0, i64 0))
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.23, i64 0, i64 0))
%29 = tail call i32* @malloc(i64 %13)
%30 = icmp eq i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %22
%32 = bitcast i32* %29 to i8*
%33 = tail call i32 @free(i8* %32)
br label %34
34: ; preds = %22, %12, %31
%35 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @str.24, i64 0, i64 0), %31 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.26, i64 0, i64 0), %12 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @str.25, i64 0, i64 0), %22 ]
%36 = tail call i32 @puts(i8* dereferenceable(1) %35)
ret void
}