#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 write_frame(int, unsigned char *, int, int);
int handle_channel_data(unsigned char *buf, int len, int channel) {
  int written = 0;
  int i = 0;
  int last = 0;
  while (written != len && i < 5) {
    last = write_frame(channel, buf + written, len - written, 0xEF);
    written += last;
    if (last == 0)
      i++;
  }
  return (len - written);
}


x86 assembly (clang, -O0):
<handle_channel_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x1d(%rbp)
je     1152 <handle_channel_data+0x42>
cmpl   $0x5,-0x18(%rbp)
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1162 <handle_channel_data+0x52>
jmp    11a4 <handle_channel_data+0x94>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x14(%rbp),%edx
mov    $0xef,%ecx
call   1030 <write_frame@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    119f <handle_channel_data+0x8f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1137 <handle_channel_data+0x27>
mov    -0xc(%rbp),%eax
sub    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_channel_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
test   %esi,%esi
je     115d <handle_channel_data+0x4d>
mov    %edx,%r14d
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%rsi
mov    %r12d,%edx
sub    %ebp,%edx
mov    %r14d,%edi
mov    $0xef,%ecx
call   1030 <write_frame@plt>
add    %eax,%ebp
cmp    $0x1,%eax
adc    $0x0,%ebx
cmp    %r12d,%ebp
je     115f <handle_channel_data+0x4f>
cmp    $0x5,%ebx
jb     1130 <handle_channel_data+0x20>
jmp    115f <handle_channel_data+0x4f>
xor    %ebp,%ebp
sub    %ebp,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @handle_channel_data(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %37, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 5
br label %17
17: ; preds = %14, %10
%18 = phi i1 [ false, %10 ], [ %16, %14 ]
br i1 %18, label %19, label %38
19: ; preds = %17
%20 = load i32, i32* %6
%21 = load i8*, i8** %4
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i32, i32* %5
%26 = load i32, i32* %7
%27 = sub nsw i32 %25, %26
%28 = call i32 @write_frame(i32 %20, i8* %24, i32 %27, i32 239)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = load i32, i32* %7
%31 = add nsw i32 %30, %29
store i32 %31, i32* %7
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %37
37: ; preds = %34, %19
br label %10
38: ; preds = %17
%39 = load i32, i32* %5
%40 = load i32, i32* %7
%41 = sub nsw i32 %39, %40
ret i32 %41
}

LLVM IR (-O2):
define i32 @handle_channel_data(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %5
%6 = phi i32 [ %15, %5 ], [ 0, %3 ]
%7 = phi i32 [ %12, %5 ], [ 0, %3 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = sub nsw i32 %1, %7
%11 = tail call i32 @write_frame(i32 %2, i8* %9, i32 %10, i32 239)
%12 = add nsw i32 %11, %7
%13 = icmp eq i32 %11, 0
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %6, %14
%16 = icmp ne i32 %12, %1
%17 = icmp ult i32 %15, 5
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %5, label %19
19: ; preds = %5, %3
%20 = phi i32 [ 0, %3 ], [ %12, %5 ]
%21 = sub nsw i32 %1, %20
ret i32 %21
}