#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 FS460_alpha_read_finish(unsigned short *, int);
int FS460_alpha_read_is_completed(int *);
int FS460_alpha_read_start(int, int);
int FS460_get_alpha_mask(unsigned short *p_alpha_mask, int mask_size,
                         int odd_field) {
  int err;
  int completed;
  if ((mask_size > 0x10000) || !p_alpha_mask)
    return 0x1000;
  err = FS460_alpha_read_start(mask_size, odd_field);
  if (!err) {
    while ((0 == (err = FS460_alpha_read_is_completed(&completed))) &&
           !completed)
      ;
    if (!err) {
      err = FS460_alpha_read_finish(p_alpha_mask, mask_size);
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<FS460_get_alpha_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x10000,-0x14(%rbp)
jg     115a <FS460_get_alpha_mask+0x2a>
cmpq   $0x0,-0x10(%rbp)
jne    1166 <FS460_get_alpha_mask+0x36>
movl   $0x1000,-0x4(%rbp)
jmp    11e5 <FS460_get_alpha_mask+0xb5>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <FS460_alpha_read_start@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11df <FS460_get_alpha_mask+0xaf>
jmp    1183 <FS460_get_alpha_mask+0x53>
lea    -0x20(%rbp),%rdi
call   1050 <FS460_alpha_read_is_completed@plt>
mov    %eax,%edx
mov    %edx,-0x1c(%rbp)
xor    %eax,%eax
xor    %ecx,%ecx
cmp    %edx,%ecx
mov    %al,-0x21(%rbp)
jne    11ac <FS460_get_alpha_mask+0x7c>
cmpl   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11bc <FS460_get_alpha_mask+0x8c>
jmp    11c1 <FS460_get_alpha_mask+0x91>
jmp    1183 <FS460_get_alpha_mask+0x53>
cmpl   $0x0,-0x1c(%rbp)
jne    11da <FS460_get_alpha_mask+0xaa>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <FS460_alpha_read_finish@plt>
mov    %eax,-0x1c(%rbp)
jmp    11df <FS460_get_alpha_mask+0xaf>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FS460_get_alpha_mask>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x1000,%eax
test   %rdi,%rdi
je     118e <FS460_get_alpha_mask+0x5e>
mov    %esi,%ebp
cmp    $0x10000,%esi
jg     118e <FS460_get_alpha_mask+0x5e>
mov    %rdi,%r14
mov    %ebp,%edi
mov    %edx,%esi
call   1030 <FS460_alpha_read_start@plt>
test   %eax,%eax
jne    118e <FS460_get_alpha_mask+0x5e>
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <FS460_alpha_read_is_completed@plt>
mov    0xc(%rsp),%ecx
or     %eax,%ecx
je     1170 <FS460_get_alpha_mask+0x40>
test   %eax,%eax
jne    118e <FS460_get_alpha_mask+0x5e>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <FS460_alpha_read_finish@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @FS460_get_alpha_mask(i16* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i16* %0, i16** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 65536
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i16*, i16** %5
%14 = icmp ne i16* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12, %3
store i32 4096, i32* %4
br label %43
16: ; preds = %12
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = call i32 @FS460_alpha_read_start(i32 %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %41, label %22
22: ; preds = %16
br label %23
23: ; preds = %32, %22
%24 = call i32 @FS460_alpha_read_is_completed(i32* %9)
store i32 %24, i32* %8
%25 = icmp eq i32 0, %24
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* %9
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br label %30
30: ; preds = %26, %23
%31 = phi i1 [ false, %23 ], [ %29, %26 ]
br i1 %31, label %32, label %33
32: ; preds = %30
br label %23
33: ; preds = %30
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i16*, i16** %5
%38 = load i32, i32* %6
%39 = call i32 @FS460_alpha_read_finish(i16* %37, i32 %38)
store i32 %39, i32* %8
br label %40
40: ; preds = %36, %33
br label %41
41: ; preds = %40, %16
%42 = load i32, i32* %8
store i32 %42, i32* %4
br label %43
43: ; preds = %41, %15
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @FS460_get_alpha_mask(i16* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp slt i32 %1, 65537
%7 = icmp ne i16* %0, null
%8 = and i1 %7, %6
br i1 %8, label %9, label %21
9: ; preds = %3
%10 = tail call i32 @FS460_alpha_read_start(i32 %1, i32 %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9, %12
%13 = call i32 @FS460_alpha_read_is_completed(i32* %4)
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 0
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %12, label %18
18: ; preds = %12
br i1 %14, label %19, label %21
19: ; preds = %18
%20 = call i32 @FS460_alpha_read_finish(i16* %0, i32 %1)
br label %21
21: ; preds = %9, %19, %18, %3
%22 = phi i32 [ 4096, %3 ], [ %10, %9 ], [ %13, %18 ], [ %20, %19 ]
ret i32 %22
}