#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 size_t uint8_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(size_t *);
size_t *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
void test_malloc(size_t bytes) {
  printf("malloctest: testing allocation of %zu bytes\n", bytes);
  uint8_t *buf = malloc(bytes);
  if (!buf) {
    printf("malloctest: malloc returned null\n");
    exit(1);
  }
  for (size_t i = 0; i < bytes; i++) {
    buf[i] = i % 256;
  }
  free(buf);
  return;
}


x86 assembly (clang, -O0):
<test_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <test_malloc+0x4e>
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11cc <test_malloc+0x8c>
mov    -0x18(%rbp),%rdx
and    $0xff,%rdx
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1196 <test_malloc+0x56>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc>:
push   %rbx
mov    %rdi,%rbx
lea    0xf05(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     12ab <test_malloc+0x15b>
test   %rbx,%rbx
je     1290 <test_malloc+0x140>
cmp    $0x4,%rbx
jae    118c <test_malloc+0x3c>
xor    %ecx,%ecx
jmp    1280 <test_malloc+0x130>
mov    %rbx,%rcx
and    $0xfffffffffffffffc,%rcx
lea    -0x4(%rcx),%rdx
mov    %rdx,%rsi
shr    $0x2,%rsi
add    $0x1,%rsi
test   %rdx,%rdx
je     1299 <test_malloc+0x149>
mov    %rsi,%rdi
and    $0xfffffffffffffffe,%rdi
movdqa 0xe46(%rip),%xmm0
xor    %edx,%edx
movdqa 0xe4c(%rip),%xmm1
movdqa 0xe54(%rip),%xmm2
movdqa 0xe5c(%rip),%xmm3
movdqa 0xe64(%rip),%xmm4
movdqa 0xe6c(%rip),%xmm5
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movdqa %xmm0,%xmm6
paddq  %xmm1,%xmm6
movdqa %xmm0,%xmm7
pand   %xmm2,%xmm7
pand   %xmm2,%xmm6
movdqu %xmm7,(%rax,%rdx,8)
movdqu %xmm6,0x10(%rax,%rdx,8)
movdqa %xmm0,%xmm6
paddq  %xmm3,%xmm6
movdqa %xmm0,%xmm7
paddq  %xmm4,%xmm7
pand   %xmm2,%xmm6
pand   %xmm2,%xmm7
movdqu %xmm6,0x20(%rax,%rdx,8)
movdqu %xmm7,0x30(%rax,%rdx,8)
add    $0x8,%rdx
paddq  %xmm5,%xmm0
add    $0xfffffffffffffffe,%rdi
jne    11f0 <test_malloc+0xa0>
test   $0x1,%sil
je     126e <test_malloc+0x11e>
movdqa 0xdc1(%rip),%xmm1
paddq  %xmm0,%xmm1
movdqa 0xdc5(%rip),%xmm2
pand   %xmm2,%xmm0
pand   %xmm2,%xmm1
movdqu %xmm0,(%rax,%rdx,8)
movdqu %xmm1,0x10(%rax,%rdx,8)
cmp    %rbx,%rcx
je     1290 <test_malloc+0x140>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl %cl,%edx
mov    %rdx,(%rax,%rcx,8)
add    $0x1,%rcx
cmp    %rcx,%rbx
jne    1280 <test_malloc+0x130>
mov    %rax,%rdi
pop    %rbx
jmp    1030 <free@plt>
movdqa 0xd5f(%rip),%xmm0
xor    %edx,%edx
test   $0x1,%sil
jne    1247 <test_malloc+0xf7>
jmp    126e <test_malloc+0x11e>
lea    0xddb(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"malloctest: testing allocation of %zu bytes\0A\00"
@.str.1 = [34 x i8] c"malloctest: malloc returned null\0A\00"
define void @test_malloc(i64 %0) {
%2 = alloca i64
%3 = alloca i64*
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %5)
%7 = load i64, i64* %2
%8 = call i64* @malloc(i64 %7)
store i64* %8, i64** %3
%9 = load i64*, i64** %3
%10 = icmp ne i64* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
store i64 0, i64* %4
br label %15
15: ; preds = %25, %14
%16 = load i64, i64* %4
%17 = load i64, i64* %2
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i64, i64* %4
%21 = urem i64 %20, 256
%22 = load i64*, i64** %3
%23 = load i64, i64* %4
%24 = getelementptr inbounds i64, i64* %22, i64 %23
store i64 %21, i64* %24
br label %25
25: ; preds = %19
%26 = load i64, i64* %4
%27 = add i64 %26, 1
store i64 %27, i64* %4
br label %15
28: ; preds = %15
%29 = load i64*, i64** %3
%30 = call i32 @free(i64* %29)
ret void
}

LLVM IR (-O2):
@.str = [45 x i8] c"malloctest: testing allocation of %zu bytes\0A\00"
@str = [33 x i8] c"malloctest: malloc returned null\00"
define void @test_malloc(i64 %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %0)
%3 = tail call i64* @malloc(i64 %0)
%4 = icmp eq i64* %3, null
br i1 %4, label %58, label %5
5: ; preds = %1
%6 = icmp eq i64 %0, 0
br i1 %6, label %61, label %7
7: ; preds = %5
%8 = icmp ult i64 %0, 4
br i1 %8, label %56, label %9
9: ; preds = %7
%10 = and i64 %0, -4
%11 = add i64 %10, -4
%12 = lshr exact i64 %11, 2
%13 = add nuw nsw i64 %12, 1
%14 = and i64 %13, 1
%15 = icmp eq i64 %11, 0
br i1 %15, label %42, label %16
16: ; preds = %9
%17 = and i64 %13, 9223372036854775806
br label %18
18: ; preds = %18, %16
%19 = phi i64 [ 0, %16 ], [ %38, %18 ]
%20 = phi <2 x i64> [ <i64 0, i64 1>, %16 ], [ %39, %18 ]
%21 = phi i64 [ 0, %16 ], [ %40, %18 ]
%22 = add <2 x i64> %20, <i64 2, i64 2>
%23 = and <2 x i64> %20, <i64 255, i64 255>
%24 = and <2 x i64> %22, <i64 255, i64 255>
%25 = getelementptr inbounds i64, i64* %3, i64 %19
%26 = bitcast i64* %25 to <2 x i64>*
store <2 x i64> %23, <2 x i64>* %26
%27 = getelementptr inbounds i64, i64* %25, i64 2
%28 = bitcast i64* %27 to <2 x i64>*
store <2 x i64> %24, <2 x i64>* %28
%29 = or i64 %19, 4
%30 = add <2 x i64> %20, <i64 4, i64 4>
%31 = add <2 x i64> %20, <i64 6, i64 6>
%32 = and <2 x i64> %30, <i64 255, i64 255>
%33 = and <2 x i64> %31, <i64 255, i64 255>
%34 = getelementptr inbounds i64, i64* %3, i64 %29
%35 = bitcast i64* %34 to <2 x i64>*
store <2 x i64> %32, <2 x i64>* %35
%36 = getelementptr inbounds i64, i64* %34, i64 2
%37 = bitcast i64* %36 to <2 x i64>*
store <2 x i64> %33, <2 x i64>* %37
%38 = add nuw i64 %19, 8
%39 = add <2 x i64> %20, <i64 8, i64 8>
%40 = add i64 %21, 2
%41 = icmp eq i64 %40, %17
br i1 %41, label %42, label %18
42: ; preds = %18, %9
%43 = phi i64 [ 0, %9 ], [ %38, %18 ]
%44 = phi <2 x i64> [ <i64 0, i64 1>, %9 ], [ %39, %18 ]
%45 = icmp eq i64 %14, 0
br i1 %45, label %54, label %46
46: ; preds = %42
%47 = add <2 x i64> %44, <i64 2, i64 2>
%48 = and <2 x i64> %44, <i64 255, i64 255>
%49 = and <2 x i64> %47, <i64 255, i64 255>
%50 = getelementptr inbounds i64, i64* %3, i64 %43
%51 = bitcast i64* %50 to <2 x i64>*
store <2 x i64> %48, <2 x i64>* %51
%52 = getelementptr inbounds i64, i64* %50, i64 2
%53 = bitcast i64* %52 to <2 x i64>*
store <2 x i64> %49, <2 x i64>* %53
br label %54
54: ; preds = %42, %46
%55 = icmp eq i64 %10, %0
br i1 %55, label %61, label %56
56: ; preds = %7, %54
%57 = phi i64 [ 0, %7 ], [ %10, %54 ]
br label %63
58: ; preds = %1
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%60 = tail call i32 @exit(i32 1)
unreachable
61: ; preds = %63, %54, %5
%62 = tail call i32 @free(i64* %3)
ret void
63: ; preds = %56, %63
%64 = phi i64 [ %67, %63 ], [ %57, %56 ]
%65 = and i64 %64, 255
%66 = getelementptr inbounds i64, i64* %3, i64 %64
store i64 %65, i64* %66
%67 = add nuw i64 %64, 1
%68 = icmp eq i64 %67, %0
br i1 %68, label %61, label %63
}