#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*/ free(void *);
void *malloc(int);
int sample_malloc_function() {
  void *p = NULL;
  void *q = NULL;
  p = malloc(10);
  if (NULL == p)
    return 1;
  q = malloc(10);
  if (NULL == q) {
    free(p);
    return 2;
  }
  free(p);
  free(q);
  return 0;
}


x86 assembly (clang, -O0):
<sample_malloc_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0xa,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    115e <sample_malloc_function+0x3e>
movl   $0x1,-0x4(%rbp)
jmp    11a6 <sample_malloc_function+0x86>
mov    $0xa,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    118d <sample_malloc_function+0x6d>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    11a6 <sample_malloc_function+0x86>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sample_malloc_function>:
push   %r14
push   %rbx
push   %rax
mov    $0xa,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     115c <sample_malloc_function+0x3c>
mov    %rax,%rbx
mov    $0xa,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     1163 <sample_malloc_function+0x43>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1168 <sample_malloc_function+0x48>
mov    $0x1,%eax
jmp    1168 <sample_malloc_function+0x48>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @sample_malloc_function() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i8* null, i8** %2
store i8* null, i8** %3
%4 = call i8* @malloc(i32 10)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* null, %5
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 1, i32* %1
br label %20
8: ; preds = %0
%9 = call i8* @malloc(i32 10)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* null, %10
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = call i32 @free(i8* %13)
store i32 2, i32* %1
br label %20
15: ; preds = %8
%16 = load i8*, i8** %2
%17 = call i32 @free(i8* %16)
%18 = load i8*, i8** %3
%19 = call i32 @free(i8* %18)
store i32 0, i32* %1
br label %20
20: ; preds = %15, %12, %7
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
define i32 @sample_malloc_function() {
%1 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%2 = icmp eq i8* %1, null
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%5 = icmp eq i8* %4, null
%6 = tail call i32 @free(i8* %1)
br i1 %5, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @free(i8* %4)
br label %9
9: ; preds = %3, %0, %7
%10 = phi i32 [ 0, %7 ], [ 1, %0 ], [ 2, %3 ]
ret i32 %10
}