#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 O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ close(int);
int dmabuf_heap_alloc(int, int, int, int *);
int dmabuf_heap_alloc_fdflags(int, int, int, int /*<<< orphan*/, int *);
int dmabuf_heap_open(char *);
int /*<<< orphan*/ printf(char *);
int test_alloc_errors(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 expected error cases:  ");
  ret = dmabuf_heap_alloc(0, (1024 * 1024), 0x111111, &dmabuf_fd);
  if (!ret) {
    printf("FAIL (Did not see expected error (invalid fd)!)\n");
    ret = -1;
    goto out;
  }
  ret = dmabuf_heap_alloc(heap_fd, (1024 * 1024), 0x111111, &dmabuf_fd);
  if (!ret) {
    printf("FAIL (Did not see expected error (invalid heap flags)!)\n");
    ret = -1;
    goto out;
  }
  ret = dmabuf_heap_alloc_fdflags(heap_fd, (1024 * 1024), ~(O_RDWR | O_CLOEXEC),
                                  0, &dmabuf_fd);
  if (!ret) {
    printf("FAIL (Did not see expected error (invalid fd flags)!)\n");
    ret = -1;
    goto out;
  }
  printf("OK\n");
  ret = 0;
out:
  if (dmabuf_fd >= 0)
    close(dmabuf_fd);
  if (heap_fd >= 0)
    close(heap_fd);
  return ret;
}


x86 assembly (clang, -O0):
<test_alloc_errors>:
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_errors+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <test_alloc_errors+0x147>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
mov    $0x100000,%esi
mov    $0x111111,%edx
lea    -0x18(%rbp),%rcx
call   1070 <dmabuf_heap_alloc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11d2 <test_alloc_errors+0x82>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    126d <test_alloc_errors+0x11d>
mov    -0x14(%rbp),%edi
mov    $0x100000,%esi
mov    $0x111111,%edx
lea    -0x18(%rbp),%rcx
call   1070 <dmabuf_heap_alloc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    120d <test_alloc_errors+0xbd>
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    126d <test_alloc_errors+0x11d>
mov    -0x14(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
xor    $0xffffffff,%edx
mov    $0x100000,%esi
xor    %ecx,%ecx
lea    -0x18(%rbp),%r8
call   1060 <dmabuf_heap_alloc_fdflags@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    125a <test_alloc_errors+0x10a>
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    126d <test_alloc_errors+0x11d>
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     127f <test_alloc_errors+0x12f>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jl     1291 <test_alloc_errors+0x141>
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_errors>:
push   %rbp
push   %rbx
push   %rax
movl   $0xffffffff,0x4(%rsp)
ff
call   1030 <dmabuf_heap_open@plt>
test   %eax,%eax
js     1204 <test_alloc_errors+0xa4>
mov    %eax,%ebx
lea    0xe7f(%rip),%rdi
call   1050 <printf@plt>
lea    0x4(%rsp),%rcx
xor    %edi,%edi
mov    $0x100000,%esi
mov    $0x111111,%edx
call   1080 <dmabuf_heap_alloc@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
je     120b <test_alloc_errors+0xab>
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x100000,%esi
mov    $0x111111,%edx
call   1080 <dmabuf_heap_alloc@plt>
test   %eax,%eax
je     1214 <test_alloc_errors+0xb4>
mov    0x2e22(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
not    %edx
lea    0x4(%rsp),%r8
mov    %ebx,%edi
mov    $0x100000,%esi
xor    %ecx,%ecx
call   1070 <dmabuf_heap_alloc_fdflags@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
lea    0xe95(%rip),%rax
lea    0xec4(%rip),%rdi
cmove  %rax,%rdi
neg    %ebp
jmp    121b <test_alloc_errors+0xbb>
mov    $0xffffffff,%ebp
jmp    1234 <test_alloc_errors+0xd4>
lea    0xe10(%rip),%rdi
jmp    121b <test_alloc_errors+0xbb>
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
js     122d <test_alloc_errors+0xcd>
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c" Testing expected error cases: \00"
@.str.1 = [49 x i8] c"FAIL (Did not see expected error (invalid fd)!)\0A\00"
@.str.2 = [57 x i8] c"FAIL (Did not see expected error (invalid heap flags)!)\0A\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str.3 = [55 x i8] c"FAIL (Did not see expected error (invalid fd flags)!)\0A\00"
@.str.4 = [4 x i8] c"OK\0A\00"
define i32 @test_alloc_errors(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 %53
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @dmabuf_heap_alloc(i32 0, i32 1048576, i32 1118481, i32* %5)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %39
19: ; preds = %12
%20 = load i32, i32* %4
%21 = call i32 @dmabuf_heap_alloc(i32 %20, i32 1048576, i32 1118481, i32* %5)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %39
26: ; preds = %19
%27 = load i32, i32* %4
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CLOEXEC
%30 = or i32 %28, %29
%31 = xor i32 %30, -1
%32 = call i32 @dmabuf_heap_alloc_fdflags(i32 %27, i32 1048576, i32 %31, i32 0, i32* %5)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %26
%36 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %39
37: ; preds = %26
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %6
br label %39
39: ; preds = %37, %35, %24, %17
%40 = load i32, i32* %5
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %4
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %45
%52 = load i32, i32* %6
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %11
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@.str = [34 x i8] c" Testing expected error cases: \00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@str = [48 x i8] c"FAIL (Did not see expected error (invalid fd)!)\00"
@str.5 = [56 x i8] c"FAIL (Did not see expected error (invalid heap flags)!)\00"
@str.6 = [54 x i8] c"FAIL (Did not see expected error (invalid fd flags)!)\00"
@str.7 = [3 x i8] c"OK\00"
define i32 @test_alloc_errors(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 %32, label %6
6: ; preds = %1
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @dmabuf_heap_alloc(i32 0, i32 1048576, i32 1118481, i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = call i32 @dmabuf_heap_alloc(i32 %4, i32 1048576, i32 1118481, i32* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CLOEXEC
%16 = or i32 %15, %14
%17 = xor i32 %16, -1
%18 = call i32 @dmabuf_heap_alloc_fdflags(i32 %4, i32 1048576, i32 %17, i32 0, i32* %2)
%19 = icmp eq i32 %18, 0
%20 = select i1 %19, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str.7, i64 0, i64 0)
%21 = sext i1 %19 to i32
br label %22
22: ; preds = %13, %10, %6
%23 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([56 x i8], [56 x i8]* @str.5, i64 0, i64 0), %10 ], [ %20, %13 ]
%24 = phi i32 [ -1, %6 ], [ -1, %10 ], [ %21, %13 ]
%25 = call i32 @puts(i8* dereferenceable(1) %23)
%26 = load i32, i32* %2
%27 = icmp sgt i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @close(i32 %26)
br label %30
30: ; preds = %22, %28
%31 = call i32 @close(i32 %4)
br label %32
32: ; preds = %1, %30
%33 = phi i32 [ %24, %30 ], [ -1, %1 ]
ret i32 %33
}