#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;

/* Variables and functions */
int /*<<< orphan*/ binary_to_hexascii(char *, char *, int);
scalar_t__ check_CRC(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int rt_modbus_min_bytes(char *);
int rt_modbus_process_request(char *, int);
int rt_modbus_total_bytes(char *, int);
void doit(uint8_t buf[]) {
  int min_size;
  int total_size;
  int reply_size;
  uint8_t ascii[1024];
  min_size = rt_modbus_min_bytes(buf);
  printf("Min size = %d\n", min_size);
  total_size = rt_modbus_total_bytes(buf, min_size);
  printf("Total size = %d\n", total_size);
  if (check_CRC(buf, total_size, 0xFFFF)) {
    printf("CRC matches\n");
  } else {
    printf("*** BAD CRC\n");
  }
  binary_to_hexascii(ascii, buf, total_size);
  printf("Message: %s\n", ascii);
  reply_size = rt_modbus_process_request(buf, total_size);
  printf("Reply size = %d\n", reply_size);
  binary_to_hexascii(ascii, buf, reply_size);
  printf("Reply: %s\n", ascii);
  if (check_CRC(buf, reply_size, 0xFFFF)) {
    printf("CRC matches\n");
  } else {
    printf("*** BAD CRC\n");
  }
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <rt_modbus_min_bytes@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1080 <rt_modbus_total_bytes@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    $0xffff,%edx
call   1050 <check_CRC@plt>
cmp    $0x0,%rax
je     11da <doit+0x7a>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e8 <doit+0x88>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <binary_to_hexascii@plt>
lea    -0x420(%rbp),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <rt_modbus_process_request@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <binary_to_hexascii@plt>
lea    -0x420(%rbp),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0xffff,%edx
call   1050 <check_CRC@plt>
cmp    $0x0,%rax
je     1286 <doit+0x126>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1294 <doit+0x134>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
call   1070 <rt_modbus_min_bytes@plt>
mov    %eax,%ebp
lea    0xe70(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <rt_modbus_total_bytes@plt>
mov    %eax,%ebp
lea    0xe63(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0xffff,%edx
call   1060 <check_CRC@plt>
test   %rax,%rax
lea    0xe7b(%rip),%r12
lea    0xe80(%rip),%r14
mov    %r14,%rdi
cmove  %r12,%rdi
call   1030 <puts@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <binary_to_hexascii@plt>
lea    0xe28(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <rt_modbus_process_request@plt>
mov    %eax,%ebp
lea    0xe18(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <binary_to_hexascii@plt>
lea    0xe0c(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0xffff,%edx
call   1060 <check_CRC@plt>
test   %rax,%rax
cmove  %r12,%r14
mov    %r14,%rdi
call   1030 <puts@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Min size = %d\0A\00"
@.str.1 = [17 x i8] c"Total size = %d\0A\00"
@.str.2 = [13 x i8] c"CRC matches\0A\00"
@.str.3 = [13 x i8] c"*** BAD CRC\0A\00"
@.str.4 = [13 x i8] c"Message: %s\0A\00"
@.str.5 = [17 x i8] c"Reply size = %d\0A\00"
@.str.6 = [11 x i8] c"Reply: %s\0A\00"
define void @doit(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @rt_modbus_min_bytes(i8* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %2
%12 = load i32, i32* %3
%13 = call i32 @rt_modbus_total_bytes(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %2
%17 = load i32, i32* %4
%18 = call i64 @check_CRC(i8* %16, i32 %17, i32 65535)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %24
22: ; preds = %1
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %20
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%26 = load i8*, i8** %2
%27 = load i32, i32* %4
%28 = call i32 @binary_to_hexascii(i8* %25, i8* %26, i32 %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %2
%32 = load i32, i32* %4
%33 = call i32 @rt_modbus_process_request(i8* %31, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = load i8*, i8** %2
%38 = load i32, i32* %5
%39 = call i32 @binary_to_hexascii(i8* %36, i8* %37, i32 %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %40)
%42 = load i8*, i8** %2
%43 = load i32, i32* %5
%44 = call i64 @check_CRC(i8* %42, i32 %43, i32 65535)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %24
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %50
48: ; preds = %24
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Min size = %d\0A\00"
@.str.1 = [17 x i8] c"Total size = %d\0A\00"
@.str.4 = [13 x i8] c"Message: %s\0A\00"
@.str.5 = [17 x i8] c"Reply size = %d\0A\00"
@.str.6 = [11 x i8] c"Reply: %s\0A\00"
@str.7 = [12 x i8] c"*** BAD CRC\00"
@str.9 = [12 x i8] c"CRC matches\00"
define void @doit(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @rt_modbus_min_bytes(i8* %0)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @rt_modbus_total_bytes(i8* %0, i32 %4)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = tail call i64 @check_CRC(i8* %0, i32 %6, i32 65535)
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0)
%11 = tail call i32 @puts(i8* dereferenceable(1) %10)
%12 = call i32 @binary_to_hexascii(i8* %3, i8* %0, i32 %6)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%14 = call i32 @rt_modbus_process_request(i8* %0, i32 %6)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %14)
%16 = call i32 @binary_to_hexascii(i8* %3, i8* %0, i32 %14)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %3)
%18 = call i64 @check_CRC(i8* %0, i32 %14, i32 65535)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0)
%21 = call i32 @puts(i8* dereferenceable(1) %20)
ret void
}