#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 */
unsigned char hex2digit(int);
int inbyte();
int /*<<< orphan*/ outbyte(unsigned char);
int strlen(unsigned char *);
void getpacket(unsigned char *buffer) {
  unsigned char checksum;
  unsigned char xmitcsum;
  int i;
  int count;
  unsigned char ch;
  do {
    while ((ch = (inbyte() & 0x7f)) != '$')
      ;
    checksum = 0;
    xmitcsum = -1;
    count = 0;
    while (count < 2048) {
      ch = inbyte() & 0x7f;
      if (ch == '#')
        break;
      checksum = checksum + ch;
      buffer[count] = ch;
      count = count + 1;
    }
    if (count >= 2048)
      continue;
    buffer[count] = 0;
    if (ch == '#') {
      xmitcsum = hex2digit(inbyte() & 0x7f) << 4;
      xmitcsum |= hex2digit(inbyte() & 0x7f);
      outbyte('+');
      return;
      if (checksum != xmitcsum)
        outbyte('-');
      else {
        outbyte('+');
        if (buffer[2] == ':') {
          outbyte(buffer[0]);
          outbyte(buffer[1]);
          count = strlen(buffer);
          for (i = 3; i <= count; i++)
            buffer[i - 3] = buffer[i];
        }
      }
    }
  } while (checksum != xmitcsum);
}


x86 assembly (clang, -O0):
<getpacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
jmp    1141 <getpacket+0x11>
mov    $0x0,%al
call   1040 <inbyte@plt>
and    $0x7f,%eax
mov    %al,-0x15(%rbp)
movzbl %al,%eax
cmp    $0x24,%eax
je     115f <getpacket+0x2f>
jmp    1141 <getpacket+0x11>
movb   $0x0,-0x9(%rbp)
movb   $0xff,-0xa(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x800,-0x14(%rbp)
jge    11c3 <getpacket+0x93>
mov    $0x0,%al
call   1040 <inbyte@plt>
and    $0x7f,%eax
mov    %al,-0x15(%rbp)
movzbl -0x15(%rbp),%eax
cmp    $0x23,%eax
jne    119a <getpacket+0x6a>
jmp    11c3 <getpacket+0x93>
movzbl -0x9(%rbp),%eax
movzbl -0x15(%rbp),%ecx
add    %ecx,%eax
mov    %al,-0x9(%rbp)
mov    -0x15(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116e <getpacket+0x3e>
cmpl   $0x800,-0x14(%rbp)
jl     11d5 <getpacket+0xa5>
jmp    1239 <getpacket+0x109>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movzbl -0x15(%rbp),%eax
cmp    $0x23,%eax
jne    1234 <getpacket+0x104>
mov    $0x0,%al
call   1040 <inbyte@plt>
mov    %eax,%edi
and    $0x7f,%edi
call   1030 <hex2digit@plt>
movzbl %al,%eax
shl    $0x4,%eax
mov    %al,-0xa(%rbp)
mov    $0x0,%al
call   1040 <inbyte@plt>
mov    %eax,%edi
and    $0x7f,%edi
call   1030 <hex2digit@plt>
movzbl %al,%ecx
movzbl -0xa(%rbp),%eax
or     %ecx,%eax
mov    %al,-0xa(%rbp)
mov    $0x2b,%edi
call   1050 <outbyte@plt>
jmp    1249 <getpacket+0x119>
jmp    1239 <getpacket+0x109>
movzbl -0x9(%rbp),%eax
movzbl -0xa(%rbp),%ecx
cmp    %ecx,%eax
jne    113c <getpacket+0xc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpacket>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <inbyte@plt>
and    $0x7f,%eax
cmp    $0x24,%eax
jne    1138 <getpacket+0x8>
xor    %ebx,%ebx
xor    %r15d,%r15d
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <inbyte@plt>
and    $0x7f,%al
cmp    $0x23,%al
je     117d <getpacket+0x4d>
add    %al,%r15b
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0x800,%rbx
jne    1150 <getpacket+0x20>
cmp    $0xff,%r15b
jne    1138 <getpacket+0x8>
pop    %rbx
pop    %r14
pop    %r15
ret
movb   $0x0,(%r14,%rbx,1)
xor    %eax,%eax
call   1040 <inbyte@plt>
and    $0x7f,%eax
mov    %eax,%edi
call   1030 <hex2digit@plt>
xor    %eax,%eax
call   1040 <inbyte@plt>
and    $0x7f,%eax
mov    %eax,%edi
call   1030 <hex2digit@plt>
mov    $0x2b,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <outbyte@plt>

LLVM IR (-O0):
define void @getpacket(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %2
br label %8
8: ; preds = %71, %1
br label %9
9: ; preds = %15, %8
%10 = call i32 (...) @inbyte()
%11 = and i32 %10, 127
%12 = trunc i32 %11 to i8
store i8 %12, i8* %7
%13 = zext i8 %12 to i32
%14 = icmp ne i32 %13, 36
br i1 %14, label %15, label %16
15: ; preds = %9
br label %9
16: ; preds = %9
store i8 0, i8* %3
store i8 -1, i8* %4
store i32 0, i32* %6
br label %17
17: ; preds = %28, %16
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 2048
br i1 %19, label %20, label %42
20: ; preds = %17
%21 = call i32 (...) @inbyte()
%22 = and i32 %21, 127
%23 = trunc i32 %22 to i8
store i8 %23, i8* %7
%24 = load i8, i8* %7
%25 = zext i8 %24 to i32
%26 = icmp eq i32 %25, 35
br i1 %26, label %27, label %28
27: ; preds = %20
br label %42
28: ; preds = %20
%29 = load i8, i8* %3
%30 = zext i8 %29 to i32
%31 = load i8, i8* %7
%32 = zext i8 %31 to i32
%33 = add nsw i32 %30, %32
%34 = trunc i32 %33 to i8
store i8 %34, i8* %3
%35 = load i8, i8* %7
%36 = load i8*, i8** %2
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %17
42: ; preds = %27, %17
%43 = load i32, i32* %6
%44 = icmp sge i32 %43, 2048
br i1 %44, label %45, label %46
45: ; preds = %42
br label %71
46: ; preds = %42
%47 = load i8*, i8** %2
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i8, i8* %7
%52 = zext i8 %51 to i32
%53 = icmp eq i32 %52, 35
br i1 %53, label %54, label %70
54: ; preds = %46
%55 = call i32 (...) @inbyte()
%56 = and i32 %55, 127
%57 = call zeroext i8 @hex2digit(i32 %56)
%58 = zext i8 %57 to i32
%59 = shl i32 %58, 4
%60 = trunc i32 %59 to i8
store i8 %60, i8* %4
%61 = call i32 (...) @inbyte()
%62 = and i32 %61, 127
%63 = call zeroext i8 @hex2digit(i32 %62)
%64 = zext i8 %63 to i32
%65 = load i8, i8* %4
%66 = zext i8 %65 to i32
%67 = or i32 %66, %64
%68 = trunc i32 %67 to i8
store i8 %68, i8* %4
%69 = call i32 @outbyte(i8 zeroext 43)
br label %77
70: ; preds = %46
br label %71
71: ; preds = %70, %45
%72 = load i8, i8* %3
%73 = zext i8 %72 to i32
%74 = load i8, i8* %4
%75 = zext i8 %74 to i32
%76 = icmp ne i32 %73, %75
br i1 %76, label %8, label %77
77: ; preds = %54, %71
ret void
}

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