#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__ SIZE_MAX;
int /*<<< orphan*/ assert(int);
char *p_malloc(scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ test_malloc_cleanup();
int /*<<< orphan*/ test_malloc_startup();
void test_allocation_fails(void) {
  char *p;
  char *p2;
  printf("\tStart  test_allocation_fails\n");
  test_malloc_startup();
  printf("\t\tExpect: malloc to fail twice\n");
  p = p_malloc(SIZE_MAX);
  p2 = p_malloc(SIZE_MAX - 100);
  assert(p == 0);
  assert(p2 == 0);
  test_malloc_cleanup();
  printf("\tFinish test_allocation_fails\n");
}


x86 assembly (clang, -O0):
<test_allocation_fails>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <test_malloc_startup@plt>
lea    0xead(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rdi
call   1070 <p_malloc@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%rdi
sub    $0x64,%rdi
call   1070 <p_malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_allocation_fails>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <test_malloc_startup@plt>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e5e(%rip),%r14
mov    (%r14),%rdi
call   1070 <p_malloc@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
add    $0xffffffffffffff9c,%rdi
call   1070 <p_malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rbx,%rbx
sete   %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
sete   %dil
call   1060 <assert@plt>
xor    %eax,%eax
call   1040 <test_malloc_cleanup@plt>
lea    0xe7f(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"\09Start test_allocation_fails\0A\00"
@.str.1 = [32 x i8] c"\09\09Expect: malloc to fail twice\0A\00"
@SIZE_MAX = global i64 0
@.str.2 = [31 x i8] c"\09Finish test_allocation_fails\0A\00"
define void @test_allocation_fails() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @test_malloc_startup()
%5 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%6 = load i64, i64* @SIZE_MAX
%7 = call i8* @p_malloc(i64 %6)
store i8* %7, i8** %1
%8 = load i64, i64* @SIZE_MAX
%9 = sub nsw i64 %8, 100
%10 = call i8* @p_malloc(i64 %9)
store i8* %10, i8** %2
%11 = load i8*, i8** %1
%12 = icmp eq i8* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %2
%16 = icmp eq i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i32 (...) @test_malloc_cleanup()
%20 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@str = [30 x i8] c"\09Start test_allocation_fails\00"
@str.3 = [31 x i8] c"\09\09Expect: malloc to fail twice\00"
@str.4 = [30 x i8] c"\09Finish test_allocation_fails\00"
define void @test_allocation_fails() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @test_malloc_startup()
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.3, i64 0, i64 0))
%4 = load i64, i64* @SIZE_MAX
%5 = tail call i8* @p_malloc(i64 %4)
%6 = load i64, i64* @SIZE_MAX
%7 = add nsw i64 %6, -100
%8 = tail call i8* @p_malloc(i64 %7)
%9 = icmp eq i8* %5, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp eq i8* %8, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i32 (...) @test_malloc_cleanup()
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.4, i64 0, i64 0))
ret void
}