#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*/ Log(char *, ...);
int /*<<< orphan*/ LogErrno(char *);
int /*<<< orphan*/ ReadChars(int, char *, int, int);
scalar_t__ errno;
int /*<<< orphan*/ explicit_bzero(char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char ReadPacket(int fd, char **message, int eof_permitted) {
  char type;
  *message = NULL;
  if (!ReadChars(fd, &type, 1, eof_permitted)) {
    return 0;
  }
  if (type == 0) {
    Log("invalid packet type 0");
    return 0;
  }
  char c;
  if (!ReadChars(fd, &c, 1, 0)) {
    return 0;
  }
  if (c != ' ') {
    Log("invalid character after packet type, expecting space");
    return 0;
  }
  int len = 0;
  for (;;) {
    errno = 0;
    if (!ReadChars(fd, &c, 1, 0)) {
      return 0;
    }
    switch (c) {
    case '\n':
      goto have_len;
    case '0':
      len = len * 10 + 0;
      break;
    case '1':
      len = len * 10 + 1;
      break;
    case '2':
      len = len * 10 + 2;
      break;
    case '3':
      len = len * 10 + 3;
      break;
    case '4':
      len = len * 10 + 4;
      break;
    case '5':
      len = len * 10 + 5;
      break;
    case '6':
      len = len * 10 + 6;
      break;
    case '7':
      len = len * 10 + 7;
      break;
    case '8':
      len = len * 10 + 8;
      break;
    case '9':
      len = len * 10 + 9;
      break;
    default:
      Log("invalid character during packet length, expecting 0-9 or newline");
      return 0;
    }
  }
have_len:
  if (len < 0 || len >= 0xFFFF) {
    Log("invalid length %d", len);
    return 0;
  }
  *message = malloc((size_t)len + 1);
  if ((type == 'p') && 0 < 0) {
    LogErrno("mlock");
  }
  if (len != 0 && !ReadChars(fd, *message, len, 0)) {
    explicit_bzero(*message, len + 1);
    free(*message);
    *message = NULL;
    return 0;
  }
  (*message)[len] = 0;
  if (!ReadChars(fd, &c, 1, 0)) {
    explicit_bzero(*message, len + 1);
    free(*message);
    *message = NULL;
    return 0;
  }
  if (c != '\n') {
    Log("invalid character after packet message, expecting newline");
    return 0;
  }
  return type;
}


x86 assembly (clang, -O0):
<ReadPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%ecx
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <ReadChars@plt>
cmp    $0x0,%eax
jne    11a3 <ReadPacket+0x43>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jne    11c7 <ReadPacket+0x67>
lea    0xf09(%rip),%rdi
mov    $0x0,%al
call   1070 <Log@plt>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
cmp    $0x0,%eax
jne    11ec <ReadPacket+0x8c>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
movsbl -0x16(%rbp),%eax
cmp    $0x20,%eax
je     1210 <ReadPacket+0xb0>
lea    0xed6(%rip),%rdi
mov    $0x0,%al
call   1070 <Log@plt>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
movl   $0x0,-0x1c(%rbp)
mov    0x2dc2(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
cmp    $0x0,%eax
jne    124a <ReadPacket+0xea>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
movsbl -0x16(%rbp),%eax
add    $0xfffffff6,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x2f,%eax
ja     130f <ReadPacket+0x1af>
mov    -0x28(%rbp),%rax
lea    0xd95(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    132b <ReadPacket+0x1cb>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x0,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x6,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
imul   $0xa,-0x1c(%rbp),%eax
add    $0x9,%eax
mov    %eax,-0x1c(%rbp)
jmp    1326 <ReadPacket+0x1c6>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1070 <Log@plt>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
jmp    1217 <ReadPacket+0xb7>
cmpl   $0x0,-0x1c(%rbp)
jl     1342 <ReadPacket+0x1e2>
cmpl   $0xffff,-0x1c(%rbp)
jl     135c <ReadPacket+0x1fc>
mov    -0x1c(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1070 <Log@plt>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
movslq -0x1c(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movsbl -0x15(%rbp),%eax
cmp    $0x70,%eax
jne    139b <ReadPacket+0x23b>
xor    %eax,%eax
test   $0x1,%al
jne    138f <ReadPacket+0x22f>
jmp    139b <ReadPacket+0x23b>
lea    0xdc8(%rip),%rdi
call   1050 <LogErrno@plt>
cmpl   $0x0,-0x1c(%rbp)
je     13f4 <ReadPacket+0x294>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
cmp    $0x0,%eax
jne    13f4 <ReadPacket+0x294>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1080 <explicit_bzero@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
cmp    $0x0,%eax
jne    1451 <ReadPacket+0x2f1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1080 <explicit_bzero@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
movsbl -0x16(%rbp),%eax
cmp    $0xa,%eax
je     1475 <ReadPacket+0x315>
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1070 <Log@plt>
movb   $0x0,-0x1(%rbp)
jmp    147b <ReadPacket+0x31b>
mov    -0x15(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadPacket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ecx
mov    %rsi,%r14
mov    %edi,%ebp
movq   $0x0,(%rsi)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <ReadChars@plt>
test   %eax,%eax
je     12a7 <ReadPacket+0x157>
cmpb   $0x0,0x7(%rsp)
je     11e9 <ReadPacket+0x99>
lea    0x6(%rsp),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
test   %eax,%eax
je     12bb <ReadPacket+0x16b>
cmpb   $0x20,0x6(%rsp)
jne    12ab <ReadPacket+0x15b>
mov    0x2e2a(%rip),%r12
movq   $0x0,(%r12)
lea    0x6(%rsp),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
test   %eax,%eax
je     12bb <ReadPacket+0x16b>
lea    0xe1e(%rip),%r13
lea    0x6(%rsp),%r15
jmp    1221 <ReadPacket+0xd1>
lea    0xed0(%rip),%rdi
jmp    12b2 <ReadPacket+0x162>
add    %ebx,%ebx
lea    (%rbx,%rbx,4),%ebx
nopw   0x0(%rax,%rax,1)
movq   $0x0,(%r12)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
test   %eax,%eax
je     12a7 <ReadPacket+0x157>
movsbl 0x6(%rsp),%eax
add    $0xfffffff6,%eax
cmp    $0x2f,%eax
ja     12e8 <ReadPacket+0x198>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
add    %ebx,%ebx
lea    (%rbx,%rbx,4),%ebx
or     $0x1,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x2,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x3,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x4,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x5,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x6,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x7,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x8,%ebx
jmp    1200 <ReadPacket+0xb0>
lea    (%rbx,%rbx,4),%eax
lea    (%rax,%rax,1),%ebx
add    $0x9,%ebx
jmp    1200 <ReadPacket+0xb0>
xor    %ebx,%ebx
jmp    12bb <ReadPacket+0x16b>
lea    0xe24(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <Log@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0xffff,%ebx
jb     12f1 <ReadPacket+0x1a1>
lea    0xe71(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <Log@plt>
xor    %ebx,%ebx
jmp    12bb <ReadPacket+0x16b>
lea    0xe1c(%rip),%rdi
jmp    12b2 <ReadPacket+0x162>
mov    %ebx,%r15d
lea    0x1(%r15),%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %ebx,%ebx
je     131b <ReadPacket+0x1cb>
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
mov    %eax,%ecx
mov    (%r14),%rax
test   %ecx,%ecx
je     135b <ReadPacket+0x20b>
movb   $0x0,(%rax,%r15,1)
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <ReadChars@plt>
test   %eax,%eax
je     1347 <ReadPacket+0x1f7>
cmpb   $0xa,0x6(%rsp)
jne    134f <ReadPacket+0x1ff>
mov    0x7(%rsp),%bl
jmp    12bb <ReadPacket+0x16b>
mov    (%r14),%rdi
add    $0x1,%ebx
jmp    1361 <ReadPacket+0x211>
lea    0xe08(%rip),%rdi
jmp    12b2 <ReadPacket+0x162>
add    $0x1,%ebx
mov    %rax,%rdi
mov    %ebx,%esi
call   1070 <explicit_bzero@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
xor    %ebx,%ebx
jmp    12bb <ReadPacket+0x16b>

LLVM IR (-O0):
@.str = [22 x i8] c"invalid packet type 0\00"
@.str.1 = [53 x i8] c"invalid character after packet type, expecting space\00"
@errno = global i64 0
@.str.2 = [65 x i8] c"invalid character during packet length, expecting 0-9 or newline\00"
@.str.3 = [18 x i8] c"invalid length %d\00"
@.str.4 = [6 x i8] c"mlock\00"
@.str.5 = [58 x i8] c"invalid character after packet message, expecting newline\00"
define signext i8 @ReadPacket(i32 %0, i8** %1, i32 %2) {
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%11 = load i8**, i8*** %6
store i8* null, i8** %11
%12 = load i32, i32* %5
%13 = load i32, i32* %7
%14 = call i32 @ReadChars(i32 %12, i8* %8, i32 1, i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %3
store i8 0, i8* %4
br label %155
17: ; preds = %3
%18 = load i8, i8* %8
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %4
br label %155
23: ; preds = %17
%24 = load i32, i32* %5
%25 = call i32 @ReadChars(i32 %24, i8* %9, i32 1, i32 0)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %23
store i8 0, i8* %4
br label %155
28: ; preds = %23
%29 = load i8, i8* %9
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 32
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
store i8 0, i8* %4
br label %155
34: ; preds = %28
store i32 0, i32* %10
br label %35
35: ; preds = %86, %34
store i64 0, i64* @errno
%36 = load i32, i32* %5
%37 = call i32 @ReadChars(i32 %36, i8* %9, i32 1, i32 0)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %35
store i8 0, i8* %4
br label %155
40: ; preds = %35
%41 = load i8, i8* %9
%42 = sext i8 %41 to i32
switch i32 %42, label %84 [
i32 10, label %43
i32 48, label %44
i32 49, label %48
i32 50, label %52
i32 51, label %56
i32 52, label %60
i32 53, label %64
i32 54, label %68
i32 55, label %72
i32 56, label %76
i32 57, label %80
]
43: ; preds = %40
br label %87
44: ; preds = %40
%45 = load i32, i32* %10
%46 = mul nsw i32 %45, 10
%47 = add nsw i32 %46, 0
store i32 %47, i32* %10
br label %86
48: ; preds = %40
%49 = load i32, i32* %10
%50 = mul nsw i32 %49, 10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %86
52: ; preds = %40
%53 = load i32, i32* %10
%54 = mul nsw i32 %53, 10
%55 = add nsw i32 %54, 2
store i32 %55, i32* %10
br label %86
56: ; preds = %40
%57 = load i32, i32* %10
%58 = mul nsw i32 %57, 10
%59 = add nsw i32 %58, 3
store i32 %59, i32* %10
br label %86
60: ; preds = %40
%61 = load i32, i32* %10
%62 = mul nsw i32 %61, 10
%63 = add nsw i32 %62, 4
store i32 %63, i32* %10
br label %86
64: ; preds = %40
%65 = load i32, i32* %10
%66 = mul nsw i32 %65, 10
%67 = add nsw i32 %66, 5
store i32 %67, i32* %10
br label %86
68: ; preds = %40
%69 = load i32, i32* %10
%70 = mul nsw i32 %69, 10
%71 = add nsw i32 %70, 6
store i32 %71, i32* %10
br label %86
72: ; preds = %40
%73 = load i32, i32* %10
%74 = mul nsw i32 %73, 10
%75 = add nsw i32 %74, 7
store i32 %75, i32* %10
br label %86
76: ; preds = %40
%77 = load i32, i32* %10
%78 = mul nsw i32 %77, 10
%79 = add nsw i32 %78, 8
store i32 %79, i32* %10
br label %86
80: ; preds = %40
%81 = load i32, i32* %10
%82 = mul nsw i32 %81, 10
%83 = add nsw i32 %82, 9
store i32 %83, i32* %10
br label %86
84: ; preds = %40
%85 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0))
store i8 0, i8* %4
br label %155
86: ; preds = %80, %76, %72, %68, %64, %60, %56, %52, %48, %44
br label %35
87: ; preds = %43
%88 = load i32, i32* %10
%89 = icmp slt i32 %88, 0
br i1 %89, label %93, label %90
90: ; preds = %87
%91 = load i32, i32* %10
%92 = icmp sge i32 %91, 65535
br i1 %92, label %93, label %96
93: ; preds = %90, %87
%94 = load i32, i32* %10
%95 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %94)
store i8 0, i8* %4
br label %155
96: ; preds = %90
%97 = load i32, i32* %10
%98 = sext i32 %97 to i64
%99 = add i64 %98, 1
%100 = call i8* @malloc(i64 %99)
%101 = load i8**, i8*** %6
store i8* %100, i8** %101
%102 = load i8, i8* %8
%103 = sext i8 %102 to i32
%104 = icmp eq i32 %103, 112
br i1 %104, label %105, label %108
105: ; preds = %96
br i1 false, label %106, label %108
106: ; preds = %105
%107 = call i32 @LogErrno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %108
108: ; preds = %106, %105, %96
%109 = load i32, i32* %10
%110 = icmp ne i32 %109, 0
br i1 %110, label %111, label %128
111: ; preds = %108
%112 = load i32, i32* %5
%113 = load i8**, i8*** %6
%114 = load i8*, i8** %113
%115 = load i32, i32* %10
%116 = call i32 @ReadChars(i32 %112, i8* %114, i32 %115, i32 0)
%117 = icmp ne i32 %116, 0
br i1 %117, label %128, label %118
118: ; preds = %111
%119 = load i8**, i8*** %6
%120 = load i8*, i8** %119
%121 = load i32, i32* %10
%122 = add nsw i32 %121, 1
%123 = call i32 @explicit_bzero(i8* %120, i32 %122)
%124 = load i8**, i8*** %6
%125 = load i8*, i8** %124
%126 = call i32 @free(i8* %125)
%127 = load i8**, i8*** %6
store i8* null, i8** %127
store i8 0, i8* %4
br label %155
128: ; preds = %111, %108
%129 = load i8**, i8*** %6
%130 = load i8*, i8** %129
%131 = load i32, i32* %10
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds i8, i8* %130, i64 %132
store i8 0, i8* %133
%134 = load i32, i32* %5
%135 = call i32 @ReadChars(i32 %134, i8* %9, i32 1, i32 0)
%136 = icmp ne i32 %135, 0
br i1 %136, label %147, label %137
137: ; preds = %128
%138 = load i8**, i8*** %6
%139 = load i8*, i8** %138
%140 = load i32, i32* %10
%141 = add nsw i32 %140, 1
%142 = call i32 @explicit_bzero(i8* %139, i32 %141)
%143 = load i8**, i8*** %6
%144 = load i8*, i8** %143
%145 = call i32 @free(i8* %144)
%146 = load i8**, i8*** %6
store i8* null, i8** %146
store i8 0, i8* %4
br label %155
147: ; preds = %128
%148 = load i8, i8* %9
%149 = sext i8 %148 to i32
%150 = icmp ne i32 %149, 10
br i1 %150, label %151, label %153
151: ; preds = %147
%152 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
store i8 0, i8* %4
br label %155
153: ; preds = %147
%154 = load i8, i8* %8
store i8 %154, i8* %4
br label %155
155: ; preds = %153, %151, %137, %118, %93, %84, %39, %32, %27, %21, %16
%156 = load i8, i8* %4
ret i8 %156
}

LLVM IR (-O2):
@.str = [22 x i8] c"invalid packet type 0\00"
@.str.1 = [53 x i8] c"invalid character after packet type, expecting space\00"
@errno = global i64 0
@.str.2 = [65 x i8] c"invalid character during packet length, expecting 0-9 or newline\00"
@.str.3 = [18 x i8] c"invalid length %d\00"
@.str.5 = [58 x i8] c"invalid character after packet message, expecting newline\00"
define signext i8 @ReadPacket(i32 %0, i8** nocapture %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8
store i8* null, i8** %1
%6 = call i32 @ReadChars(i32 %0, i8* %4, i32 1, i32 %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %101, label %8
8: ; preds = %3
%9 = load i8, i8* %4
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %101
13: ; preds = %8
%14 = call i32 @ReadChars(i32 %0, i8* %5, i32 1, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %99, label %16
16: ; preds = %13
%17 = load i8, i8* %5
%18 = icmp eq i8 %17, 32
br i1 %18, label %19, label %22
19: ; preds = %16
store i64 0, i64* @errno
%20 = call i32 @ReadChars(i32 %0, i8* %5, i32 1, i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %99, label %24
22: ; preds = %16
%23 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %99
24: ; preds = %19, %59
%25 = phi i32 [ %60, %59 ], [ 0, %19 ]
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
switch i32 %27, label %57 [
i32 10, label %63
i32 48, label %28
i32 49, label %30
i32 50, label %33
i32 51, label %36
i32 52, label %39
i32 53, label %42
i32 54, label %45
i32 55, label %48
i32 56, label %51
i32 57, label %54
]
28: ; preds = %24
%29 = mul nsw i32 %25, 10
br label %59
30: ; preds = %24
%31 = mul nsw i32 %25, 10
%32 = or i32 %31, 1
br label %59
33: ; preds = %24
%34 = mul nsw i32 %25, 10
%35 = add nsw i32 %34, 2
br label %59
36: ; preds = %24
%37 = mul nsw i32 %25, 10
%38 = add nsw i32 %37, 3
br label %59
39: ; preds = %24
%40 = mul nsw i32 %25, 10
%41 = add nsw i32 %40, 4
br label %59
42: ; preds = %24
%43 = mul nsw i32 %25, 10
%44 = add nsw i32 %43, 5
br label %59
45: ; preds = %24
%46 = mul nsw i32 %25, 10
%47 = add nsw i32 %46, 6
br label %59
48: ; preds = %24
%49 = mul nsw i32 %25, 10
%50 = add nsw i32 %49, 7
br label %59
51: ; preds = %24
%52 = mul nsw i32 %25, 10
%53 = add nsw i32 %52, 8
br label %59
54: ; preds = %24
%55 = mul nsw i32 %25, 10
%56 = add nsw i32 %55, 9
br label %59
57: ; preds = %24
%58 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0))
br label %99
59: ; preds = %54, %51, %48, %45, %42, %39, %36, %33, %30, %28
%60 = phi i32 [ %56, %54 ], [ %53, %51 ], [ %50, %48 ], [ %47, %45 ], [ %44, %42 ], [ %41, %39 ], [ %38, %36 ], [ %35, %33 ], [ %32, %30 ], [ %29, %28 ]
store i64 0, i64* @errno
%61 = call i32 @ReadChars(i32 %0, i8* %5, i32 1, i32 0)
%62 = icmp eq i32 %61, 0
br i1 %62, label %99, label %24
63: ; preds = %24
%64 = icmp ugt i32 %25, 65534
br i1 %64, label %65, label %67
65: ; preds = %63
%66 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %99
67: ; preds = %63
%68 = zext i32 %25 to i64
%69 = add nuw nsw i64 %68, 1
%70 = call i8* @malloc(i64 %69)
store i8* %70, i8** %1
%71 = icmp eq i32 %25, 0
br i1 %71, label %81, label %72
72: ; preds = %67
%73 = call i32 @ReadChars(i32 %0, i8* %70, i32 %25, i32 0)
%74 = icmp eq i32 %73, 0
%75 = load i8*, i8** %1
br i1 %74, label %76, label %81
76: ; preds = %72
%77 = add nuw nsw i32 %25, 1
%78 = call i32 @explicit_bzero(i8* %75, i32 %77)
%79 = load i8*, i8** %1
%80 = call i32 @free(i8* %79)
store i8* null, i8** %1
br label %99
81: ; preds = %72, %67
%82 = phi i8* [ %70, %67 ], [ %75, %72 ]
%83 = getelementptr inbounds i8, i8* %82, i64 %68
store i8 0, i8* %83
%84 = call i32 @ReadChars(i32 %0, i8* %5, i32 1, i32 0)
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %92
86: ; preds = %81
%87 = load i8*, i8** %1
%88 = add nuw nsw i32 %25, 1
%89 = call i32 @explicit_bzero(i8* %87, i32 %88)
%90 = load i8*, i8** %1
%91 = call i32 @free(i8* %90)
store i8* null, i8** %1
br label %99
92: ; preds = %81
%93 = load i8, i8* %5
%94 = icmp eq i8 %93, 10
br i1 %94, label %97, label %95
95: ; preds = %92
%96 = call i32 (i8*, ...) @Log(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
br label %99
97: ; preds = %92
%98 = load i8, i8* %4
br label %99
99: ; preds = %59, %19, %57, %65, %76, %86, %95, %97, %13, %22
%100 = phi i8 [ 0, %22 ], [ 0, %13 ], [ 0, %57 ], [ 0, %65 ], [ 0, %95 ], [ %98, %97 ], [ 0, %86 ], [ 0, %76 ], [ 0, %19 ], [ 0, %59 ]
br label %101
101: ; preds = %3, %99, %11
%102 = phi i8 [ 0, %11 ], [ %100, %99 ], [ 0, %3 ]
ret i8 %102
}