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

/* Variables and functions */
int _get_result(int);
int _hex_to_int(char const *, int);
int /*<<< orphan*/ _write_crc(int, int *, int, int *);
int /*<<< orphan*/ _write_crc_byte(int, int, int *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ write(int, int *, int);
int _write_out(void *ctx, uint32_t addr, uint8_t len, const char *s) {
  int fd = *(int *)ctx;
  printf("0x%x: %u bytes ", addr, len);
  uint8_t crc = 0xFF;
  _write_crc_byte(fd, 'w', &crc);
  _write_crc_byte(fd, sizeof(addr) + len, &crc);
  _write_crc(fd, &addr, sizeof(addr), &crc);
  while (len--) {
    uint8_t byte = _hex_to_int(s, 2);
    _write_crc_byte(fd, byte, &crc);
    s += 2;
  }
  write(fd, &crc, 1);
  return _get_result(fd);
}


x86 assembly (clang, -O0):
<_write_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe74(%rip),%rdi
call   1050 <printf@plt>
movl   $0xff,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x77,%esi
lea    -0x20(%rbp),%rdx
call   1060 <_write_crc_byte@plt>
mov    -0x1c(%rbp),%edi
movslq -0x10(%rbp),%rax
add    $0x4,%rax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1060 <_write_crc_byte@plt>
mov    -0x1c(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
lea    -0x20(%rbp),%rcx
call   1070 <_write_crc@plt>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
cmp    $0x0,%eax
je     1219 <_write_out+0xb9>
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   1030 <_hex_to_int@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1060 <_write_crc_byte@plt>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    11d4 <_write_out+0x74>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1080 <_get_result@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_out>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %esi,0x4(%rsp)
mov    (%rdi),%r14d
lea    0xe86(%rip),%rdi
call   1050 <printf@plt>
movl   $0xff,(%rsp)
mov    %rsp,%r15
mov    %r14d,%edi
mov    $0x77,%esi
mov    %r15,%rdx
call   1060 <_write_crc_byte@plt>
lea    0x4(%rbp),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   1060 <_write_crc_byte@plt>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
mov    $0x4,%edx
mov    %r15,%rcx
call   1070 <_write_crc@plt>
test   %ebp,%ebp
je     11f3 <_write_out+0x93>
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <_hex_to_int@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1060 <_write_crc_byte@plt>
add    $0x2,%rbx
add    $0xffffffff,%ebp
jne    11d0 <_write_out+0x70>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r14d,%edi
call   1080 <_get_result@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"0x%x: %u bytes \00"
define i32 @_write_out(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%12 = load i8*, i8** %5
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
store i32 %14, i32* %9
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
store i32 255, i32* %10
%18 = load i32, i32* %9
%19 = call i32 @_write_crc_byte(i32 %18, i32 119, i32* %10)
%20 = load i32, i32* %9
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = add i64 4, %22
%24 = trunc i64 %23 to i32
%25 = call i32 @_write_crc_byte(i32 %20, i32 %24, i32* %10)
%26 = load i32, i32* %9
%27 = call i32 @_write_crc(i32 %26, i32* %6, i32 4, i32* %10)
br label %28
28: ; preds = %32, %4
%29 = load i32, i32* %7
%30 = add nsw i32 %29, -1
store i32 %30, i32* %7
%31 = icmp ne i32 %29, 0
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i8*, i8** %8
%34 = call i32 @_hex_to_int(i8* %33, i32 2)
store i32 %34, i32* %11
%35 = load i32, i32* %9
%36 = load i32, i32* %11
%37 = call i32 @_write_crc_byte(i32 %35, i32 %36, i32* %10)
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i64 2
store i8* %39, i8** %8
br label %28
40: ; preds = %28
%41 = load i32, i32* %9
%42 = call i32 @write(i32 %41, i32* %10, i32 1)
%43 = load i32, i32* %9
%44 = call i32 @_get_result(i32 %43)
ret i32 %44
}

LLVM IR (-O2):
@.str = [16 x i8] c"0x%x: %u bytes \00"
define i32 @_write_out(i8* nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
store i32 %1, i32* %5
%7 = bitcast i8* %0 to i32*
%8 = load i32, i32* %7
%9 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%10 = bitcast i32* %6 to i8*
store i32 255, i32* %6
%11 = call i32 @_write_crc_byte(i32 %8, i32 119, i32* %6)
%12 = add i32 %2, 4
%13 = call i32 @_write_crc_byte(i32 %8, i32 %12, i32* %6)
%14 = call i32 @_write_crc(i32 %8, i32* %5, i32 4, i32* %6)
%15 = icmp eq i32 %2, 0
br i1 %15, label %24, label %16
16: ; preds = %4, %16
%17 = phi i32 [ %19, %16 ], [ %2, %4 ]
%18 = phi i8* [ %22, %16 ], [ %3, %4 ]
%19 = add nsw i32 %17, -1
%20 = call i32 @_hex_to_int(i8* %18, i32 2)
%21 = call i32 @_write_crc_byte(i32 %8, i32 %20, i32* %6)
%22 = getelementptr inbounds i8, i8* %18, i64 2
%23 = icmp eq i32 %19, 0
br i1 %23, label %24, label %16
24: ; preds = %16, %4
%25 = call i32 @write(i32 %8, i32* %6, i32 1)
%26 = call i32 @_get_result(i32 %8)
ret i32 %26
}