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

/* Variables and functions */
int CRC_Calc_Data(int, int);
int CRC_Calc_Header(int, int);
int /*<<< orphan*/ rs485_bytes_send(int *, int);
void MSTP_Send_Frame(uint8_t frame_type, uint8_t destination, uint8_t source,
                     uint8_t *data, uint16_t data_len) {
  uint8_t crc8 = 0xFF;
  uint16_t crc16 = 0xFFFF;
  uint8_t buffer[8];
  uint8_t buffer_crc[2];
  uint16_t i = 0;
  buffer[0] = 0x55;
  buffer[1] = 0xFF;
  buffer[2] = frame_type;
  crc8 = CRC_Calc_Header(buffer[2], crc8);
  buffer[3] = destination;
  crc8 = CRC_Calc_Header(buffer[3], crc8);
  buffer[4] = source;
  crc8 = CRC_Calc_Header(buffer[4], crc8);
  buffer[5] = ((uint8_t)((uint16_t)(data_len) >> 8));
  crc8 = CRC_Calc_Header(buffer[5], crc8);
  buffer[6] = ((uint8_t)(data_len));
  crc8 = CRC_Calc_Header(buffer[6], crc8);
  buffer[7] = ~crc8;
  if (data_len) {
    for (i = 0; i < data_len; i++) {
      crc16 = CRC_Calc_Data(data[i], crc16);
    }
    crc16 = ~crc16;
    buffer_crc[0] = (crc16 & 0x00FF);
    buffer_crc[1] = ((crc16 & 0xFF00) >> 8);
  }
  rs485_bytes_send(buffer, 8);
  if (data_len) {
    rs485_bytes_send(data, data_len);
    rs485_bytes_send(buffer_crc, 2);
  }
}


x86 assembly (clang, -O0):
<MSTP_Send_Frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0xff,-0x20(%rbp)
movl   $0xffff,-0x24(%rbp)
movl   $0x0,-0x5c(%rbp)
movl   $0x55,-0x50(%rbp)
movl   $0xff,-0x4c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <CRC_Calc_Header@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <CRC_Calc_Header@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <CRC_Calc_Header@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <CRC_Calc_Header@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <CRC_Calc_Header@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     123f <MSTP_Send_Frame+0x10f>
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    121d <MSTP_Send_Frame+0xed>
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x24(%rbp),%esi
call   1050 <CRC_Calc_Data@plt>
mov    %eax,-0x24(%rbp)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    11ed <MSTP_Send_Frame+0xbd>
mov    -0x24(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x58(%rbp)
mov    -0x24(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x8,%esi
call   1030 <rs485_bytes_send@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1271 <MSTP_Send_Frame+0x141>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <rs485_bytes_send@plt>
lea    -0x58(%rbp),%rdi
mov    $0x2,%esi
call   1030 <rs485_bytes_send@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MSTP_Send_Frame>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %r8d,%r14d
mov    %rcx,%r12
mov    %edx,%r15d
mov    %esi,%ebp
movabs $0xff00000055,%rax
mov    %rax,0x10(%rsp)
mov    %edi,0x18(%rsp)
mov    $0xff,%esi
call   1040 <CRC_Calc_Header@plt>
mov    %ebp,0x1c(%rsp)
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <CRC_Calc_Header@plt>
mov    %r15d,0x20(%rsp)
mov    %r15d,%edi
mov    %eax,%esi
call   1040 <CRC_Calc_Header@plt>
mov    %r14d,%edi
sar    $0x8,%edi
mov    %edi,0x24(%rsp)
mov    %eax,%esi
call   1040 <CRC_Calc_Header@plt>
mov    %r14d,0x28(%rsp)
mov    %r14d,%edi
mov    %eax,%esi
call   1040 <CRC_Calc_Header@plt>
not    %eax
mov    %eax,0x2c(%rsp)
test   %r14d,%r14d
je     11d8 <MSTP_Send_Frame+0xa8>
jle    11e4 <MSTP_Send_Frame+0xb4>
mov    %r14d,%ebp
mov    $0xffff,%eax
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r12,%rbx,4),%edi
mov    %eax,%esi
call   1050 <CRC_Calc_Data@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <MSTP_Send_Frame+0x90>
not    %eax
jmp    11e9 <MSTP_Send_Frame+0xb9>
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
jmp    121b <MSTP_Send_Frame+0xeb>
mov    $0xffff0000,%eax
movzbl %al,%ecx
mov    %ecx,0x8(%rsp)
movzbl %ah,%eax
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
call   1030 <rs485_bytes_send@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <rs485_bytes_send@plt>
lea    0x8(%rsp),%rdi
mov    $0x2,%esi
call   1030 <rs485_bytes_send@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @MSTP_Send_Frame(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [8 x i32]
%14 = alloca [2 x i32]
%15 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
store i32 255, i32* %11
store i32 65535, i32* %12
store i32 0, i32* %15
%16 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
store i32 85, i32* %16
%17 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 1
store i32 255, i32* %17
%18 = load i32, i32* %6
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 2
store i32 %18, i32* %19
%20 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 2
%21 = load i32, i32* %20
%22 = load i32, i32* %11
%23 = call i32 @CRC_Calc_Header(i32 %21, i32 %22)
store i32 %23, i32* %11
%24 = load i32, i32* %7
%25 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 3
store i32 %24, i32* %25
%26 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 3
%27 = load i32, i32* %26
%28 = load i32, i32* %11
%29 = call i32 @CRC_Calc_Header(i32 %27, i32 %28)
store i32 %29, i32* %11
%30 = load i32, i32* %8
%31 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 4
store i32 %30, i32* %31
%32 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 4
%33 = load i32, i32* %32
%34 = load i32, i32* %11
%35 = call i32 @CRC_Calc_Header(i32 %33, i32 %34)
store i32 %35, i32* %11
%36 = load i32, i32* %10
%37 = ashr i32 %36, 8
%38 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 5
store i32 %37, i32* %38
%39 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 5
%40 = load i32, i32* %39
%41 = load i32, i32* %11
%42 = call i32 @CRC_Calc_Header(i32 %40, i32 %41)
store i32 %42, i32* %11
%43 = load i32, i32* %10
%44 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 6
store i32 %43, i32* %44
%45 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 6
%46 = load i32, i32* %45
%47 = load i32, i32* %11
%48 = call i32 @CRC_Calc_Header(i32 %46, i32 %47)
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = xor i32 %49, -1
%51 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 7
store i32 %50, i32* %51
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %80
54: ; preds = %5
store i32 0, i32* %15
br label %55
55: ; preds = %67, %54
%56 = load i32, i32* %15
%57 = load i32, i32* %10
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %70
59: ; preds = %55
%60 = load i32*, i32** %9
%61 = load i32, i32* %15
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = load i32, i32* %12
%66 = call i32 @CRC_Calc_Data(i32 %64, i32 %65)
store i32 %66, i32* %12
br label %67
67: ; preds = %59
%68 = load i32, i32* %15
%69 = add nsw i32 %68, 1
store i32 %69, i32* %15
br label %55
70: ; preds = %55
%71 = load i32, i32* %12
%72 = xor i32 %71, -1
store i32 %72, i32* %12
%73 = load i32, i32* %12
%74 = and i32 %73, 255
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
store i32 %74, i32* %75
%76 = load i32, i32* %12
%77 = and i32 %76, 65280
%78 = ashr i32 %77, 8
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
store i32 %78, i32* %79
br label %80
80: ; preds = %70, %5
%81 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
%82 = call i32 @rs485_bytes_send(i32* %81, i32 8)
%83 = load i32, i32* %10
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %91
85: ; preds = %80
%86 = load i32*, i32** %9
%87 = load i32, i32* %10
%88 = call i32 @rs485_bytes_send(i32* %86, i32 %87)
%89 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%90 = call i32 @rs485_bytes_send(i32* %89, i32 2)
br label %91
91: ; preds = %85, %80
ret void
}

LLVM IR (-O2):
define void @MSTP_Send_Frame(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca [8 x i32]
%7 = alloca [2 x i32]
%8 = bitcast [8 x i32]* %6 to i8*
%9 = bitcast [2 x i32]* %7 to i8*
%10 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 0
store i32 85, i32* %10
%11 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 1
store i32 255, i32* %11
%12 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 2
store i32 %0, i32* %12
%13 = tail call i32 @CRC_Calc_Header(i32 %0, i32 255)
%14 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 3
store i32 %1, i32* %14
%15 = tail call i32 @CRC_Calc_Header(i32 %1, i32 %13)
%16 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 4
store i32 %2, i32* %16
%17 = tail call i32 @CRC_Calc_Header(i32 %2, i32 %15)
%18 = ashr i32 %4, 8
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 5
store i32 %18, i32* %19
%20 = tail call i32 @CRC_Calc_Header(i32 %18, i32 %17)
%21 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 6
store i32 %4, i32* %21
%22 = tail call i32 @CRC_Calc_Header(i32 %4, i32 %20)
%23 = xor i32 %22, -1
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 7
store i32 %23, i32* %24
%25 = icmp eq i32 %4, 0
br i1 %25, label %50, label %26
26: ; preds = %5
%27 = icmp sgt i32 %4, 0
br i1 %27, label %28, label %40
28: ; preds = %26
%29 = zext i32 %4 to i64
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 0, %28 ], [ %36, %30 ]
%32 = phi i32 [ 65535, %28 ], [ %35, %30 ]
%33 = getelementptr inbounds i32, i32* %3, i64 %31
%34 = load i32, i32* %33
%35 = tail call i32 @CRC_Calc_Data(i32 %34, i32 %32)
%36 = add nuw nsw i64 %31, 1
%37 = icmp eq i64 %36, %29
br i1 %37, label %38, label %30
38: ; preds = %30
%39 = xor i32 %35, -1
br label %40
40: ; preds = %26, %38
%41 = phi i32 [ -65536, %26 ], [ %39, %38 ]
%42 = and i32 %41, 255
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
store i32 %42, i32* %43
%44 = lshr i32 %41, 8
%45 = and i32 %44, 255
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
store i32 %45, i32* %46
%47 = call i32 @rs485_bytes_send(i32* %10, i32 8)
%48 = call i32 @rs485_bytes_send(i32* %3, i32 %4)
%49 = call i32 @rs485_bytes_send(i32* %43, i32 2)
br label %52
50: ; preds = %5
%51 = call i32 @rs485_bytes_send(i32* %10, i32 8)
br label %52
52: ; preds = %50, %40
ret void
}