#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
void mallocExample(size_t bytes) {
  printf("Allocating %zu bytes of memory with malloc()...\n", bytes);
  char *buffer = (char *)malloc(bytes);
  if (buffer == NULL) {
    fprintf(stderr, "malloc() failed.\n");
    exit(EXIT_FAILURE);
  }
  printf("Counting non-null bytes...\n");
  int nonZeroBytes = 0;
  for (size_t i = 0; i < bytes; i++)
    if (buffer[i] != 0)
      nonZeroBytes++;
  printf("%d bytes contained something other than zero.\n", nonZeroBytes);
  printf("Freeing the allocated space...\n");
  free(buffer);
}


x86 assembly (clang, -O0):
<mallocExample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a9 <mallocExample+0x59>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1208 <mallocExample+0xb8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11f2 <mallocExample+0xa2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f7 <mallocExample+0xa7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c6 <mallocExample+0x76>
mov    -0x14(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mallocExample>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     134d <mallocExample+0x1ed>
mov    %rax,%rbx
lea    0xeed(%rip),%rdi
call   1040 <puts@plt>
test   %r14,%r14
je     1310 <mallocExample+0x1b0>
cmp    $0x8,%r14
jae    11b2 <mallocExample+0x52>
xor    %eax,%eax
xor    %ebp,%ebp
jmp    1300 <mallocExample+0x1a0>
mov    %r14,%rax
and    $0xfffffffffffffff8,%rax
lea    -0x8(%rax),%rdx
mov    %rdx,%rcx
shr    $0x3,%rcx
add    $0x1,%rcx
test   %rdx,%rdx
je     1338 <mallocExample+0x1d8>
mov    %rcx,%rsi
and    $0xfffffffffffffffe,%rsi
pxor   %xmm2,%xmm2
xor    %edx,%edx
pcmpeqd %xmm3,%xmm3
movdqa 0xe16(%rip),%xmm4
pxor   %xmm0,%xmm0
pxor   %xmm1,%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movd   (%rbx,%rdx,1),%xmm6
movd   0x4(%rbx,%rdx,1),%xmm5
pcmpeqb %xmm2,%xmm6
pxor   %xmm3,%xmm6
punpcklbw %xmm6,%xmm6
punpcklwd %xmm6,%xmm6
pand   %xmm4,%xmm6
paddd  %xmm0,%xmm6
pcmpeqb %xmm2,%xmm5
pxor   %xmm3,%xmm5
punpcklbw %xmm5,%xmm5
punpcklwd %xmm5,%xmm5
pand   %xmm4,%xmm5
paddd  %xmm1,%xmm5
movd   0x8(%rbx,%rdx,1),%xmm0
movd   0xc(%rbx,%rdx,1),%xmm1
pcmpeqb %xmm2,%xmm0
pxor   %xmm3,%xmm0
punpcklbw %xmm0,%xmm0
punpcklwd %xmm0,%xmm0
pand   %xmm4,%xmm0
paddd  %xmm6,%xmm0
pcmpeqb %xmm2,%xmm1
pxor   %xmm3,%xmm1
punpcklbw %xmm1,%xmm1
punpcklwd %xmm1,%xmm1
pand   %xmm4,%xmm1
paddd  %xmm5,%xmm1
add    $0x10,%rdx
add    $0xfffffffffffffffe,%rsi
jne    1200 <mallocExample+0xa0>
test   $0x1,%cl
je     12d5 <mallocExample+0x175>
movd   (%rbx,%rdx,1),%xmm2
movd   0x4(%rbx,%rdx,1),%xmm3
pxor   %xmm4,%xmm4
pcmpeqb %xmm4,%xmm2
pcmpeqd %xmm5,%xmm5
pxor   %xmm5,%xmm2
punpcklbw %xmm2,%xmm2
punpcklwd %xmm2,%xmm2
movdqa 0xd4b(%rip),%xmm6
pand   %xmm6,%xmm2
paddd  %xmm2,%xmm0
pcmpeqb %xmm4,%xmm3
pxor   %xmm5,%xmm3
punpcklbw %xmm3,%xmm3
punpcklwd %xmm3,%xmm3
pand   %xmm6,%xmm3
paddd  %xmm3,%xmm1
paddd  %xmm1,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%ebp
cmp    %r14,%rax
je     1310 <mallocExample+0x1b0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x1,(%rbx,%rax,1)
sbb    $0xffffffff,%ebp
add    $0x1,%rax
cmp    %rax,%r14
jne    1300 <mallocExample+0x1a0>
lea    0xd3c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd76(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <free@plt>
pxor   %xmm0,%xmm0
xor    %edx,%edx
pxor   %xmm1,%xmm1
test   $0x1,%cl
jne    128a <mallocExample+0x12a>
jmp    12d5 <mallocExample+0x175>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
lea    0xce4(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"Allocating %zu bytes of memory with malloc()...\0A\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"malloc() failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"Counting non-null bytes...\0A\00"
@.str.3 = [47 x i8] c"%d bytes contained something other than zero.\0A\00"
@.str.4 = [32 x i8] c"Freeing the allocated space...\0A\00"
define void @mallocExample(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i64 %6)
%8 = load i64, i64* %2
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
store i64 0, i64* %5
br label %20
20: ; preds = %35, %18
%21 = load i64, i64* %5
%22 = load i64, i64* %2
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %38
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i64, i64* %5
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %34
34: ; preds = %31, %24
br label %35
35: ; preds = %34
%36 = load i64, i64* %5
%37 = add i64 %36, 1
store i64 %37, i64* %5
br label %20
38: ; preds = %20
%39 = load i32, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%42 = load i8*, i8** %3
%43 = call i32 @free(i8* %42)
ret void
}

LLVM IR (-O2):
@.str = [49 x i8] c"Allocating %zu bytes of memory with malloc()...\0A\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"malloc() failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [47 x i8] c"%d bytes contained something other than zero.\0A\00"
@str = [27 x i8] c"Counting non-null bytes...\00"
@str.5 = [31 x i8] c"Freeing the allocated space...\00"
define void @mallocExample(i64 %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i64 %0)
%3 = tail call i64 @malloc(i64 %0)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%13 = icmp eq i64 %0, 0
br i1 %13, label %87, label %14
14: ; preds = %11
%15 = icmp ult i64 %0, 8
br i1 %15, label %84, label %16
16: ; preds = %14
%17 = and i64 %0, -8
%18 = add i64 %17, -8
%19 = lshr exact i64 %18, 3
%20 = add nuw nsw i64 %19, 1
%21 = and i64 %20, 1
%22 = icmp eq i64 %18, 0
br i1 %22, label %58, label %23
23: ; preds = %16
%24 = and i64 %20, 4611686018427387902
br label %25
25: ; preds = %25, %23
%26 = phi i64 [ 0, %23 ], [ %55, %25 ]
%27 = phi <4 x i32> [ zeroinitializer, %23 ], [ %53, %25 ]
%28 = phi <4 x i32> [ zeroinitializer, %23 ], [ %54, %25 ]
%29 = phi i64 [ 0, %23 ], [ %56, %25 ]
%30 = getelementptr inbounds i8, i8* %4, i64 %26
%31 = bitcast i8* %30 to <4 x i8>*
%32 = load <4 x i8>, <4 x i8>* %31
%33 = getelementptr inbounds i8, i8* %30, i64 4
%34 = bitcast i8* %33 to <4 x i8>*
%35 = load <4 x i8>, <4 x i8>* %34
%36 = icmp ne <4 x i8> %32, zeroinitializer
%37 = icmp ne <4 x i8> %35, zeroinitializer
%38 = zext <4 x i1> %36 to <4 x i32>
%39 = zext <4 x i1> %37 to <4 x i32>
%40 = add <4 x i32> %27, %38
%41 = add <4 x i32> %28, %39
%42 = or i64 %26, 8
%43 = getelementptr inbounds i8, i8* %4, i64 %42
%44 = bitcast i8* %43 to <4 x i8>*
%45 = load <4 x i8>, <4 x i8>* %44
%46 = getelementptr inbounds i8, i8* %43, i64 4
%47 = bitcast i8* %46 to <4 x i8>*
%48 = load <4 x i8>, <4 x i8>* %47
%49 = icmp ne <4 x i8> %45, zeroinitializer
%50 = icmp ne <4 x i8> %48, zeroinitializer
%51 = zext <4 x i1> %49 to <4 x i32>
%52 = zext <4 x i1> %50 to <4 x i32>
%53 = add <4 x i32> %40, %51
%54 = add <4 x i32> %41, %52
%55 = add nuw i64 %26, 16
%56 = add i64 %29, 2
%57 = icmp eq i64 %56, %24
br i1 %57, label %58, label %25
58: ; preds = %25, %16
%59 = phi <4 x i32> [ undef, %16 ], [ %53, %25 ]
%60 = phi <4 x i32> [ undef, %16 ], [ %54, %25 ]
%61 = phi i64 [ 0, %16 ], [ %55, %25 ]
%62 = phi <4 x i32> [ zeroinitializer, %16 ], [ %53, %25 ]
%63 = phi <4 x i32> [ zeroinitializer, %16 ], [ %54, %25 ]
%64 = icmp eq i64 %21, 0
br i1 %64, label %78, label %65
65: ; preds = %58
%66 = getelementptr inbounds i8, i8* %4, i64 %61
%67 = bitcast i8* %66 to <4 x i8>*
%68 = load <4 x i8>, <4 x i8>* %67
%69 = getelementptr inbounds i8, i8* %66, i64 4
%70 = bitcast i8* %69 to <4 x i8>*
%71 = load <4 x i8>, <4 x i8>* %70
%72 = icmp ne <4 x i8> %68, zeroinitializer
%73 = icmp ne <4 x i8> %71, zeroinitializer
%74 = zext <4 x i1> %72 to <4 x i32>
%75 = zext <4 x i1> %73 to <4 x i32>
%76 = add <4 x i32> %62, %74
%77 = add <4 x i32> %63, %75
br label %78
78: ; preds = %58, %65
%79 = phi <4 x i32> [ %59, %58 ], [ %76, %65 ]
%80 = phi <4 x i32> [ %60, %58 ], [ %77, %65 ]
%81 = add <4 x i32> %80, %79
%82 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %81)
%83 = icmp eq i64 %17, %0
br i1 %83, label %87, label %84
84: ; preds = %14, %78
%85 = phi i64 [ 0, %14 ], [ %17, %78 ]
%86 = phi i32 [ 0, %14 ], [ %82, %78 ]
br label %92
87: ; preds = %92, %78, %11
%88 = phi i32 [ 0, %11 ], [ %82, %78 ], [ %99, %92 ]
%89 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %88)
%90 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
%91 = tail call i32 @free(i8* %4)
ret void
92: ; preds = %84, %92
%93 = phi i64 [ %100, %92 ], [ %85, %84 ]
%94 = phi i32 [ %99, %92 ], [ %86, %84 ]
%95 = getelementptr inbounds i8, i8* %4, i64 %93
%96 = load i8, i8* %95
%97 = icmp ne i8 %96, 0
%98 = zext i1 %97 to i32
%99 = add nuw nsw i32 %94, %98
%100 = add nuw i64 %93, 1
%101 = icmp eq i64 %100, %0
br i1 %101, label %87, label %92
}