#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*/ TCIFLUSH;
scalar_t__ parse(char *, int, float *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int readQ(int fd, float *q) {
  char read_buffer[512];
  int bytes_read = 0;
  int i = 0;
  int startLastFrame = 0;
  int endLastFrame = 0;
  bytes_read = read(fd, read_buffer, 512);
  if (bytes_read >= 500) {
    printf("A lot of data in buffer: %d\n", bytes_read);
    tcflush(fd, TCIFLUSH);
    bytes_read = read(fd, read_buffer, 512);
  }
  if (bytes_read <= 0) {
    return -1;
  }
  for (i = (bytes_read - 1); i >= 0; i--) {
    if (read_buffer[i] == '\n')
      endLastFrame = i;
    if (read_buffer[i] == 'G') {
      startLastFrame = i;
      break;
    }
  }
  int len = endLastFrame - startLastFrame + 1;
  if (len > 40) {
    if (parse(&read_buffer[startLastFrame], len, q) > 0)
      return 0;
    else
      return -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<readQ>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x214(%rbp)
movl   $0x0,-0x218(%rbp)
movl   $0x0,-0x21c(%rbp)
movl   $0x0,-0x220(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x1f4,-0x214(%rbp)
jl     11e1 <readQ+0xa1>
mov    -0x214(%rbp),%esi
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jg     11fa <readQ+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readQ+0x1a7>
mov    -0x214(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x218(%rbp)
cmpl   $0x0,-0x218(%rbp)
jl     127c <readQ+0x13c>
movslq -0x218(%rbp),%rax
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    123a <readQ+0xfa>
mov    -0x218(%rbp),%eax
mov    %eax,-0x220(%rbp)
movslq -0x218(%rbp),%rax
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0x47,%eax
jne    1263 <readQ+0x123>
mov    -0x218(%rbp),%eax
mov    %eax,-0x21c(%rbp)
jmp    127c <readQ+0x13c>
jmp    1268 <readQ+0x128>
mov    -0x218(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x218(%rbp)
jmp    1209 <readQ+0xc9>
mov    -0x220(%rbp),%eax
sub    -0x21c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
cmpl   $0x28,-0x224(%rbp)
jle    12e0 <readQ+0x1a0>
movslq -0x21c(%rbp),%rax
lea    -0x210(%rbp),%rdi
add    %rax,%rdi
mov    -0x224(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1060 <parse@plt>
cmp    $0x0,%rax
jle    12d4 <readQ+0x194>
movl   $0x0,-0x4(%rbp)
jmp    12e7 <readQ+0x1a7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readQ+0x1a7>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readQ>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,%esi
cmp    $0x1f4,%eax
jl     1193 <readQ+0x53>
lea    0xe93(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <tcflush@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,%esi
mov    $0xffffffff,%eax
test   %esi,%esi
jle    11f1 <readQ+0xb1>
mov    %esi,%edx
xor    %ecx,%ecx
xor    %esi,%esi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %rdx,%rdx
jle    11cc <readQ+0x8c>
add    $0xffffffffffffffff,%rdx
mov    %edx,%edi
movzbl (%rsp,%rdi,1),%ebx
cmp    $0xa,%bl
cmove  %edx,%esi
cmp    $0x47,%bl
jne    11b0 <readQ+0x70>
mov    %edx,%ecx
sub    %ecx,%esi
cmp    $0x28,%esi
jl     11f1 <readQ+0xb1>
add    $0x1,%esi
mov    %ecx,%eax
lea    (%rsp,%rax,1),%rdi
mov    %r14,%rdx
call   1060 <parse@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setle  %al
neg    %eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"A lot of data in buffer: %d\0A\00"
@TCIFLUSH = global i32 0
define i32 @readQ(i32 %0, float* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca float*
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store float* %1, float** %5
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %4
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 512)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp sge i32 %15, 500
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %4
%21 = load i32, i32* @TCIFLUSH
%22 = call i32 @tcflush(i32 %20, i32 %21)
%23 = load i32, i32* %4
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 512)
store i32 %25, i32* %7
br label %26
26: ; preds = %17, %2
%27 = load i32, i32* %7
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 -1, i32* %3
br label %76
30: ; preds = %26
%31 = load i32, i32* %7
%32 = sub nsw i32 %31, 1
store i32 %32, i32* %8
br label %33
33: ; preds = %55, %30
%34 = load i32, i32* %8
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %58
36: ; preds = %33
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = load i32, i32* %8
store i32 %44, i32* %10
br label %45
45: ; preds = %43, %36
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 71
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = load i32, i32* %8
store i32 %53, i32* %9
br label %58
54: ; preds = %45
br label %55
55: ; preds = %54
%56 = load i32, i32* %8
%57 = add nsw i32 %56, -1
store i32 %57, i32* %8
br label %33
58: ; preds = %52, %33
%59 = load i32, i32* %10
%60 = load i32, i32* %9
%61 = sub nsw i32 %59, %60
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
%63 = load i32, i32* %11
%64 = icmp sgt i32 %63, 40
br i1 %64, label %65, label %75
65: ; preds = %58
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %67
%69 = load i32, i32* %11
%70 = load float*, float** %5
%71 = call i64 @parse(i8* %68, i32 %69, float* %70)
%72 = icmp sgt i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %65
store i32 0, i32* %3
br label %76
74: ; preds = %65
store i32 -1, i32* %3
br label %76
75: ; preds = %58
store i32 -1, i32* %3
br label %76
76: ; preds = %75, %74, %73, %29
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [29 x i8] c"A lot of data in buffer: %d\0A\00"
@TCIFLUSH = global i32 0
define i32 @readQ(i32 %0, float* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 512)
%6 = icmp sgt i32 %5, 499
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %5)
%9 = load i32, i32* @TCIFLUSH
%10 = tail call i32 @tcflush(i32 %0, i32 %9)
%11 = call i32 @read(i32 %0, i8* %4, i32 512)
br label %12
12: ; preds = %7, %2
%13 = phi i32 [ %11, %7 ], [ %5, %2 ]
%14 = icmp slt i32 %13, 1
br i1 %14, label %44, label %15
15: ; preds = %12
%16 = zext i32 %13 to i64
br label %17
17: ; preds = %15, %22
%18 = phi i64 [ %16, %15 ], [ %20, %22 ]
%19 = phi i32 [ 0, %15 ], [ %28, %22 ]
%20 = add nsw i64 %18, -1
%21 = icmp sgt i64 %18, 0
br i1 %21, label %22, label %32
22: ; preds = %17
%23 = trunc i64 %20 to i32
%24 = and i64 %20, 4294967295
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
%28 = select i1 %27, i32 %23, i32 %19
%29 = icmp eq i8 %26, 71
br i1 %29, label %30, label %17
30: ; preds = %22
%31 = trunc i64 %20 to i32
br label %32
32: ; preds = %17, %30
%33 = phi i32 [ %31, %30 ], [ 0, %17 ]
%34 = phi i32 [ %28, %30 ], [ %19, %17 ]
%35 = sub nsw i32 %34, %33
%36 = icmp sgt i32 %35, 39
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = add nuw nsw i32 %35, 1
%39 = zext i32 %33 to i64
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %39
%41 = call i64 @parse(i8* %40, i32 %38, float* %1)
%42 = icmp slt i64 %41, 1
%43 = sext i1 %42 to i32
br label %44
44: ; preds = %37, %32, %12
%45 = phi i32 [ -1, %12 ], [ %43, %37 ], [ -1, %32 ]
ret i32 %45
}