#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 serial_read(int, char *, int, double);
int serial_readline(int fd, char *buffer, int len, double timeout) {
  char *curs = buffer;
  while (curs == buffer || *(curs - 1) != '\n') {
    int r = serial_read(fd, curs, 1, timeout);
    if (r == 0)
      return 0;
    else if (r == -1)
      return -1;
    else
      curs += r;
  }
  return curs - buffer;
}


x86 assembly (clang, -O0):
<serial_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    $0x1,%al
cmp    -0x10(%rbp),%rcx
mov    %al,-0x2d(%rbp)
je     1153 <serial_readline+0x43>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1163 <serial_readline+0x53>
jmp    11c3 <serial_readline+0xb3>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%edx
call   1030 <serial_read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1192 <serial_readline+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <serial_readline+0xc1>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a8 <serial_readline+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <serial_readline+0xc1>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11be <serial_readline+0xae>
jmp    112f <serial_readline+0x1f>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_readline>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsi,%rbx
cmp    %r14,%rbx
je     1136 <serial_readline+0x26>
nopl   0x0(%rax,%rax,1)
cmpb   $0xa,-0x1(%rbx)
je     1163 <serial_readline+0x53>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
movsd  0x8(%rsp),%xmm0
call   1030 <serial_read@plt>
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jb     115f <serial_readline+0x4f>
cltq
add    %rax,%rbx
cmp    %r14,%rbx
jne    1130 <serial_readline+0x20>
jmp    1136 <serial_readline+0x26>
mov    %eax,%ebx
jmp    1166 <serial_readline+0x56>
sub    %r14d,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @serial_readline(i32 %0, i8* %1, i32 %2, double %3) {
br label %5
5: ; preds = %14, %4
%6 = phi i8* [ %1, %4 ], [ %16, %14 ]
%7 = icmp eq i8* %6, %1
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %6, i64 -1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 10
br i1 %11, label %17, label %12
12: ; preds = %5, %8
%13 = tail call i32 @serial_read(i32 %0, i8* %6, i32 1, double %3)
switch i32 %13, label %14 [
i32 0, label %22
i32 -1, label %22
]
14: ; preds = %12
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds i8, i8* %6, i64 %15
br label %5
17: ; preds = %8
%18 = ptrtoint i8* %6 to i64
%19 = ptrtoint i8* %1 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
br label %22
22: ; preds = %12, %12, %17
%23 = phi i32 [ %21, %17 ], [ %13, %12 ], [ %13, %12 ]
ret i32 %23
}