#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 gets_serial_port(char *data, int len) {
  int i;
  for (i = 0; i < len - 1; i++) {
    data[i] = read_serial_port();
    if (data[i] == 0 || data[i] == '\r' || data[i] == '\n') {
      break;
    }
  }
  data[i] = 0;
  return i;
}


x86 assembly (clang, -O0):
<gets_serial_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a2 <gets_serial_port+0x92>
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     118a <gets_serial_port+0x7a>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
je     118a <gets_serial_port+0x7a>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    118f <gets_serial_port+0x7f>
jmp    11a2 <gets_serial_port+0x92>
jmp    1194 <gets_serial_port+0x84>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <gets_serial_port+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets_serial_port>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
xor    %ebp,%ebp
cmp    $0x2,%esi
jl     1165 <gets_serial_port+0x55>
mov    %esi,%r14d
add    $0xffffffff,%r14d
mov    %r14d,%r15d
mov    $0x2401,%r12d
xor    %ebp,%ebp
jmp    1149 <gets_serial_port+0x39>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%r15
je     1162 <gets_serial_port+0x52>
xor    %eax,%eax
call   1030 <read_serial_port@plt>
mov    %al,(%rbx,%rbp,1)
cmp    $0xd,%al
ja     1140 <gets_serial_port+0x30>
movzbl %al,%eax
bt     %rax,%r12
jae    1140 <gets_serial_port+0x30>
jmp    1165 <gets_serial_port+0x55>
mov    %r14d,%ebp
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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