#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 */
char highhex(int);
char lowhex(int);
char serial_getc();
int /*<<< orphan*/ serial_putc(char);
void putpacket(register char *buffer) {
  register int checksum;
  do {
    char *src = buffer;
    serial_putc('$');
    checksum = 0;
    while (*src) {
      int runlen;
      for (runlen = 0; runlen < 100; runlen++) {
        if (src[0] != src[runlen] || runlen == 99) {
          if (runlen > 3) {
            int encode;
            serial_putc(*src);
            checksum += *src;
            serial_putc('*');
            checksum += '*';
            checksum += (encode = runlen + ' ' - 4);
            serial_putc(encode);
            src += runlen;
          } else {
            serial_putc(*src);
            checksum += *src;
            src++;
          }
          break;
        }
      }
    }
    serial_putc('#');
    serial_putc(highhex(checksum));
    serial_putc(lowhex(checksum));
  } while (serial_getc() != '+');
}


x86 assembly (clang, -O0):
<putpacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x24,%edi
call   1040 <serial_putc@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1253 <putpacket+0x113>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    124e <putpacket+0x10e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    11a8 <putpacket+0x68>
cmpl   $0x63,-0x1c(%rbp)
jne    123b <putpacket+0xfb>
cmpl   $0x3,-0x1c(%rbp)
jle    1211 <putpacket+0xd1>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <serial_putc@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x2a,%edi
call   1040 <serial_putc@plt>
mov    -0xc(%rbp),%eax
add    $0x2a,%eax
mov    %eax,-0xc(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%eax
sub    $0x4,%eax
mov    %eax,-0x20(%rbp)
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%eax
movsbl %al,%edi
call   1040 <serial_putc@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1236 <putpacket+0xf6>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <serial_putc@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    124e <putpacket+0x10e>
jmp    1240 <putpacket+0x100>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1179 <putpacket+0x39>
jmp    1165 <putpacket+0x25>
mov    $0x23,%edi
call   1040 <serial_putc@plt>
mov    -0xc(%rbp),%edi
call   1050 <highhex@plt>
movsbl %al,%edi
call   1040 <serial_putc@plt>
mov    -0xc(%rbp),%edi
call   1030 <lowhex@plt>
movsbl %al,%edi
call   1040 <serial_putc@plt>
mov    $0x0,%al
call   1060 <serial_getc@plt>
movsbl %al,%eax
cmp    $0x2b,%eax
jne    114c <putpacket+0xc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putpacket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
jmp    1189 <putpacket+0x49>
mov    $0x23,%edi
call   1040 <serial_putc@plt>
mov    %r15d,%edi
call   1050 <highhex@plt>
movsbl %al,%edi
call   1040 <serial_putc@plt>
mov    %r15d,%edi
call   1030 <lowhex@plt>
movsbl %al,%edi
call   1040 <serial_putc@plt>
xor    %eax,%eax
call   1060 <serial_getc@plt>
cmp    $0x2b,%al
je     121e <putpacket+0xde>
mov    $0x24,%edi
call   1040 <serial_putc@plt>
mov    (%r14),%al
mov    $0x0,%r15d
test   %al,%al
je     1150 <putpacket+0x10>
xor    %r15d,%r15d
mov    %r14,%r12
jmp    11cc <putpacket+0x8c>
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1040 <serial_putc@plt>
movsbl (%r12),%eax
add    %eax,%r15d
add    $0x1,%r12
mov    (%r12),%al
test   %al,%al
je     1150 <putpacket+0x10>
xor    %ecx,%ecx
xchg   %ax,%ax
lea    0x1(%rcx),%rbx
cmp    0x1(%r12,%rcx,1),%al
jne    11e4 <putpacket+0xa4>
cmp    $0x62,%rcx
mov    %rbx,%rcx
jne    11d0 <putpacket+0x90>
cmp    $0x4,%ebx
jb     11b0 <putpacket+0x70>
lea    (%r12,%rbx,1),%r13
movsbl %al,%edi
call   1040 <serial_putc@plt>
movsbl (%r12),%ebp
mov    $0x2a,%edi
call   1040 <serial_putc@plt>
add    %ebp,%r15d
add    %ebx,%r15d
add    $0x46,%r15d
add    $0x1c,%ebx
movsbl %bl,%edi
call   1040 <serial_putc@plt>
mov    %r13,%r12
jmp    11c4 <putpacket+0x84>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @putpacket(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
br label %7
7: ; preds = %85, %1
%8 = load i8*, i8** %2
store i8* %8, i8** %4
%9 = call i32 @serial_putc(i8 signext 36)
store i32 0, i32* %3
br label %10
10: ; preds = %76, %7
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
br i1 %13, label %14, label %77
14: ; preds = %10
store i32 0, i32* %5
br label %15
15: ; preds = %73, %14
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 100
br i1 %17, label %18, label %76
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %22, %28
br i1 %29, label %33, label %30
30: ; preds = %18
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, 99
br i1 %32, label %33, label %72
33: ; preds = %30, %18
%34 = load i32, i32* %5
%35 = icmp sgt i32 %34, 3
br i1 %35, label %36, label %60
36: ; preds = %33
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = call i32 @serial_putc(i8 signext %38)
%40 = load i8*, i8** %4
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %42
store i32 %44, i32* %3
%45 = call i32 @serial_putc(i8 signext 42)
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 42
store i32 %47, i32* %3
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 32
%50 = sub nsw i32 %49, 4
store i32 %50, i32* %6
%51 = load i32, i32* %3
%52 = add nsw i32 %51, %50
store i32 %52, i32* %3
%53 = load i32, i32* %6
%54 = trunc i32 %53 to i8
%55 = call i32 @serial_putc(i8 signext %54)
%56 = load i32, i32* %5
%57 = load i8*, i8** %4
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8* %59, i8** %4
br label %71
60: ; preds = %33
%61 = load i8*, i8** %4
%62 = load i8, i8* %61
%63 = call i32 @serial_putc(i8 signext %62)
%64 = load i8*, i8** %4
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = load i32, i32* %3
%68 = add nsw i32 %67, %66
store i32 %68, i32* %3
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %4
br label %71
71: ; preds = %60, %36
br label %76
72: ; preds = %30
br label %73
73: ; preds = %72
%74 = load i32, i32* %5
%75 = add nsw i32 %74, 1
store i32 %75, i32* %5
br label %15
76: ; preds = %71, %15
br label %10
77: ; preds = %10
%78 = call i32 @serial_putc(i8 signext 35)
%79 = load i32, i32* %3
%80 = call signext i8 @highhex(i32 %79)
%81 = call i32 @serial_putc(i8 signext %80)
%82 = load i32, i32* %3
%83 = call signext i8 @lowhex(i32 %82)
%84 = call i32 @serial_putc(i8 signext %83)
br label %85
85: ; preds = %77
%86 = call signext i8 (...) @serial_getc()
%87 = sext i8 %86 to i32
%88 = icmp ne i32 %87, 43
br i1 %88, label %7, label %89
89: ; preds = %85
ret void
}

LLVM IR (-O2):
define void @putpacket(i8* nocapture %0) {
br label %2
2: ; preds = %45, %1
%3 = tail call i32 @serial_putc(i8 signext 36)
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %45, label %6
6: ; preds = %2, %40
%7 = phi i8 [ %43, %40 ], [ %4, %2 ]
%8 = phi i32 [ %42, %40 ], [ 0, %2 ]
%9 = phi i8* [ %41, %40 ], [ %0, %2 ]
br label %10
10: ; preds = %6, %10
%11 = phi i64 [ 1, %6 ], [ %17, %10 ]
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = icmp ne i8 %7, %13
%15 = icmp eq i64 %11, 99
%16 = select i1 %14, i1 true, i1 %15
%17 = add nuw nsw i64 %11, 1
br i1 %16, label %18, label %10
18: ; preds = %10
%19 = trunc i64 %11 to i32
%20 = icmp ugt i32 %19, 3
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = and i64 %11, 4294967295
%23 = getelementptr inbounds i8, i8* %9, i64 %22
%24 = tail call i32 @serial_putc(i8 signext %7)
%25 = load i8, i8* %9
%26 = sext i8 %25 to i32
%27 = tail call i32 @serial_putc(i8 signext 42)
%28 = add nuw nsw i32 %19, 28
%29 = add i32 %8, 42
%30 = add i32 %29, %28
%31 = add i32 %30, %26
%32 = trunc i32 %28 to i8
%33 = tail call i32 @serial_putc(i8 signext %32)
br label %40
34: ; preds = %18
%35 = tail call i32 @serial_putc(i8 signext %7)
%36 = load i8, i8* %9
%37 = sext i8 %36 to i32
%38 = add nsw i32 %8, %37
%39 = getelementptr inbounds i8, i8* %9, i64 1
br label %40
40: ; preds = %21, %34
%41 = phi i8* [ %23, %21 ], [ %39, %34 ]
%42 = phi i32 [ %31, %21 ], [ %38, %34 ]
%43 = load i8, i8* %41
%44 = icmp eq i8 %43, 0
br i1 %44, label %45, label %6
45: ; preds = %40, %2
%46 = phi i32 [ 0, %2 ], [ %42, %40 ]
%47 = tail call i32 @serial_putc(i8 signext 35)
%48 = tail call signext i8 @highhex(i32 %46)
%49 = tail call i32 @serial_putc(i8 signext %48)
%50 = tail call signext i8 @lowhex(i32 %46)
%51 = tail call i32 @serial_putc(i8 signext %50)
%52 = tail call signext i8 (...) @serial_getc()
%53 = icmp eq i8 %52, 43
br i1 %53, label %54, label %2
54: ; preds = %45
ret void
}