#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 */
unsigned int acclExchange(int, int, char *, unsigned int *, char **);
int /*<<< orphan*/ free(void *);
unsigned int getpagesize();
int /*<<< orphan*/ memcpy(void *, void *, unsigned int);
scalar_t__ posix_memalign(void **, unsigned int, unsigned int);
void *DIABLO_Mobility_DownloadByIndex(unsigned int block_index,
                                      unsigned int *block_length) {
  ;
  unsigned int page_size, buffer_size, ret, full_pages;
  void *temp_buffer, *out_buffer;
  page_size = getpagesize();
  buffer_size = page_size * 2;
  ret = acclExchange(20, (int)sizeof(unsigned int), (char *)&block_index,
                     &buffer_size, (char **)&temp_buffer);
  if (0 == ret) {
    ;
    if (buffer_size > 0) {
      full_pages =
          buffer_size / page_size + (buffer_size % page_size > 0 ? 1 : 0);
      ;
      if (0 != posix_memalign(&out_buffer, page_size, full_pages * page_size)) {
        free(temp_buffer);
        return 0;
      }
      memcpy(block_length, &buffer_size, sizeof(unsigned int));
      memcpy(out_buffer, temp_buffer, buffer_size);
      ;
    } else {
      free(temp_buffer);
      return 0;
    }
  } else {
    free(temp_buffer);
    return 0;
  }
  free(temp_buffer);
  return out_buffer;
}


x86 assembly (clang, -O0):
<DIABLO_Mobility_DownloadByIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
lea    -0xc(%rbp),%rdx
mov    $0x14,%edi
mov    $0x4,%esi
lea    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1050 <acclExchange@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jne    1248 <DIABLO_Mobility_DownloadByIndex+0xf8>
cmpl   $0x0,-0x20(%rbp)
jbe    122d <DIABLO_Mobility_DownloadByIndex+0xdd>
mov    -0x20(%rbp),%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %edx,%esi
xor    %ecx,%ecx
mov    $0x1,%edx
cmp    $0x0,%esi
cmova  %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%edx
imul   -0x1c(%rbp),%edx
lea    -0x38(%rbp),%rdi
call   1070 <posix_memalign@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1206 <DIABLO_Mobility_DownloadByIndex+0xb6>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <DIABLO_Mobility_DownloadByIndex+0x11f>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <memcpy@plt>
jmp    1243 <DIABLO_Mobility_DownloadByIndex+0xf3>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <DIABLO_Mobility_DownloadByIndex+0x11f>
jmp    125e <DIABLO_Mobility_DownloadByIndex+0x10e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <DIABLO_Mobility_DownloadByIndex+0x11f>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DIABLO_Mobility_DownloadByIndex>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,0x1c(%rsp)
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,%ebx
lea    (%rbx,%rbx,1),%eax
mov    %eax,0xc(%rsp)
lea    0x1c(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    $0x14,%edi
mov    $0x4,%esi
call   1050 <acclExchange@plt>
test   %eax,%eax
je     11a4 <DIABLO_Mobility_DownloadByIndex+0x54>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1190 <DIABLO_Mobility_DownloadByIndex+0x40>
xor    %edx,%edx
div    %ebx
cmp    $0x1,%edx
sbb    $0xffffffff,%eax
mov    %ebx,%edx
imul   %eax,%edx
lea    0x20(%rsp),%rdi
mov    %ebx,%esi
call   1070 <posix_memalign@plt>
test   %rax,%rax
jne    1190 <DIABLO_Mobility_DownloadByIndex+0x40>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x20(%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
call   1040 <memcpy@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x20(%rsp),%rax
jmp    119c <DIABLO_Mobility_DownloadByIndex+0x4c>

LLVM IR (-O0):
define i8* @DIABLO_Mobility_DownloadByIndex(i32 %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 %0, i32* %4
store i32* %1, i32** %5
%12 = call i32 (...) @getpagesize()
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = mul i32 %13, 2
store i32 %14, i32* %7
%15 = bitcast i32* %4 to i8*
%16 = call i32 @acclExchange(i32 20, i32 4, i8* %15, i32* %7, i8** %10)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 0, %17
br i1 %18, label %19, label %55
19: ; preds = %2
%20 = load i32, i32* %7
%21 = icmp ugt i32 %20, 0
br i1 %21, label %22, label %51
22: ; preds = %19
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = udiv i32 %23, %24
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = urem i32 %26, %27
%29 = icmp ugt i32 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 1, i32 0
%32 = add i32 %25, %31
store i32 %32, i32* %9
%33 = load i32, i32* %6
%34 = load i32, i32* %9
%35 = load i32, i32* %6
%36 = mul i32 %34, %35
%37 = call i64 @posix_memalign(i8** %11, i32 %33, i32 %36)
%38 = icmp ne i64 0, %37
br i1 %38, label %39, label %42
39: ; preds = %22
%40 = load i8*, i8** %10
%41 = call i32 @free(i8* %40)
store i8* null, i8** %3
br label %62
42: ; preds = %22
%43 = load i32*, i32** %5
%44 = bitcast i32* %43 to i8*
%45 = bitcast i32* %7 to i8*
%46 = call i32 @memcpy(i8* %44, i8* %45, i32 4)
%47 = load i8*, i8** %11
%48 = load i8*, i8** %10
%49 = load i32, i32* %7
%50 = call i32 @memcpy(i8* %47, i8* %48, i32 %49)
br label %54
51: ; preds = %19
%52 = load i8*, i8** %10
%53 = call i32 @free(i8* %52)
store i8* null, i8** %3
br label %62
54: ; preds = %42
br label %58
55: ; preds = %2
%56 = load i8*, i8** %10
%57 = call i32 @free(i8* %56)
store i8* null, i8** %3
br label %62
58: ; preds = %54
%59 = load i8*, i8** %10
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %11
store i8* %61, i8** %3
br label %62
62: ; preds = %58, %55, %51, %39
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
define i8* @DIABLO_Mobility_DownloadByIndex(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 %0, i32* %3
%7 = bitcast i32* %4 to i8*
%8 = bitcast i8** %5 to i8*
%9 = bitcast i8** %6 to i8*
%10 = tail call i32 (...) @getpagesize()
%11 = shl i32 %10, 1
store i32 %11, i32* %4
%12 = bitcast i32* %3 to i8*
%13 = call i32 @acclExchange(i32 20, i32 4, i8* %12, i32* %4, i8** %5)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
br i1 %17, label %40, label %18
18: ; preds = %15
%19 = udiv i32 %16, %10
%20 = urem i32 %16, %10
%21 = icmp ne i32 %20, 0
%22 = zext i1 %21 to i32
%23 = add i32 %19, %22
%24 = mul i32 %23, %10
%25 = call i64 @posix_memalign(i8** %6, i32 %10, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
br label %46
30: ; preds = %18
%31 = bitcast i32* %1 to i8*
%32 = call i32 @memcpy(i8* %31, i8* %7, i32 4)
%33 = load i8*, i8** %6
%34 = load i8*, i8** %5
%35 = load i32, i32* %4
%36 = call i32 @memcpy(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
%39 = load i8*, i8** %6
br label %46
40: ; preds = %15
%41 = load i8*, i8** %5
%42 = call i32 @free(i8* %41)
br label %46
43: ; preds = %2
%44 = load i8*, i8** %5
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %30, %43, %40, %27
%47 = phi i8* [ null, %27 ], [ %39, %30 ], [ null, %40 ], [ null, %43 ]
ret i8* %47
}