#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 readByte();
int readPacket(char *request, int maxLength) {
  int ch;
  int packetLen;
  do {
    ch = readByte();
    if (ch < 0)
      return -1;
  } while (ch != '$');
  packetLen = 0;
  while (1) {
    ch = readByte();
    if (ch < 0)
      return -1;
    if (ch == '#')
      break;
    if (packetLen < maxLength)
      request[packetLen++] = (char)ch;
  }
  readByte();
  readByte();
  request[packetLen] = '\0';
  return packetLen;
}


x86 assembly (clang, -O0):
<readPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1030 <readByte@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    113f <readPacket+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <readPacket+0xbf>
jmp    1144 <readPacket+0x34>
cmpl   $0x24,-0x18(%rbp)
jne    111f <readPacket+0xf>
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <readByte@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1175 <readPacket+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <readPacket+0xbf>
cmpl   $0x23,-0x18(%rbp)
jne    1184 <readPacket+0x74>
jmp    11af <readPacket+0x9f>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11aa <readPacket+0x9a>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1155 <readPacket+0x45>
mov    $0x0,%al
call   1030 <readByte@plt>
mov    $0x0,%al
call   1030 <readByte@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readPacket>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <readByte@plt>
test   %eax,%eax
js     1160 <readPacket+0x50>
cmp    $0x24,%eax
jne    1120 <readPacket+0x10>
xor    %eax,%eax
call   1030 <readByte@plt>
test   %eax,%eax
js     1160 <readPacket+0x50>
xor    %ebx,%ebx
jmp    114b <readPacket+0x3b>
nop
xor    %eax,%eax
call   1030 <readByte@plt>
test   %eax,%eax
js     1160 <readPacket+0x50>
cmp    $0x23,%eax
je     116c <readPacket+0x5c>
cmp    %ebp,%ebx
jge    1140 <readPacket+0x30>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %al,(%r14,%rcx,1)
jmp    1140 <readPacket+0x30>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <readByte@plt>
xor    %eax,%eax
call   1030 <readByte@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
jmp    1165 <readPacket+0x55>

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

LLVM IR (-O2):
define i32 @readPacket(i8* nocapture writeonly %0, i32 %1) {
br label %3
3: ; preds = %6, %2
%4 = tail call i32 (...) @readByte()
%5 = icmp slt i32 %4, 0
br i1 %5, label %31, label %6
6: ; preds = %3
%7 = icmp eq i32 %4, 36
br i1 %7, label %8, label %3
8: ; preds = %6
%9 = tail call i32 (...) @readByte()
%10 = icmp slt i32 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %8, %22
%12 = phi i32 [ %24, %22 ], [ %9, %8 ]
%13 = phi i32 [ %23, %22 ], [ 0, %8 ]
%14 = icmp eq i32 %12, 35
br i1 %14, label %26, label %15
15: ; preds = %11
%16 = icmp slt i32 %13, %1
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = trunc i32 %12 to i8
%19 = add nsw i32 %13, 1
%20 = sext i32 %13 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
store i8 %18, i8* %21
br label %22
22: ; preds = %17, %15
%23 = phi i32 [ %19, %17 ], [ %13, %15 ]
%24 = tail call i32 (...) @readByte()
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %11
26: ; preds = %11
%27 = tail call i32 (...) @readByte()
%28 = tail call i32 (...) @readByte()
%29 = sext i32 %13 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %3, %22, %8, %26
%32 = phi i32 [ %13, %26 ], [ -1, %8 ], [ -1, %22 ], [ -1, %3 ]
ret i32 %32
}