#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 */

/* Variables and functions */
int /*<<< orphan*/ Calc_CRC(unsigned char, unsigned int *);
unsigned int modbus_rtu_crc16(unsigned char *encoded_message, int length) {
  unsigned char *ptr = encoded_message;
  unsigned int CRC16;
  int i;
  CRC16 = 0xFFFF;
  for (i = 0; i < length; i++) {
    Calc_CRC(ptr[i], &CRC16);
  }
  return (CRC16);
}


x86 assembly (clang, -O0):
<modbus_rtu_crc16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0xffff,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1164 <modbus_rtu_crc16+0x54>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
lea    -0x1c(%rbp),%rsi
movzbl (%rax,%rcx,1),%edi
call   1030 <Calc_CRC@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1135 <modbus_rtu_crc16+0x25>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<modbus_rtu_crc16>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movl   $0xffff,0x4(%rsp)
test   %esi,%esi
jle    115c <modbus_rtu_crc16+0x4c>
mov    %rdi,%r15
mov    %esi,%r12d
xor    %ebx,%ebx
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%r15,%rbx,1),%edi
mov    %r14,%rsi
call   1030 <Calc_CRC@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <modbus_rtu_crc16+0x30>
mov    0x4(%rsp),%eax
jmp    1161 <modbus_rtu_crc16+0x51>
mov    $0xffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @modbus_rtu_crc16(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
store i8* %8, i8** %5
store i32 65535, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %20, %2
%10 = load i32, i32* %7
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load i8*, i8** %5
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @Calc_CRC(i8 zeroext %18, i32* %6)
br label %20
20: ; preds = %13
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
br label %9
23: ; preds = %9
%24 = load i32, i32* %6
ret i32 %24
}

LLVM IR (-O2):
define i32 @modbus_rtu_crc16(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 65535, i32* %3
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %13, %8 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = call i32 @Calc_CRC(i8 zeroext %11, i32* %3)
%13 = add nuw nsw i64 %9, 1
%14 = icmp eq i64 %13, %7
br i1 %14, label %15, label %8
15: ; preds = %8
%16 = load i32, i32* %3
br label %17
17: ; preds = %15, %2
%18 = phi i32 [ %16, %15 ], [ 65535, %2 ]
ret i32 %18
}