#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 */
scalar_t__ kiss_serial_buffer_count(struct serial_buffer *);
unsigned char kiss_serial_buffer_getb(struct serial_buffer *);
int kiss_serial_buffer_get(struct serial_buffer *buffer, unsigned char *data,
                           int count) {
  int i;
  for (i = 0; i < count && kiss_serial_buffer_count(buffer); i++)
    data[i] = kiss_serial_buffer_getb(buffer);
  return i;
}


x86 assembly (clang, -O0):
<kiss_serial_buffer_get>:
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    115e <kiss_serial_buffer_get+0x3e>
mov    -0x8(%rbp),%rdi
call   1040 <kiss_serial_buffer_count@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116e <kiss_serial_buffer_get+0x4e>
jmp    1192 <kiss_serial_buffer_get+0x72>
mov    -0x8(%rbp),%rdi
call   1030 <kiss_serial_buffer_getb@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113a <kiss_serial_buffer_get+0x1a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kiss_serial_buffer_get>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1167 <kiss_serial_buffer_get+0x47>
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>
test   %rax,%rax
je     1169 <kiss_serial_buffer_get+0x49>
mov    %r15,%rdi
call   1030 <kiss_serial_buffer_getb@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <kiss_serial_buffer_get+0x20>
mov    %r12d,%ebx
jmp    1169 <kiss_serial_buffer_get+0x49>
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_get(%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 = %25, %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 i64 @kiss_serial_buffer_count(%struct.serial_buffer* %13)
%15 = icmp ne i64 %14, 0
br label %16
16: ; preds = %12, %8
%17 = phi i1 [ false, %8 ], [ %15, %12 ]
br i1 %17, label %18, label %28
18: ; preds = %16
%19 = load %struct.serial_buffer*, %struct.serial_buffer** %4
%20 = call zeroext i8 @kiss_serial_buffer_getb(%struct.serial_buffer* %19)
%21 = load i8*, i8** %5
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 %20, i8* %24
br label %25
25: ; preds = %18
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %8
28: ; preds = %16
%29 = load i32, i32* %7
ret i32 %29
}

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