#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 */
scalar_t__ read(int, void *, int);
void gps_serial_read(int fd, char *buffer) {
  char character;
  int first_character = 1;
  while (1) {
    if (read(fd, (void *)(&character), 1) > 0) {
      if (first_character && character == '$') {
        *buffer++ = character;
        first_character = 0;
      } else if (!first_character) {
        if (character == '\n') {
          *buffer = '\0';
          break;
        }
        *buffer++ = character;
      }
    }
  }
}


x86 assembly (clang, -O0):
<gps_serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jle    11b9 <gps_serial_read+0xa9>
cmpl   $0x0,-0x18(%rbp)
je     1178 <gps_serial_read+0x68>
movsbl -0x11(%rbp),%eax
cmp    $0x24,%eax
jne    1178 <gps_serial_read+0x68>
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x0,-0x18(%rbp)
jmp    11b4 <gps_serial_read+0xa4>
cmpl   $0x0,-0x18(%rbp)
jne    11af <gps_serial_read+0x9f>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    119b <gps_serial_read+0x8b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11be <gps_serial_read+0xae>
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11b4 <gps_serial_read+0xa4>
jmp    11b9 <gps_serial_read+0xa9>
jmp    1126 <gps_serial_read+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gps_serial_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x1,%r15d
lea    0x7(%rsp),%rbx
jmp    1140 <gps_serial_read+0x30>
test   %r15d,%r15d
mov    $0x1,%r15d
je     1166 <gps_serial_read+0x56>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1140 <gps_serial_read+0x30>
movzbl 0x7(%rsp),%eax
test   %r15d,%r15d
je     1129 <gps_serial_read+0x19>
cmp    $0x24,%al
jne    1129 <gps_serial_read+0x19>
mov    $0x24,%al
jmp    116a <gps_serial_read+0x5a>
cmp    $0xa,%al
je     1176 <gps_serial_read+0x66>
mov    %al,(%r14)
add    $0x1,%r14
xor    %r15d,%r15d
jmp    1140 <gps_serial_read+0x30>
movb   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @gps_serial_read(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 1, i32* %6
br label %7
7: ; preds = %2, %37
%8 = load i32, i32* %3
%9 = call i64 @read(i32 %8, i8* %5, i32 1)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %37
11: ; preds = %7
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i8, i8* %5
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 36
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i8, i8* %5
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
store i8 %19, i8* %20
store i32 0, i32* %6
br label %36
22: ; preds = %14, %11
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %35, label %25
25: ; preds = %22
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i8*, i8** %4
store i8 0, i8* %30
br label %38
31: ; preds = %25
%32 = load i8, i8* %5
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %4
store i8 %32, i8* %33
br label %35
35: ; preds = %31, %22
br label %36
36: ; preds = %35, %18
br label %37
37: ; preds = %36, %7
br label %7
38: ; preds = %29
ret void
}

LLVM IR (-O2):
define void @gps_serial_read(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i8
br label %4
4: ; preds = %21, %2
%5 = phi i8* [ %23, %21 ], [ %1, %2 ]
%6 = phi i32 [ 0, %21 ], [ 1, %2 ]
br label %7
7: ; preds = %4, %17
%8 = phi i32 [ %6, %4 ], [ 1, %17 ]
br label %9
9: ; preds = %7, %9
%10 = call i64 @read(i32 %0, i8* %3, i32 1)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %9
12: ; preds = %9
%13 = icmp ne i32 %8, 0
%14 = load i8, i8* %3
%15 = icmp eq i8 %14, 36
%16 = select i1 %13, i1 %15, i1 false
br i1 %16, label %21, label %17
17: ; preds = %12
br i1 %13, label %7, label %18
18: ; preds = %17
%19 = icmp eq i8 %14, 10
br i1 %19, label %20, label %21
20: ; preds = %18
store i8 0, i8* %5
ret void
21: ; preds = %12, %18
%22 = phi i8 [ %14, %18 ], [ 36, %12 ]
%23 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %22, i8* %5
br label %4
}