#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 /*<<< orphan*/ TCIOFLUSH;
int read(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int obd_read(int fd, char *buf) {
  int valid_chars = 0;
  char current_char;
  while ((read(fd, &buf[valid_chars], 1) == 1) && (valid_chars < 50)) {
    current_char = buf[valid_chars];
    if (current_char == '\r' || current_char == '\n') {
      continue;
    } else if (current_char == '>') {
      if (current_char == 0)
        continue;
      else
        break;
    } else {
      valid_chars++;
    }
  }
  buf[valid_chars] = '\0';
  tcflush(fd, TCIOFLUSH);
  return valid_chars;
}


x86 assembly (clang, -O0):
<obd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x16(%rbp)
jne    1168 <obd_read+0x48>
cmpl   $0x32,-0x14(%rbp)
setl   %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    1178 <obd_read+0x58>
jmp    11dc <obd_read+0xbc>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
je     11a0 <obd_read+0x80>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    11a5 <obd_read+0x85>
jmp    1136 <obd_read+0x16>
movsbl -0x15(%rbp),%eax
cmp    $0x3e,%eax
jne    11c9 <obd_read+0xa9>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jne    11c4 <obd_read+0xa4>
jmp    1136 <obd_read+0x16>
jmp    11dc <obd_read+0xbc>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d7 <obd_read+0xb7>
jmp    1136 <obd_read+0x16>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<obd_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x1,%edx
call   1040 <read@plt>
xor    %r15d,%r15d
cmp    $0x1,%eax
jne    119f <obd_read+0x7f>
xor    %r12d,%r12d
mov    %r14,%rbx
jmp    1164 <obd_read+0x44>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11a4 <obd_read+0x84>
movzbl (%rbx),%eax
cmp    $0xa,%al
je     1150 <obd_read+0x30>
cmp    $0xd,%al
je     1150 <obd_read+0x30>
cmp    $0x3e,%al
je     11a4 <obd_read+0x84>
lea    0x1(%r12),%r15
lea    (%r14,%r12,1),%rbx
add    $0x1,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11a7 <obd_read+0x87>
cmp    $0x31,%r12
mov    %r15,%r12
jb     1164 <obd_read+0x44>
jmp    11a7 <obd_read+0x87>
mov    %r14,%rbx
jmp    11a7 <obd_read+0x87>
mov    %r12d,%r15d
movb   $0x0,(%rbx)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <tcflush@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@TCIOFLUSH = global i32 0
define i32 @obd_read(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %7
7: ; preds = %48, %42, %33, %2
%8 = load i32, i32* %3
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = call i32 @read(i32 %8, i8* %12, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 50
br label %18
18: ; preds = %15, %7
%19 = phi i1 [ false, %7 ], [ %17, %15 ]
br i1 %19, label %20, label %49
20: ; preds = %18
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* %6
%26 = load i8, i8* %6
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 13
br i1 %28, label %33, label %29
29: ; preds = %20
%30 = load i8, i8* %6
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %34
33: ; preds = %29, %20
br label %7
34: ; preds = %29
%35 = load i8, i8* %6
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 62
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = load i8, i8* %6
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %7
43: ; preds = %38
br label %49
44: ; preds = %34
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %47
47: ; preds = %44
br label %48
48: ; preds = %47
br label %7
49: ; preds = %43, %18
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
%54 = load i32, i32* %3
%55 = load i32, i32* @TCIOFLUSH
%56 = call i32 @tcflush(i32 %54, i32 %55)
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
@TCIOFLUSH = global i32 0
define i32 @obd_read(i32 %0, i8* nocapture %1) {
%3 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %24
5: ; preds = %2, %13
%6 = phi i64 [ %14, %13 ], [ 0, %2 ]
%7 = phi i8* [ %15, %13 ], [ %1, %2 ]
br label %8
8: ; preds = %5, %10
%9 = load i8, i8* %7
switch i8 %9, label %13 [
i8 13, label %10
i8 10, label %10
i8 62, label %22
]
10: ; preds = %8, %8
%11 = tail call i32 @read(i32 %0, i8* %7, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %8, label %22
13: ; preds = %8
%14 = add nuw nsw i64 %6, 1
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = tail call i32 @read(i32 %0, i8* %15, i32 1)
%17 = icmp eq i32 %16, 1
%18 = icmp ult i64 %6, 49
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %5, label %20
20: ; preds = %13
%21 = trunc i64 %14 to i32
br label %24
22: ; preds = %8, %10
%23 = trunc i64 %6 to i32
br label %24
24: ; preds = %22, %20, %2
%25 = phi i32 [ 0, %2 ], [ %21, %20 ], [ %23, %22 ]
%26 = phi i8* [ %1, %2 ], [ %15, %20 ], [ %7, %22 ]
store i8 0, i8* %26
%27 = load i32, i32* @TCIOFLUSH
%28 = tail call i32 @tcflush(i32 %0, i32 %27)
ret i32 %25
}