#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 {
  int dummy;
};

/* Variables and functions */
int serial_getc(struct serial *);
scalar_t__ serial_has_data(struct serial *, int /*<<< orphan*/, int);
int recv_line(struct serial *dev, unsigned char *s, int maxsize) {
  int i = 0;
  while ((i < (maxsize - 1)) && (serial_has_data(dev, 0, 250000))) {
    int c;
    c = serial_getc(dev);
    if (c >= 0) {
      s[i++] = c;
      if (c == 0x0a) {
        s[i] = 0;
        return i;
      }
    }
  }
  s[i] = 0;
  return i;
}


x86 assembly (clang, -O0):
<recv_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x25(%rbp)
jge    116a <recv_line+0x4a>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x3d090,%edx
call   1030 <serial_has_data@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    117a <recv_line+0x5a>
jmp    11d5 <recv_line+0xb5>
mov    -0x10(%rbp),%rdi
call   1040 <serial_getc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11d0 <recv_line+0xb0>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0x24(%rbp)
jne    11cb <recv_line+0xab>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <recv_line+0xc7>
jmp    11d0 <recv_line+0xb0>
jmp    113a <recv_line+0x1a>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
xor    %ebp,%ebp
cmp    $0x2,%edx
jl     1174 <recv_line+0x54>
mov    %edx,%r15d
mov    %rdi,%rbx
add    $0xffffffff,%r15d
xor    %ebp,%ebp
jmp    1145 <recv_line+0x25>
nop
cmp    %r15d,%ebp
jge    1174 <recv_line+0x54>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3d090,%edx
call   1030 <serial_has_data@plt>
test   %rax,%rax
je     1174 <recv_line+0x54>
mov    %rbx,%rdi
call   1040 <serial_getc@plt>
test   %eax,%eax
js     1140 <recv_line+0x20>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%r14,%rcx,1)
cmp    $0xa,%eax
jne    1140 <recv_line+0x20>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.serial = type { i32 }
define i32 @recv_line(%struct.serial* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.serial*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.serial* %0, %struct.serial** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %43, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load %struct.serial*, %struct.serial** %5
%17 = call i64 @serial_has_data(%struct.serial* %16, i32 0, i32 250000)
%18 = icmp ne i64 %17, 0
br label %19
19: ; preds = %15, %10
%20 = phi i1 [ false, %10 ], [ %18, %15 ]
br i1 %20, label %21, label %44
21: ; preds = %19
%22 = load %struct.serial*, %struct.serial** %5
%23 = call i32 @serial_getc(%struct.serial* %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %43
26: ; preds = %21
%27 = load i32, i32* %9
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %6
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %28, i8* %33
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 10
br i1 %35, label %36, label %42
36: ; preds = %26
%37 = load i8*, i8** %6
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %8
store i32 %41, i32* %4
br label %50
42: ; preds = %26
br label %43
43: ; preds = %42, %21
br label %10
44: ; preds = %19
%45 = load i8*, i8** %6
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %50
50: ; preds = %44, %36
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.serial = type { i32 }
define i32 @recv_line(%struct.serial* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = add nsw i32 %2, -1
%5 = icmp sgt i32 %2, 1
br i1 %5, label %6, label %22
6: ; preds = %3, %19
%7 = phi i32 [ %20, %19 ], [ 0, %3 ]
%8 = tail call i64 @serial_has_data(%struct.serial* %0, i32 0, i32 250000)
%9 = icmp eq i64 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = tail call i32 @serial_getc(%struct.serial* %0)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = trunc i32 %11 to i8
%15 = add nsw i32 %7, 1
%16 = sext i32 %7 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %14, i8* %17
%18 = icmp eq i32 %11, 10
br i1 %18, label %22, label %19
19: ; preds = %10, %13
%20 = phi i32 [ %15, %13 ], [ %7, %10 ]
%21 = icmp slt i32 %20, %4
br i1 %21, label %6, label %22
22: ; preds = %19, %6, %13, %3
%23 = phi i32 [ 0, %3 ], [ %15, %13 ], [ %20, %19 ], [ %7, %6 ]
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %25
ret i32 %23
}