#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 */
typedef char uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char int_to_hex(char);
int log_getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void put_packet(FILE *fp, char *packet) {
  putc('$', fp);
  uint8_t checksum = 0;
  char *ptr;
  for (ptr = packet; *ptr != 0; ptr++) {
    assert(*ptr != '$');
    assert(*ptr != '#');
    putc(*ptr, fp);
    checksum += *ptr;
  }
  putc('#', fp);
  putc(int_to_hex(checksum >> 4), fp);
  putc(int_to_hex(checksum & 0xf), fp);
  fprintf(stderr, "sent: '%s'\n", packet);
  int ch = log_getc(fp);
  if (ch != '+') {
    fprintf(stderr, "Unexpected ack char: '%c' (%i)\n", ch, ch);
  }
}


x86 assembly (clang, -O0):
<put_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    $0x24,%edi
call   1040 <putc@plt>
movb   $0x0,-0x11(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11eb <put_packet+0x9b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
movsbl -0x11(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x11(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    117a <put_packet+0x2a>
mov    -0x8(%rbp),%rsi
mov    $0x23,%edi
call   1040 <putc@plt>
movsbl -0x11(%rbp),%eax
sar    $0x4,%eax
movsbl %al,%edi
call   1070 <int_to_hex@plt>
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
movsbl -0x11(%rbp),%eax
and    $0xf,%eax
movsbl %al,%edi
call   1070 <int_to_hex@plt>
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <log_getc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x2b,-0x24(%rbp)
je     127d <put_packet+0x12d>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x24,%edi
mov    %r15,%rsi
call   1040 <putc@plt>
mov    (%r14),%al
xor    %ebp,%ebp
test   %al,%al
je     11ba <put_packet+0x6a>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
cmp    $0x24,%al
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
cmpb   $0x23,(%rbx)
setne  %dil
call   1050 <assert@plt>
movsbl (%rbx),%edi
mov    %r15,%rsi
call   1040 <putc@plt>
movzbl (%rbx),%eax
add    %ebp,%eax
movsbl %al,%ebp
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1180 <put_packet+0x30>
mov    $0x23,%edi
mov    %r15,%rsi
call   1040 <putc@plt>
mov    %ebp,%edi
sar    $0x4,%edi
call   1070 <int_to_hex@plt>
movsbl %al,%edi
mov    %r15,%rsi
call   1040 <putc@plt>
and    $0xf,%ebp
mov    %ebp,%edi
call   1070 <int_to_hex@plt>
movsbl %al,%edi
mov    %r15,%rsi
call   1040 <putc@plt>
mov    0x2de8(%rip),%rbx
mov    (%rbx),%edi
lea    0xdff(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <log_getc@plt>
cmp    $0x2b,%eax
jne    1223 <put_packet+0xd3>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%edi
lea    0xde0(%rip),%rsi
mov    %eax,%edx
mov    %eax,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"sent: '%s'\0A\00"
@.str.1 = [32 x i8] c"Unexpected ack char: '%c' (%i)\0A\00"
define void @put_packet(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%8 = load i32*, i32** %3
%9 = call i32 @putc(i8 signext 36, i32* %8)
store i8 0, i8* %5
%10 = load i8*, i8** %4
store i8* %10, i8** %6
br label %11
11: ; preds = %40, %2
%12 = load i8*, i8** %6
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %11
%17 = load i8*, i8** %6
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 36
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %6
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 35
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i8*, i8** %6
%30 = load i8, i8* %29
%31 = load i32*, i32** %3
%32 = call i32 @putc(i8 signext %30, i32* %31)
%33 = load i8*, i8** %6
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = load i8, i8* %5
%37 = sext i8 %36 to i32
%38 = add nsw i32 %37, %35
%39 = trunc i32 %38 to i8
store i8 %39, i8* %5
br label %40
40: ; preds = %16
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %6
br label %11
43: ; preds = %11
%44 = load i32*, i32** %3
%45 = call i32 @putc(i8 signext 35, i32* %44)
%46 = load i8, i8* %5
%47 = sext i8 %46 to i32
%48 = ashr i32 %47, 4
%49 = trunc i32 %48 to i8
%50 = call signext i8 @int_to_hex(i8 signext %49)
%51 = load i32*, i32** %3
%52 = call i32 @putc(i8 signext %50, i32* %51)
%53 = load i8, i8* %5
%54 = sext i8 %53 to i32
%55 = and i32 %54, 15
%56 = trunc i32 %55 to i8
%57 = call signext i8 @int_to_hex(i8 signext %56)
%58 = load i32*, i32** %3
%59 = call i32 @putc(i8 signext %57, i32* %58)
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %4
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %61)
%63 = load i32*, i32** %3
%64 = call i32 @log_getc(i32* %63)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp ne i32 %65, 43
br i1 %66, label %67, label %72
67: ; preds = %43
%68 = load i32, i32* @stderr
%69 = load i32, i32* %7
%70 = load i32, i32* %7
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %69, i32 %70)
br label %72
72: ; preds = %67, %43
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"sent: '%s'\0A\00"
@.str.1 = [32 x i8] c"Unexpected ack char: '%c' (%i)\0A\00"
define void @put_packet(i32* %0, i8* %1) {
%3 = tail call i32 @putc(i8 signext 36, i32* %0)
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %2, %6
%7 = phi i8 [ %25, %6 ], [ %4, %2 ]
%8 = phi i32 [ %24, %6 ], [ 0, %2 ]
%9 = phi i8* [ %22, %6 ], [ %1, %2 ]
%10 = icmp ne i8 %7, 36
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = load i8, i8* %9
%14 = icmp ne i8 %13, 35
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = load i8, i8* %9
%18 = tail call i32 @putc(i8 signext %17, i32* %0)
%19 = load i8, i8* %9
%20 = zext i8 %19 to i32
%21 = add nsw i32 %8, %20
%22 = getelementptr inbounds i8, i8* %9, i64 1
%23 = shl i32 %21, 24
%24 = ashr exact i32 %23, 24
%25 = load i8, i8* %22
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %6
27: ; preds = %6, %2
%28 = phi i32 [ 0, %2 ], [ %24, %6 ]
%29 = tail call i32 @putc(i8 signext 35, i32* %0)
%30 = ashr i32 %28, 4
%31 = trunc i32 %30 to i8
%32 = tail call signext i8 @int_to_hex(i8 signext %31)
%33 = tail call i32 @putc(i8 signext %32, i32* %0)
%34 = trunc i32 %28 to i8
%35 = and i8 %34, 15
%36 = tail call signext i8 @int_to_hex(i8 signext %35)
%37 = tail call i32 @putc(i8 signext %36, i32* %0)
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %1)
%40 = tail call i32 @log_getc(i32* %0)
%41 = icmp eq i32 %40, 43
br i1 %41, label %45, label %42
42: ; preds = %27
%43 = load i32, i32* @stderr
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %40)
br label %45
45: ; preds = %42, %27
ret void
}