#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*/ uint16_t;

/* Variables and functions */
scalar_t__ apuGetBuf();
int /*<<< orphan*/ audio_batch_cb(int /*<<< orphan*/ *, int);
void audioFrameEnd(int samples) {
  uint16_t *buffer_in = (uint16_t *)apuGetBuf();
  while (samples > 512) {
    audio_batch_cb(buffer_in, 512);
    buffer_in += 1024;
    samples -= 512;
  }
  if (samples)
    audio_batch_cb(buffer_in, samples);
}


x86 assembly (clang, -O0):
<audioFrameEnd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <apuGetBuf@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x200,-0x4(%rbp)
jle    116f <audioFrameEnd+0x4f>
mov    -0x10(%rbp),%rdi
mov    $0x200,%esi
call   1040 <audio_batch_cb@plt>
mov    -0x10(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x4(%rbp)
jmp    1136 <audioFrameEnd+0x16>
cmpl   $0x0,-0x4(%rbp)
je     1185 <audioFrameEnd+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <audio_batch_cb@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<audioFrameEnd>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %eax,%eax
call   1030 <apuGetBuf@plt>
mov    %rax,%rbx
cmp    $0x201,%r14d
jl     1168 <audioFrameEnd+0x48>
mov    %r14d,%ebp
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x200,%esi
call   1040 <audio_batch_cb@plt>
add    $0x1000,%rbx
lea    -0x200(%rbp),%r14d
cmp    $0x400,%ebp
mov    %r14d,%ebp
ja     1140 <audioFrameEnd+0x20>
jmp    116d <audioFrameEnd+0x4d>
test   %r14d,%r14d
je     117c <audioFrameEnd+0x5c>
mov    %rbx,%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <audio_batch_cb@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @audioFrameEnd(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
store i32 %0, i32* %2
%4 = call i64 (...) @apuGetBuf()
%5 = inttoptr i64 %4 to i32*
store i32* %5, i32** %3
br label %6
6: ; preds = %9, %1
%7 = load i32, i32* %2
%8 = icmp sgt i32 %7, 512
br i1 %8, label %9, label %16
9: ; preds = %6
%10 = load i32*, i32** %3
%11 = call i32 @audio_batch_cb(i32* %10, i32 512)
%12 = load i32*, i32** %3
%13 = getelementptr inbounds i32, i32* %12, i64 1024
store i32* %13, i32** %3
%14 = load i32, i32* %2
%15 = sub nsw i32 %14, 512
store i32 %15, i32* %2
br label %6
16: ; preds = %6
%17 = load i32, i32* %2
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32*, i32** %3
%21 = load i32, i32* %2
%22 = call i32 @audio_batch_cb(i32* %20, i32 %21)
br label %23
23: ; preds = %19, %16
ret void
}

LLVM IR (-O2):
define void @audioFrameEnd(i32 %0) {
%2 = tail call i64 (...) @apuGetBuf()
%3 = inttoptr i64 %2 to i32*
%4 = icmp sgt i32 %0, 512
br i1 %4, label %5, label %12
5: ; preds = %1, %5
%6 = phi i32* [ %9, %5 ], [ %3, %1 ]
%7 = phi i32 [ %10, %5 ], [ %0, %1 ]
%8 = tail call i32 @audio_batch_cb(i32* %6, i32 512)
%9 = getelementptr inbounds i32, i32* %6, i64 1024
%10 = add nsw i32 %7, -512
%11 = icmp ugt i32 %7, 1024
br i1 %11, label %5, label %14
12: ; preds = %1
%13 = icmp eq i32 %0, 0
br i1 %13, label %18, label %14
14: ; preds = %5, %12
%15 = phi i32* [ %3, %12 ], [ %9, %5 ]
%16 = phi i32 [ %0, %12 ], [ %10, %5 ]
%17 = tail call i32 @audio_batch_cb(i32* %15, i32 %16)
br label %18
18: ; preds = %14, %12
ret void
}