#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 */
typedef int /*<<< orphan*/ out;
typedef int /*<<< orphan*/ in;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ TXboxAdpcmDecoder_Decode_Memory(void *, int, void *, int);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int TXboxAdpcmDecoder_Decode(FILE *ASource, FILE *ADest, int SourcePos,
                             int SourceSize, int FChannels) {
  int DestSize;
  unsigned char *in = (unsigned char *)malloc(36 * FChannels),
                *out = (unsigned char *)malloc(130 * FChannels);
  if (FChannels <= 0)
    return (0);
  if (SourcePos >= 0) {
    if (fseek(ASource, SourcePos, SEEK_SET) < 0)
      return (0);
  }
  if (SourceSize > 0) {
    SourceSize -= (SourceSize % 36);
    SourceSize = (SourceSize / 36) / FChannels;
  }
  for (DestSize = 0; SourceSize; SourceSize--) {
    if (!fread(in, sizeof(in), 1, ASource))
      break;
    DestSize += TXboxAdpcmDecoder_Decode_Memory((void *)in, sizeof(in),
                                                (void *)out, FChannels);
    if (!fwrite(out, sizeof(out), 1, ADest))
      break;
  }
  free(in);
  free(out);
  return (DestSize);
}


x86 assembly (clang, -O0):
<TXboxAdpcmDecoder_Decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
imul   $0x24,-0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
imul   $0x82,-0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11ad <TXboxAdpcmDecoder_Decode+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    12be <TXboxAdpcmDecoder_Decode+0x15e>
cmpl   $0x0,-0x1c(%rbp)
jl     11e7 <TXboxAdpcmDecoder_Decode+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
cmp    $0x0,%rax
jge    11e2 <TXboxAdpcmDecoder_Decode+0x82>
movl   $0x0,-0x4(%rbp)
jmp    12be <TXboxAdpcmDecoder_Decode+0x15e>
jmp    11e7 <TXboxAdpcmDecoder_Decode+0x87>
cmpl   $0x0,-0x20(%rbp)
jle    1216 <TXboxAdpcmDecoder_Decode+0xb6>
mov    -0x20(%rbp),%eax
mov    $0x24,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x24,%ecx
cltd
idiv   %ecx
cltd
idivl  -0x24(%rbp)
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12a6 <TXboxAdpcmDecoder_Decode+0x146>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x8,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x0,%eax
jne    124c <TXboxAdpcmDecoder_Decode+0xec>
jmp    12a6 <TXboxAdpcmDecoder_Decode+0x146>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x8,%esi
call   1060 <TXboxAdpcmDecoder_Decode_Memory@plt>
mov    %rax,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x8,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x0,%eax
jne    1293 <TXboxAdpcmDecoder_Decode+0x133>
jmp    12a6 <TXboxAdpcmDecoder_Decode+0x146>
jmp    1298 <TXboxAdpcmDecoder_Decode+0x138>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    121d <TXboxAdpcmDecoder_Decode+0xbd>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TXboxAdpcmDecoder_Decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r14d
mov    %ecx,%ebp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
lea    0x0(,%r14,4),%eax
lea    (%rax,%rax,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %r14d,%eax
shl    $0x7,%eax
lea    (%rax,%r14,2),%edi
call   1050 <malloc@plt>
xor    %r15d,%r15d
test   %r14d,%r14d
jle    1269 <TXboxAdpcmDecoder_Decode+0x109>
mov    %rax,%rbx
mov    0xc(%rsp),%esi
test   %esi,%esi
js     11d2 <TXboxAdpcmDecoder_Decode+0x72>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
call   1070 <fseek@plt>
test   %rax,%rax
js     1269 <TXboxAdpcmDecoder_Decode+0x109>
test   %ebp,%ebp
jle    11ea <TXboxAdpcmDecoder_Decode+0x8a>
mov    %ebp,%eax
imul   $0x38e38e39,%rax,%rax
shr    $0x23,%rax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebp
test   %ebp,%ebp
je     1256 <TXboxAdpcmDecoder_Decode+0xf6>
add    $0xffffffff,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1259 <TXboxAdpcmDecoder_Decode+0xf9>
mov    %r13,%rdi
mov    $0x8,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1060 <TXboxAdpcmDecoder_Decode_Memory@plt>
add    %eax,%r15d
mov    %rbx,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    0x10(%rsp),%rcx
call   1080 <fwrite@plt>
sub    $0x1,%ebp
setb   %cl
test   %eax,%eax
je     1259 <TXboxAdpcmDecoder_Decode+0xf9>
test   %cl,%cl
je     1200 <TXboxAdpcmDecoder_Decode+0xa0>
jmp    1259 <TXboxAdpcmDecoder_Decode+0xf9>
xor    %r15d,%r15d
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @TXboxAdpcmDecoder_Decode(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i32, i32* %11
%16 = mul nsw i32 36, %15
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %13
%19 = load i32, i32* %11
%20 = mul nsw i32 130, %19
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %14
%23 = load i32, i32* %11
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %5
store i32 0, i32* %6
br label %83
26: ; preds = %5
%27 = load i32, i32* %9
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32*, i32** %7
%31 = load i32, i32* %9
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @fseek(i32* %30, i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 0, i32* %6
br label %83
36: ; preds = %29
br label %37
37: ; preds = %36, %26
%38 = load i32, i32* %10
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %37
%41 = load i32, i32* %10
%42 = srem i32 %41, 36
%43 = load i32, i32* %10
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = sdiv i32 %45, 36
%47 = load i32, i32* %11
%48 = sdiv i32 %46, %47
store i32 %48, i32* %10
br label %49
49: ; preds = %40, %37
store i32 0, i32* %12
br label %50
50: ; preds = %74, %49
%51 = load i32, i32* %10
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %77
53: ; preds = %50
%54 = load i8*, i8** %13
%55 = load i32*, i32** %7
%56 = call i32 @fread(i8* %54, i32 8, i32 1, i32* %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %53
br label %77
59: ; preds = %53
%60 = load i8*, i8** %13
%61 = load i8*, i8** %14
%62 = load i32, i32* %11
%63 = call i64 @TXboxAdpcmDecoder_Decode_Memory(i8* %60, i32 8, i8* %61, i32 %62)
%64 = load i32, i32* %12
%65 = sext i32 %64 to i64
%66 = add nsw i64 %65, %63
%67 = trunc i64 %66 to i32
store i32 %67, i32* %12
%68 = load i8*, i8** %14
%69 = load i32*, i32** %8
%70 = call i32 @fwrite(i8* %68, i32 8, i32 1, i32* %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %73, label %72
72: ; preds = %59
br label %77
73: ; preds = %59
br label %74
74: ; preds = %73
%75 = load i32, i32* %10
%76 = add nsw i32 %75, -1
store i32 %76, i32* %10
br label %50
77: ; preds = %72, %58, %50
%78 = load i8*, i8** %13
%79 = call i32 @free(i8* %78)
%80 = load i8*, i8** %14
%81 = call i32 @free(i8* %80)
%82 = load i32, i32* %12
store i32 %82, i32* %6
br label %83
83: ; preds = %77, %35, %25
%84 = load i32, i32* %6
ret i32 %84
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @TXboxAdpcmDecoder_Decode(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = mul nsw i32 %4, 36
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = mul nsw i32 %4, 130
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp slt i32 %4, 1
br i1 %12, label %45, label %13
13: ; preds = %5
%14 = icmp sgt i32 %2, -1
br i1 %14, label %15, label %19
15: ; preds = %13
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @fseek(i32* %0, i32 %2, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %45, label %19
19: ; preds = %15, %13
%20 = icmp sgt i32 %3, 0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = udiv i32 %3, 36
%23 = udiv i32 %22, %4
br label %24
24: ; preds = %21, %19
%25 = phi i32 [ %23, %21 ], [ %3, %19 ]
%26 = icmp eq i32 %25, 0
br i1 %26, label %41, label %27
27: ; preds = %24, %32
%28 = phi i32 [ %38, %32 ], [ %25, %24 ]
%29 = phi i32 [ %35, %32 ], [ 0, %24 ]
%30 = tail call i32 @fread(i8* %8, i32 8, i32 1, i32* %0)
%31 = icmp eq i32 %30, 0
br i1 %31, label %41, label %32
32: ; preds = %27
%33 = tail call i64 @TXboxAdpcmDecoder_Decode_Memory(i8* %8, i32 8, i8* %11, i32 %4)
%34 = trunc i64 %33 to i32
%35 = add i32 %29, %34
%36 = tail call i32 @fwrite(i8* %11, i32 8, i32 1, i32* %1)
%37 = icmp eq i32 %36, 0
%38 = add nsw i32 %28, -1
%39 = icmp eq i32 %38, 0
%40 = select i1 %37, i1 true, i1 %39
br i1 %40, label %41, label %27
41: ; preds = %27, %32, %24
%42 = phi i32 [ 0, %24 ], [ %35, %32 ], [ %29, %27 ]
%43 = tail call i32 @free(i8* %8)
%44 = tail call i32 @free(i8* %11)
br label %45
45: ; preds = %15, %5, %41
%46 = phi i32 [ %42, %41 ], [ 0, %5 ], [ 0, %15 ]
ret i32 %46
}