#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 /*<<< orphan*/ add_CRC(int *, int, int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ swap16(int *);
int /*<<< orphan*/ write_register(int, int);
int rt_modbus_preset_multiple_registers(uint8_t *buf, int sz) {
  int data_bytes;
  int size;
  uint16_t start_point;
  uint16_t num_points;
  uint16_t tmp;
  int i;
  data_bytes = buf[6];
  size = 9 + data_bytes;
  memcpy(&start_point, buf + 2, 2);
  swap16(&start_point);
  memcpy(&num_points, buf + 4, 2);
  swap16(&num_points);
  printf("start = %hu, num = %hu\n", start_point, num_points);
  printf("data bytes = %hu, calculated size = %d\n", data_bytes,
         num_points * 2);
  if ((num_points * 2) == data_bytes) {
    printf("Size matches\n");
  } else {
    printf("*** Data error size does NOT match\n");
  }
  for (i = 0; i < num_points; i++) {
    memcpy(&tmp, buf + (i * 2) + 7, 2);
    swap16(&tmp);
    printf("value %d: %04hx, addr: %d\n", i, tmp, start_point + i);
    write_register(start_point + i, tmp);
  }
  add_CRC(buf, 8, 0xffff);
  return 8;
}


x86 assembly (clang, -O0):
<rt_modbus_preset_multiple_registers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x18(%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x9,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
add    $0x8,%rsi
lea    -0x18(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x18(%rbp),%rdi
call   1070 <swap16@plt>
mov    -0x8(%rbp),%rsi
add    $0x10,%rsi
lea    -0x1c(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <swap16@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0x1c(%rbp),%edx
shl    $0x1,%edx
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cmp    -0x10(%rbp),%eax
jne    11fd <rt_modbus_preset_multiple_registers+0xad>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120b <rt_modbus_preset_multiple_registers+0xbb>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1282 <rt_modbus_preset_multiple_registers+0x132>
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
add    $0x1c,%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x20(%rbp),%rdi
call   1070 <swap16@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%ecx
add    -0x24(%rbp),%ecx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
add    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1060 <write_register@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1212 <rt_modbus_preset_multiple_registers+0xc2>
mov    -0x8(%rbp),%rdi
mov    $0x8,%esi
mov    $0xffff,%edx
call   1050 <add_CRC@plt>
mov    $0x8,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rt_modbus_preset_multiple_registers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x18(%rdi),%ebp
lea    0x8(%rdi),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1080 <swap16@plt>
lea    0x10(%r14),%rsi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1080 <swap16@plt>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
add    %edx,%edx
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%eax
add    %eax,%eax
cmp    %ebp,%eax
lea    0xe93(%rip),%rax
lea    0xe69(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
cmpl   $0x0,0xc(%rsp)
jle    126f <rt_modbus_preset_multiple_registers+0x10f>
lea    0x1c(%r14),%r13
lea    0x14(%rsp),%r15
lea    0xe2e(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1080 <swap16@plt>
mov    0x14(%rsp),%edx
mov    0x10(%rsp),%ecx
add    %ebx,%ecx
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rsp),%edi
add    %ebx,%edi
mov    0x14(%rsp),%esi
call   1070 <write_register@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
add    $0x8,%r13
cmp    %rax,%rbx
jl     1220 <rt_modbus_preset_multiple_registers+0xc0>
mov    %r14,%rdi
mov    $0x8,%esi
mov    $0xffff,%edx
call   1060 <add_CRC@plt>
mov    $0x8,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"start = %hu, num = %hu\0A\00"
@.str.1 = [40 x i8] c"data bytes = %hu, calculated size = %d\0A\00"
@.str.2 = [14 x i8] c"Size matches\0A\00"
@.str.3 = [36 x i8] c"*** Data error size does NOT match\0A\00"
@.str.4 = [27 x i8] c"value %d: %04hx, addr: %d\0A\00"
define i32 @rt_modbus_preset_multiple_registers(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%11 = load i32*, i32** %3
%12 = getelementptr inbounds i32, i32* %11, i64 6
%13 = load i32, i32* %12
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = add nsw i32 9, %14
store i32 %15, i32* %6
%16 = load i32*, i32** %3
%17 = getelementptr inbounds i32, i32* %16, i64 2
%18 = call i32 @memcpy(i32* %7, i32* %17, i32 2)
%19 = call i32 @swap16(i32* %7)
%20 = load i32*, i32** %3
%21 = getelementptr inbounds i32, i32* %20, i64 4
%22 = call i32 @memcpy(i32* %8, i32* %21, i32 2)
%23 = call i32 @swap16(i32* %8)
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* %5
%28 = load i32, i32* %8
%29 = mul nsw i32 %28, 2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %29)
%31 = load i32, i32* %8
%32 = mul nsw i32 %31, 2
%33 = load i32, i32* %5
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %39
37: ; preds = %2
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %35
store i32 0, i32* %10
br label %40
40: ; preds = %64, %39
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %67
44: ; preds = %40
%45 = load i32*, i32** %3
%46 = load i32, i32* %10
%47 = mul nsw i32 %46, 2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %45, i64 %48
%50 = getelementptr inbounds i32, i32* %49, i64 7
%51 = call i32 @memcpy(i32* %9, i32* %50, i32 2)
%52 = call i32 @swap16(i32* %9)
%53 = load i32, i32* %10
%54 = load i32, i32* %9
%55 = load i32, i32* %7
%56 = load i32, i32* %10
%57 = add nsw i32 %55, %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %54, i32 %57)
%59 = load i32, i32* %7
%60 = load i32, i32* %10
%61 = add nsw i32 %59, %60
%62 = load i32, i32* %9
%63 = call i32 @write_register(i32 %61, i32 %62)
br label %64
64: ; preds = %44
%65 = load i32, i32* %10
%66 = add nsw i32 %65, 1
store i32 %66, i32* %10
br label %40
67: ; preds = %40
%68 = load i32*, i32** %3
%69 = call i32 @add_CRC(i32* %68, i32 8, i32 65535)
ret i32 8
}

LLVM IR (-O2):
@.str = [24 x i8] c"start = %hu, num = %hu\0A\00"
@.str.1 = [40 x i8] c"data bytes = %hu, calculated size = %d\0A\00"
@.str.4 = [27 x i8] c"value %d: %04hx, addr: %d\0A\00"
@str = [35 x i8] c"*** Data error size does NOT match\00"
@str.5 = [13 x i8] c"Size matches\00"
define i32 @rt_modbus_preset_multiple_registers(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds i32, i32* %0, i64 6
%10 = load i32, i32* %9
%11 = getelementptr inbounds i32, i32* %0, i64 2
%12 = call i32 @memcpy(i32* %3, i32* %11, i32 2)
%13 = call i32 @swap16(i32* %3)
%14 = getelementptr inbounds i32, i32* %0, i64 4
%15 = call i32 @memcpy(i32* %4, i32* %14, i32 2)
%16 = call i32 @swap16(i32* %4)
%17 = load i32, i32* %3
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i32, i32* %4
%21 = shl nsw i32 %20, 1
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %21)
%23 = load i32, i32* %4
%24 = shl nsw i32 %23, 1
%25 = icmp eq i32 %24, %10
%26 = select i1 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0)
%27 = call i32 @puts(i8* dereferenceable(1) %26)
%28 = load i32, i32* %4
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %51
30: ; preds = %2
%31 = getelementptr inbounds i32, i32* %0, i64 7
br label %32
32: ; preds = %30, %32
%33 = phi i64 [ 0, %30 ], [ %47, %32 ]
%34 = shl nuw nsw i64 %33, 1
%35 = getelementptr inbounds i32, i32* %31, i64 %34
%36 = call i32 @memcpy(i32* %5, i32* %35, i32 2)
%37 = call i32 @swap16(i32* %5)
%38 = load i32, i32* %5
%39 = load i32, i32* %3
%40 = trunc i64 %33 to i32
%41 = add nsw i32 %39, %40
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %38, i32 %41)
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %40
%45 = load i32, i32* %5
%46 = call i32 @write_register(i32 %44, i32 %45)
%47 = add nuw nsw i64 %33, 1
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = icmp slt i64 %47, %49
br i1 %50, label %32, label %51
51: ; preds = %32, %2
%52 = call i32 @add_CRC(i32* %0, i32 8, i32 65535)
ret i32 8
}