#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_Send_Data(int *, int);
int /*<<< orphan*/ RS485_Transmitter_Enable(int);
int /*<<< orphan*/ RS485_Turnaround_Delay();
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];
  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;
  RS485_Turnaround_Delay();
  RS485_Transmitter_Enable(true);
  RS485_Send_Data(buffer, 8);
  if (data_len) {
    for (i = 0; i < data_len; i++) {
      crc16 = CRC_Calc_Data(data[i], crc16);
    }
    crc16 = ~crc16;
    buffer[0] = (crc16 & 0x00FF);
    buffer[1] = ((crc16 & 0xFF00) >> 8);
    RS485_Send_Data(data, data_len);
    RS485_Send_Data(buffer, 2);
  }
  RS485_Transmitter_Enable(false);
}


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,-0x54(%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   1050 <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   1050 <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   1050 <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   1050 <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   1050 <CRC_Calc_Header@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1060 <RS485_Turnaround_Delay@plt>
mov    $0x1,%edi
call   1040 <RS485_Transmitter_Enable@plt>
lea    -0x50(%rbp),%rdi
mov    $0x8,%esi
call   1030 <RS485_Send_Data@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1298 <MSTP_Send_Frame+0x148>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    125c <MSTP_Send_Frame+0x10c>
mov    -0x18(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x24(%rbp),%esi
call   1070 <CRC_Calc_Data@plt>
mov    %eax,-0x24(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    122c <MSTP_Send_Frame+0xdc>
mov    -0x24(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x50(%rbp)
mov    -0x24(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <RS485_Send_Data@plt>
lea    -0x50(%rbp),%rdi
mov    $0x2,%esi
call   1030 <RS485_Send_Data@plt>
xor    %edi,%edi
call   1040 <RS485_Transmitter_Enable@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    $0x20,%rsp
mov    %r8d,%r14d
mov    %rcx,%r12
mov    %edx,%r15d
mov    %esi,%ebp
movabs $0xff00000055,%rax
mov    %rax,(%rsp)
mov    %edi,0x8(%rsp)
mov    $0xff,%esi
call   1050 <CRC_Calc_Header@plt>
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <CRC_Calc_Header@plt>
mov    %r15d,0x10(%rsp)
mov    %r15d,%edi
mov    %eax,%esi
call   1050 <CRC_Calc_Header@plt>
mov    %r14d,%edi
sar    $0x8,%edi
mov    %edi,0x14(%rsp)
mov    %eax,%esi
call   1050 <CRC_Calc_Header@plt>
mov    %r14d,0x18(%rsp)
mov    %r14d,%edi
mov    %eax,%esi
call   1050 <CRC_Calc_Header@plt>
not    %eax
mov    %eax,0x1c(%rsp)
xor    %eax,%eax
call   1060 <RS485_Turnaround_Delay@plt>
mov    $0x1,%edi
call   1040 <RS485_Transmitter_Enable@plt>
mov    %rsp,%rdi
mov    $0x8,%esi
call   1030 <RS485_Send_Data@plt>
test   %r14d,%r14d
je     1242 <MSTP_Send_Frame+0xf2>
jle    1218 <MSTP_Send_Frame+0xc8>
mov    %r14d,%ebp
mov    $0xffff,%eax
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12,%rbx,4),%edi
mov    %eax,%esi
call   1070 <CRC_Calc_Data@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1200 <MSTP_Send_Frame+0xb0>
not    %eax
jmp    121d <MSTP_Send_Frame+0xcd>
mov    $0xffff0000,%eax
movzbl %al,%ecx
mov    %ecx,(%rsp)
movzbl %ah,%eax
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <RS485_Send_Data@plt>
mov    %rsp,%rdi
mov    $0x2,%esi
call   1030 <RS485_Send_Data@plt>
xor    %edi,%edi
call   1040 <RS485_Transmitter_Enable@plt>
add    $0x20,%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 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* %14
%15 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
store i32 85, i32* %15
%16 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 1
store i32 255, i32* %16
%17 = load i32, i32* %6
%18 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 2
store i32 %17, i32* %18
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 2
%20 = load i32, i32* %19
%21 = load i32, i32* %11
%22 = call i32 @CRC_Calc_Header(i32 %20, i32 %21)
store i32 %22, i32* %11
%23 = load i32, i32* %7
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 3
store i32 %23, i32* %24
%25 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 3
%26 = load i32, i32* %25
%27 = load i32, i32* %11
%28 = call i32 @CRC_Calc_Header(i32 %26, i32 %27)
store i32 %28, i32* %11
%29 = load i32, i32* %8
%30 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 4
store i32 %29, i32* %30
%31 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 4
%32 = load i32, i32* %31
%33 = load i32, i32* %11
%34 = call i32 @CRC_Calc_Header(i32 %32, i32 %33)
store i32 %34, i32* %11
%35 = load i32, i32* %10
%36 = ashr i32 %35, 8
%37 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 5
store i32 %36, i32* %37
%38 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 5
%39 = load i32, i32* %38
%40 = load i32, i32* %11
%41 = call i32 @CRC_Calc_Header(i32 %39, i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %10
%43 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 6
store i32 %42, i32* %43
%44 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 6
%45 = load i32, i32* %44
%46 = load i32, i32* %11
%47 = call i32 @CRC_Calc_Header(i32 %45, i32 %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = xor i32 %48, -1
%50 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 7
store i32 %49, i32* %50
%51 = call i32 (...) @RS485_Turnaround_Delay()
%52 = call i32 @RS485_Transmitter_Enable(i32 1)
%53 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
%54 = call i32 @RS485_Send_Data(i32* %53, i32 8)
%55 = load i32, i32* %10
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %88
57: ; preds = %5
store i32 0, i32* %14
br label %58
58: ; preds = %70, %57
%59 = load i32, i32* %14
%60 = load i32, i32* %10
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %73
62: ; preds = %58
%63 = load i32*, i32** %9
%64 = load i32, i32* %14
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
%68 = load i32, i32* %12
%69 = call i32 @CRC_Calc_Data(i32 %67, i32 %68)
store i32 %69, i32* %12
br label %70
70: ; preds = %62
%71 = load i32, i32* %14
%72 = add nsw i32 %71, 1
store i32 %72, i32* %14
br label %58
73: ; preds = %58
%74 = load i32, i32* %12
%75 = xor i32 %74, -1
store i32 %75, i32* %12
%76 = load i32, i32* %12
%77 = and i32 %76, 255
%78 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
store i32 %77, i32* %78
%79 = load i32, i32* %12
%80 = and i32 %79, 65280
%81 = ashr i32 %80, 8
%82 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 1
store i32 %81, i32* %82
%83 = load i32*, i32** %9
%84 = load i32, i32* %10
%85 = call i32 @RS485_Send_Data(i32* %83, i32 %84)
%86 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
%87 = call i32 @RS485_Send_Data(i32* %86, i32 2)
br label %88
88: ; preds = %73, %5
%89 = call i32 @RS485_Transmitter_Enable(i32 0)
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 = bitcast [8 x i32]* %6 to i8*
%8 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 0
store i32 85, i32* %8
%9 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 1
store i32 255, i32* %9
%10 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 2
store i32 %0, i32* %10
%11 = tail call i32 @CRC_Calc_Header(i32 %0, i32 255)
%12 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 3
store i32 %1, i32* %12
%13 = tail call i32 @CRC_Calc_Header(i32 %1, i32 %11)
%14 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 4
store i32 %2, i32* %14
%15 = tail call i32 @CRC_Calc_Header(i32 %2, i32 %13)
%16 = ashr i32 %4, 8
%17 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 5
store i32 %16, i32* %17
%18 = tail call i32 @CRC_Calc_Header(i32 %16, i32 %15)
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 6
store i32 %4, i32* %19
%20 = tail call i32 @CRC_Calc_Header(i32 %4, i32 %18)
%21 = xor i32 %20, -1
%22 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 7
store i32 %21, i32* %22
%23 = tail call i32 (...) @RS485_Turnaround_Delay()
%24 = tail call i32 @RS485_Transmitter_Enable(i32 1)
%25 = call i32 @RS485_Send_Data(i32* %8, i32 8)
%26 = icmp eq i32 %4, 0
br i1 %26, label %48, label %27
27: ; preds = %5
%28 = icmp sgt i32 %4, 0
br i1 %28, label %29, label %41
29: ; preds = %27
%30 = zext i32 %4 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %37, %31 ]
%33 = phi i32 [ 65535, %29 ], [ %36, %31 ]
%34 = getelementptr inbounds i32, i32* %3, i64 %32
%35 = load i32, i32* %34
%36 = call i32 @CRC_Calc_Data(i32 %35, i32 %33)
%37 = add nuw nsw i64 %32, 1
%38 = icmp eq i64 %37, %30
br i1 %38, label %39, label %31
39: ; preds = %31
%40 = xor i32 %36, -1
br label %41
41: ; preds = %39, %27
%42 = phi i32 [ -65536, %27 ], [ %40, %39 ]
%43 = and i32 %42, 255
store i32 %43, i32* %8
%44 = lshr i32 %42, 8
%45 = and i32 %44, 255
store i32 %45, i32* %9
%46 = call i32 @RS485_Send_Data(i32* %3, i32 %4)
%47 = call i32 @RS485_Send_Data(i32* %8, i32 2)
br label %48
48: ; preds = %41, %5
%49 = call i32 @RS485_Transmitter_Enable(i32 0)
ret void
}