#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 */
struct serial_buffer {
  int dummy;
};

/* Variables and functions */
int kiss_serial_buffer_count(struct serial_buffer *);
int /*<<< orphan*/ kiss_serial_buffer_putb(struct serial_buffer *,
                                           unsigned char);
int kiss_serial_buffer_put(struct serial_buffer *buffer, unsigned char *data,
                           int count) {
  int i;
  for (i = 0; i < count && kiss_serial_buffer_count(buffer) < 256 - 1; i++)
    kiss_serial_buffer_putb(buffer, data[i]);
  return i;
}


x86 assembly (clang, -O0):
<kiss_serial_buffer_put>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x19(%rbp)
jge    115f <kiss_serial_buffer_put+0x3f>
mov    -0x8(%rbp),%rdi
call   1040 <kiss_serial_buffer_count@plt>
cmp    $0xff,%eax
setl   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116f <kiss_serial_buffer_put+0x4f>
jmp    1192 <kiss_serial_buffer_put+0x72>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
call   1030 <kiss_serial_buffer_putb@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113a <kiss_serial_buffer_put+0x1a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kiss_serial_buffer_put>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    116a <kiss_serial_buffer_put+0x4a>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <kiss_serial_buffer_count@plt>
cmp    $0xfe,%eax
jg     116c <kiss_serial_buffer_put+0x4c>
movzbl (%r14,%rbx,1),%esi
mov    %r15,%rdi
call   1030 <kiss_serial_buffer_putb@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <kiss_serial_buffer_put+0x20>
mov    %r12d,%ebx
jmp    116c <kiss_serial_buffer_put+0x4c>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.serial_buffer = type { i32 }
define i32 @kiss_serial_buffer_put(%struct.serial_buffer* %0, i8* %1, i32 %2) {
%4 = alloca %struct.serial_buffer*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store %struct.serial_buffer* %0, %struct.serial_buffer** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %26, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load %struct.serial_buffer*, %struct.serial_buffer** %4
%14 = call i32 @kiss_serial_buffer_count(%struct.serial_buffer* %13)
%15 = icmp slt i32 %14, 255
br label %16
16: ; preds = %12, %8
%17 = phi i1 [ false, %8 ], [ %15, %12 ]
br i1 %17, label %18, label %29
18: ; preds = %16
%19 = load %struct.serial_buffer*, %struct.serial_buffer** %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = call i32 @kiss_serial_buffer_putb(%struct.serial_buffer* %19, i8 zeroext %24)
br label %26
26: ; preds = %18
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %8
29: ; preds = %16
%30 = load i32, i32* %7
ret i32 %30
}

LLVM IR (-O2):
%struct.serial_buffer = type { i32 }
define i32 @kiss_serial_buffer_put(%struct.serial_buffer* %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %11
%8 = phi i64 [ 0, %5 ], [ %15, %11 ]
%9 = tail call i32 @kiss_serial_buffer_count(%struct.serial_buffer* %0)
%10 = icmp slt i32 %9, 255
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %1, i64 %8
%13 = load i8, i8* %12
%14 = tail call i32 @kiss_serial_buffer_putb(%struct.serial_buffer* %0, i8 zeroext %13)
%15 = add nuw nsw i64 %8, 1
%16 = icmp eq i64 %15, %6
br i1 %16, label %19, label %7
17: ; preds = %7
%18 = trunc i64 %8 to i32
br label %19
19: ; preds = %11, %17, %3
%20 = phi i32 [ 0, %3 ], [ %18, %17 ], [ %2, %11 ]
ret i32 %20
}