#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 digit2hex(unsigned char);
int inbyte();
int /*<<< orphan*/ outbyte(char);
void putpacket(unsigned char *buffer) {
  unsigned char checksum;
  int count;
  unsigned char ch;
  do {
    outbyte('$');
    checksum = 0;
    count = 0;
    while (ch = buffer[count]) {
      if (!outbyte(ch))
        return;
      checksum += ch;
      count += 1;
    }
    outbyte('#');
    outbyte(digit2hex(checksum >> 4));
    outbyte(digit2hex(checksum & 0xf));
  } while ((inbyte() & 0x7f) != '+');
}


x86 assembly (clang, -O0):
<putpacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x24,%edi
call   1050 <outbyte@plt>
movb   $0x0,-0x9(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
cmp    $0x0,%al
je     1199 <putpacket+0x69>
movsbl -0x11(%rbp),%edi
call   1050 <outbyte@plt>
cmp    $0x0,%eax
jne    117e <putpacket+0x4e>
jmp    11e4 <putpacket+0xb4>
movzbl -0x11(%rbp),%ecx
movzbl -0x9(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x9(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1151 <putpacket+0x21>
mov    $0x23,%edi
call   1050 <outbyte@plt>
movzbl -0x9(%rbp),%eax
sar    $0x4,%eax
movzbl %al,%edi
call   1030 <digit2hex@plt>
movsbl %al,%edi
call   1050 <outbyte@plt>
movzbl -0x9(%rbp),%eax
and    $0xf,%eax
movzbl %al,%edi
call   1030 <digit2hex@plt>
movsbl %al,%edi
call   1050 <outbyte@plt>
mov    $0x0,%al
call   1040 <inbyte@plt>
and    $0x7f,%eax
cmp    $0x2b,%eax
jne    113c <putpacket+0xc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putpacket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
lea    0x1(%rdi),%r14
jmp    1194 <putpacket+0x64>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x23,%edi
call   1050 <outbyte@plt>
mov    %r12d,%eax
shr    $0x4,%al
movzbl %al,%edi
call   1030 <digit2hex@plt>
movsbl %al,%edi
call   1050 <outbyte@plt>
and    $0xf,%r12b
movzbl %r12b,%edi
call   1030 <digit2hex@plt>
movsbl %al,%edi
call   1050 <outbyte@plt>
xor    %eax,%eax
call   1040 <inbyte@plt>
and    $0x7f,%eax
cmp    $0x2b,%eax
je     11cd <putpacket+0x9d>
mov    $0x24,%edi
call   1050 <outbyte@plt>
mov    (%r15),%bl
xor    %r12d,%r12d
test   %bl,%bl
je     1150 <putpacket+0x20>
mov    %r14,%rbp
nopl   0x0(%rax,%rax,1)
movsbl %bl,%edi
call   1050 <outbyte@plt>
test   %eax,%eax
je     11cd <putpacket+0x9d>
add    %bl,%r12b
movzbl 0x0(%rbp),%ebx
add    $0x1,%rbp
test   %bl,%bl
jne    11b0 <putpacket+0x80>
jmp    1150 <putpacket+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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