#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 */
unsigned char rj485_getc();
int rj_readstat();
int rj485_read(unsigned char *buf, int maxlen) {
  int i = 0;
  while ((rj_readstat() & 0x04) && (i < maxlen)) {
    buf[i++] = rj485_getc();
  }
  buf[i] = 0;
  return i;
}


x86 assembly (clang, -O0):
<rj485_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <rj_readstat@plt>
mov    %eax,%ecx
and    $0x4,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
je     115c <rj485_read+0x3c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    116c <rj485_read+0x4c>
jmp    118f <rj485_read+0x6f>
mov    $0x0,%al
call   1040 <rj485_getc@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1136 <rj485_read+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rj485_read>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <rj_readstat@plt>
test   $0x4,%al
je     115f <rj485_read+0x3f>
test   %ebp,%ebp
jle    115f <rj485_read+0x3f>
mov    %ebp,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rj485_getc@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <rj_readstat@plt>
test   $0x4,%al
je     115f <rj485_read+0x3f>
cmp    %rbp,%rbx
jb     1140 <rj485_read+0x20>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @rj485_read(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 (...) @rj_readstat()
%4 = and i32 %3, 4
%5 = icmp ne i32 %4, 0
%6 = icmp sgt i32 %1, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %22
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %13, %10 ]
%12 = tail call zeroext i8 (...) @rj485_getc()
%13 = add nuw nsw i64 %11, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %12, i8* %14
%15 = tail call i32 (...) @rj_readstat()
%16 = and i32 %15, 4
%17 = icmp ne i32 %16, 0
%18 = icmp ult i64 %13, %9
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %10, label %20
20: ; preds = %10
%21 = trunc i64 %13 to i32
br label %22
22: ; preds = %20, %2
%23 = phi i32 [ 0, %2 ], [ %21, %20 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
ret i32 %23
}