#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*/ close(int);
int dmabuf_heap_alloc_newer(int, int, int /*<<< orphan*/, int *);
int dmabuf_heap_alloc_older(int, int, int /*<<< orphan*/, int *);
int dmabuf_heap_open(char *);
int /*<<< orphan*/ printf(char *);
int test_alloc_compat(char *heap_name) {
  int heap_fd = -1, dmabuf_fd = -1;
  int ret;
  heap_fd = dmabuf_heap_open(heap_name);
  if (heap_fd < 0)
    return -1;
  printf("  Testing (theoretical)older alloc compat:  ");
  ret = dmabuf_heap_alloc_older(heap_fd, (1024 * 1024), 0, &dmabuf_fd);
  if (ret) {
    printf("FAIL (Older compat allocation failed!)\n");
    ret = -1;
    goto out;
  }
  close(dmabuf_fd);
  printf("OK\n");
  printf("  Testing (theoretical)newer alloc compat:  ");
  ret = dmabuf_heap_alloc_newer(heap_fd, (1024 * 1024), 0, &dmabuf_fd);
  if (ret) {
    printf("FAIL (Newer compat allocation failed!)\n");
    ret = -1;
    goto out;
  }
  printf("OK\n");
out:
  if (dmabuf_fd >= 0)
    close(dmabuf_fd);
  if (heap_fd >= 0)
    close(heap_fd);
  return ret;
}


x86 assembly (clang, -O0):
<test_alloc_compat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <dmabuf_heap_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118c <test_alloc_compat+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <test_alloc_compat+0x10e>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x100000,%esi
xor    %edx,%edx
lea    -0x18(%rbp),%rcx
call   1070 <dmabuf_heap_alloc_older@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11d0 <test_alloc_compat+0x80>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1234 <test_alloc_compat+0xe4>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x100000,%esi
xor    %edx,%edx
lea    -0x18(%rbp),%rcx
call   1060 <dmabuf_heap_alloc_newer@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1228 <test_alloc_compat+0xd8>
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1234 <test_alloc_compat+0xe4>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jl     1246 <test_alloc_compat+0xf6>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jl     1258 <test_alloc_compat+0x108>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_alloc_compat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
call   1030 <dmabuf_heap_open@plt>
test   %eax,%eax
js     11ac <test_alloc_compat+0x4c>
mov    %eax,%ebx
lea    0xe7e(%rip),%rdi
call   1050 <printf@plt>
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x100000,%esi
xor    %edx,%edx
call   1080 <dmabuf_heap_alloc_older@plt>
test   %eax,%eax
je     11b3 <test_alloc_compat+0x53>
mov    $0xffffffff,%ebp
lea    0xeda(%rip),%rdi
jmp    11ff <test_alloc_compat+0x9f>
mov    $0xffffffff,%ebp
jmp    1218 <test_alloc_compat+0xb8>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0xe97(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x100000,%esi
xor    %edx,%edx
call   1070 <dmabuf_heap_alloc_newer@plt>
xor    %ebp,%ebp
test   %eax,%eax
setne  %bpl
lea    0xe64(%rip),%rdi
cmove  %r14,%rdi
neg    %ebp
call   1040 <puts@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
js     1211 <test_alloc_compat+0xb1>
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c" Testing (theoretical)older alloc compat: \00"
@.str.1 = [40 x i8] c"FAIL (Older compat allocation failed!)\0A\00"
@.str.2 = [4 x i8] c"OK\0A\00"
@.str.3 = [45 x i8] c" Testing (theoretical)newer alloc compat: \00"
@.str.4 = [40 x i8] c"FAIL (Newer compat allocation failed!)\0A\00"
define i32 @test_alloc_compat(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
store i32 -1, i32* %5
%7 = load i8*, i8** %3
%8 = call i32 @dmabuf_heap_open(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %47
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %4
%15 = call i32 @dmabuf_heap_alloc_older(i32 %14, i32 1048576, i32 0, i32* %5)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %33
20: ; preds = %12
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = call i32 @dmabuf_heap_alloc_newer(i32 %25, i32 1048576, i32 0, i32* %5)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %6
br label %33
31: ; preds = %20
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %29, %18
%34 = load i32, i32* %5
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %4
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %6
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %11
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [45 x i8] c" Testing (theoretical)older alloc compat: \00"
@.str.3 = [45 x i8] c" Testing (theoretical)newer alloc compat: \00"
@str.5 = [3 x i8] c"OK\00"
@str.6 = [39 x i8] c"FAIL (Newer compat allocation failed!)\00"
@str.7 = [39 x i8] c"FAIL (Older compat allocation failed!)\00"
define i32 @test_alloc_compat(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%4 = tail call i32 @dmabuf_heap_open(i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %30, label %6
6: ; preds = %1
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @dmabuf_heap_alloc_older(i32 %4, i32 1048576, i32 0, i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %6
%11 = load i32, i32* %2
%12 = call i32 @close(i32 %11)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.5, i64 0, i64 0))
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @dmabuf_heap_alloc_newer(i32 %4, i32 1048576, i32 0, i32* %2)
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str.5, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @str.6, i64 0, i64 0)
%18 = xor i1 %16, true
%19 = sext i1 %18 to i32
br label %20
20: ; preds = %10, %6
%21 = phi i8* [ getelementptr inbounds ([39 x i8], [39 x i8]* @str.7, i64 0, i64 0), %6 ], [ %17, %10 ]
%22 = phi i32 [ -1, %6 ], [ %19, %10 ]
%23 = call i32 @puts(i8* dereferenceable(1) %21)
%24 = load i32, i32* %2
%25 = icmp sgt i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @close(i32 %24)
br label %28
28: ; preds = %20, %26
%29 = call i32 @close(i32 %4)
br label %30
30: ; preds = %1, %28
%31 = phi i32 [ %22, %28 ], [ -1, %1 ]
ret i32 %31
}