#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char hex_to_int(void *);
void *log_getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void get_packet(FILE *fp, char *buffer, int buffer_size) {
  while (1) {
    while (1) {
      int ch = log_getc(fp);
      if (ch == '$')
        break;
      fprintf(stderr, "Unexpected char: '%c' (%i)\n", ch, ch);
    }
    int count = 0;
    uint8_t checksum = 0;
    while (1) {
      assert(count < buffer_size);
      char ch = log_getc(fp);
      if (ch == '#')
        break;
      checksum += ch;
      buffer[count++] = ch;
    }
    buffer[count] = 0;
    uint8_t received_checksum = hex_to_int(log_getc(fp)) << 4;
    received_checksum += hex_to_int(log_getc(fp));
    if (received_checksum != checksum) {
      fprintf(stderr, "got bad checksum: 0x%02x != 0x%02x\n", received_checksum,
              checksum);
      putc('-', fp);
    } else {
      putc('+', fp);
    }
    fflush(fp);
    if (received_checksum == checksum) {
      fprintf(stderr, "received: '%s'\n", buffer);
      return;
    }
  }
}


x86 assembly (clang, -O0):
<get_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
jmp    1178 <get_packet+0x18>
mov    -0x8(%rbp),%rdi
call   1030 <log_getc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x24,-0x18(%rbp)
jne    1193 <get_packet+0x33>
jmp    11b5 <get_packet+0x55>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1178 <get_packet+0x18>
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <log_getc@plt>
mov    %al,-0x1e(%rbp)
movsbl -0x1e(%rbp),%eax
cmp    $0x23,%eax
jne    11f1 <get_packet+0x91>
jmp    121b <get_packet+0xbb>
movsbl -0x1e(%rbp),%ecx
movsbl -0x1d(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1d(%rbp)
mov    -0x1e(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11c0 <get_packet+0x60>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1030 <log_getc@plt>
mov    %rax,%rdi
call   1080 <hex_to_int@plt>
movsbl %al,%eax
shl    $0x4,%eax
mov    %al,-0x1f(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <log_getc@plt>
mov    %rax,%rdi
call   1080 <hex_to_int@plt>
movsbl %al,%ecx
movsbl -0x1f(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1f(%rbp)
movsbl -0x1f(%rbp),%eax
movsbl -0x1d(%rbp),%ecx
cmp    %ecx,%eax
je     12a0 <get_packet+0x140>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
movsbl -0x1f(%rbp),%edx
movsbl -0x1d(%rbp),%ecx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rsi
mov    $0x2d,%edi
call   1040 <putc@plt>
jmp    12ae <get_packet+0x14e>
mov    -0x8(%rbp),%rsi
mov    $0x2b,%edi
call   1040 <putc@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fflush@plt>
movsbl -0x1f(%rbp),%eax
movsbl -0x1d(%rbp),%ecx
cmp    %ecx,%eax
jne    12e8 <get_packet+0x188>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret
jmp    1173 <get_packet+0x13>

x86 assembly (clang, -O2):
<get_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r14
xor    %eax,%eax
test   %edx,%edx
setg   %al
mov    %eax,0xc(%rsp)
movslq %edx,%r12
mov    0x2e55(%rip),%rbp
nopl   0x0(%rax,%rax,1)
lea    0xe69(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <log_getc@plt>
cmp    $0x24,%eax
je     11c0 <get_packet+0x60>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11a0 <get_packet+0x40>
mov    0xc(%rsp),%edi
call   1050 <assert@plt>
mov    %r14,%rdi
call   1030 <log_getc@plt>
xor    %ebp,%ebp
xor    %r15d,%r15d
cmp    $0x23,%al
jne    11f0 <get_packet+0x90>
mov    0x10(%rsp),%rbx
jmp    1224 <get_packet+0xc4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
add    %al,%r15b
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
xor    %edi,%edi
cmp    %r12,%rbp
setl   %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1030 <log_getc@plt>
cmp    $0x23,%al
jne    1200 <get_packet+0xa0>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1030 <log_getc@plt>
mov    %rax,%rdi
call   1080 <hex_to_int@plt>
mov    %eax,%r13d
shl    $0x4,%r13b
mov    %r14,%rdi
call   1030 <log_getc@plt>
mov    %rax,%rdi
call   1080 <hex_to_int@plt>
add    %r13b,%al
cmp    %r15b,%al
je     1292 <get_packet+0x132>
movsbl %r15b,%ecx
movsbl %al,%edx
mov    0x2d79(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xdab(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2d,%edi
mov    %r14,%rsi
call   1040 <putc@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
jmp    1190 <get_packet+0x30>
mov    $0x2b,%edi
mov    %r14,%rsi
call   1040 <putc@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Unexpected char: '%c' (%i)\0A\00"
@.str.1 = [36 x i8] c"got bad checksum: 0x%02x != 0x%02x\0A\00"
@.str.2 = [16 x i8] c"received: '%s'\0A\00"
define void @get_packet(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %12
12: ; preds = %3, %100
br label %13
13: ; preds = %12, %20
%14 = load i32*, i32** %4
%15 = call i8* @log_getc(i32* %14)
%16 = ptrtoint i8* %15 to i32
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 36
br i1 %18, label %19, label %20
19: ; preds = %13
br label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* %7
%23 = load i32, i32* %7
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23)
br label %13
25: ; preds = %19
store i32 0, i32* %8
store i8 0, i8* %9
br label %26
26: ; preds = %25, %39
%27 = load i32, i32* %8
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32*, i32** %4
%33 = call i8* @log_getc(i32* %32)
%34 = ptrtoint i8* %33 to i8
store i8 %34, i8* %10
%35 = load i8, i8* %10
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 35
br i1 %37, label %38, label %39
38: ; preds = %26
br label %52
39: ; preds = %26
%40 = load i8, i8* %10
%41 = sext i8 %40 to i32
%42 = load i8, i8* %9
%43 = sext i8 %42 to i32
%44 = add nsw i32 %43, %41
%45 = trunc i32 %44 to i8
store i8 %45, i8* %9
%46 = load i8, i8* %10
%47 = load i8*, i8** %5
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %8
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 %46, i8* %51
br label %26
52: ; preds = %38
%53 = load i8*, i8** %5
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
%57 = load i32*, i32** %4
%58 = call i8* @log_getc(i32* %57)
%59 = call signext i8 @hex_to_int(i8* %58)
%60 = sext i8 %59 to i32
%61 = shl i32 %60, 4
%62 = trunc i32 %61 to i8
store i8 %62, i8* %11
%63 = load i32*, i32** %4
%64 = call i8* @log_getc(i32* %63)
%65 = call signext i8 @hex_to_int(i8* %64)
%66 = sext i8 %65 to i32
%67 = load i8, i8* %11
%68 = sext i8 %67 to i32
%69 = add nsw i32 %68, %66
%70 = trunc i32 %69 to i8
store i8 %70, i8* %11
%71 = load i8, i8* %11
%72 = sext i8 %71 to i32
%73 = load i8, i8* %9
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %72, %74
br i1 %75, label %76, label %85
76: ; preds = %52
%77 = load i32, i32* @stderr
%78 = load i8, i8* %11
%79 = sext i8 %78 to i32
%80 = load i8, i8* %9
%81 = sext i8 %80 to i32
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %79, i32 %81)
%83 = load i32*, i32** %4
%84 = call i32 @putc(i8 signext 45, i32* %83)
br label %88
85: ; preds = %52
%86 = load i32*, i32** %4
%87 = call i32 @putc(i8 signext 43, i32* %86)
br label %88
88: ; preds = %85, %76
%89 = load i32*, i32** %4
%90 = call i32 @fflush(i32* %89)
%91 = load i8, i8* %11
%92 = sext i8 %91 to i32
%93 = load i8, i8* %9
%94 = sext i8 %93 to i32
%95 = icmp eq i32 %92, %94
br i1 %95, label %96, label %100
96: ; preds = %88
%97 = load i32, i32* @stderr
%98 = load i8*, i8** %5
%99 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %98)
ret void
100: ; preds = %88
br label %12
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Unexpected char: '%c' (%i)\0A\00"
@.str.1 = [36 x i8] c"got bad checksum: 0x%02x != 0x%02x\0A\00"
@.str.2 = [16 x i8] c"received: '%s'\0A\00"
define void @get_packet(i32* %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
%5 = zext i1 %4 to i32
%6 = sext i32 %2 to i64
br label %7
7: ; preds = %52, %3
%8 = tail call i8* @log_getc(i32* %0)
%9 = ptrtoint i8* %8 to i64
%10 = trunc i64 %9 to i32
%11 = icmp eq i32 %10, 36
br i1 %11, label %12, label %18
12: ; preds = %18, %7
%13 = tail call i32 @assert(i32 %5)
%14 = tail call i8* @log_getc(i32* %0)
%15 = ptrtoint i8* %14 to i64
%16 = trunc i64 %15 to i8
%17 = icmp eq i8 %16, 35
br i1 %17, label %40, label %26
18: ; preds = %7, %18
%19 = phi i32 [ %24, %18 ], [ %10, %7 ]
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %19)
%22 = tail call i8* @log_getc(i32* %0)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %24, 36
br i1 %25, label %12, label %18
26: ; preds = %12, %26
%27 = phi i64 [ %31, %26 ], [ 0, %12 ]
%28 = phi i8 [ %38, %26 ], [ %16, %12 ]
%29 = phi i8 [ %30, %26 ], [ 0, %12 ]
%30 = add i8 %28, %29
%31 = add nuw i64 %27, 1
%32 = getelementptr inbounds i8, i8* %1, i64 %27
store i8 %28, i8* %32
%33 = icmp slt i64 %31, %6
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
%36 = tail call i8* @log_getc(i32* %0)
%37 = ptrtoint i8* %36 to i64
%38 = trunc i64 %37 to i8
%39 = icmp eq i8 %38, 35
br i1 %39, label %40, label %26
40: ; preds = %26, %12
%41 = phi i64 [ 0, %12 ], [ %31, %26 ]
%42 = phi i8 [ 0, %12 ], [ %30, %26 ]
%43 = and i64 %41, 4294967295
%44 = getelementptr inbounds i8, i8* %1, i64 %43
store i8 0, i8* %44
%45 = tail call i8* @log_getc(i32* %0)
%46 = tail call signext i8 @hex_to_int(i8* %45)
%47 = shl i8 %46, 4
%48 = tail call i8* @log_getc(i32* %0)
%49 = tail call signext i8 @hex_to_int(i8* %48)
%50 = add i8 %49, %47
%51 = icmp eq i8 %50, %42
br i1 %51, label %59, label %52
52: ; preds = %40
%53 = sext i8 %42 to i32
%54 = sext i8 %50 to i32
%55 = load i32, i32* @stderr
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %54, i32 %53)
%57 = tail call i32 @putc(i8 signext 45, i32* %0)
%58 = tail call i32 @fflush(i32* %0)
br label %7
59: ; preds = %40
%60 = tail call i32 @putc(i8 signext 43, i32* %0)
%61 = tail call i32 @fflush(i32* %0)
%62 = load i32, i32* @stderr
%63 = tail call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %1)
ret void
}