#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 */
char read_serial_port();
int read_serial_data(char *s, int size) {
  int i = 0;
  while (size > 0) {
    s[i] = read_serial_port();
    if (s[i] == 0x00 || s[i] == '\n' || s[i] == '\r')
      break;
    i++;
    size--;
  }
  return i;
}


x86 assembly (clang, -O0):
<read_serial_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    119f <read_serial_data+0x8f>
mov    $0x0,%al
call   1030 <read_serial_port@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1183 <read_serial_data+0x73>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1183 <read_serial_data+0x73>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1188 <read_serial_data+0x78>
jmp    119f <read_serial_data+0x8f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1126 <read_serial_data+0x16>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_serial_data>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1153 <read_serial_data+0x43>
mov    %rdi,%r14
mov    %esi,%r15d
mov    $0x2401,%r12d
xor    %ebx,%ebx
jmp    1139 <read_serial_data+0x29>
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1157 <read_serial_data+0x47>
xor    %eax,%eax
call   1030 <read_serial_port@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xd,%al
ja     1130 <read_serial_data+0x20>
movzbl %al,%eax
bt     %rax,%r12
jae    1130 <read_serial_data+0x20>
jmp    115a <read_serial_data+0x4a>
xor    %ebx,%ebx
jmp    115a <read_serial_data+0x4a>
mov    %r15d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @read_serial_data(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 = %39, %2
%7 = load i32, i32* %4
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %44
9: ; preds = %6
%10 = call signext i8 (...) @read_serial_port()
%11 = load i8*, i8** %3
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 %10, i8* %14
%15 = load i8*, i8** %3
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %38, label %22
22: ; preds = %9
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %38, label %30
30: ; preds = %22
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 13
br i1 %37, label %38, label %39
38: ; preds = %30, %22, %9
br label %44
39: ; preds = %30
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
%42 = load i32, i32* %4
%43 = add nsw i32 %42, -1
store i32 %43, i32* %4
br label %6
44: ; preds = %38, %6
%45 = load i32, i32* %5
ret i32 %45
}

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