#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*/ 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);
  for (i = 0; i < num_points; i++) {
    memcpy(&tmp, buf + (i * 2) + 7, 2);
    swap16(&tmp);
    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   1030 <memcpy@plt>
lea    -0x18(%rbp),%rdi
call   1060 <swap16@plt>
mov    -0x8(%rbp),%rsi
add    $0x10,%rsi
lea    -0x1c(%rbp),%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
lea    -0x1c(%rbp),%rdi
call   1060 <swap16@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11fd <rt_modbus_preset_multiple_registers+0xbd>
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   1030 <memcpy@plt>
lea    -0x20(%rbp),%rdi
call   1060 <swap16@plt>
mov    -0x18(%rbp),%edi
add    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1050 <write_register@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a7 <rt_modbus_preset_multiple_registers+0x67>
mov    -0x8(%rbp),%rdi
mov    $0x8,%esi
mov    $0xffff,%edx
call   1040 <add_CRC@plt>
mov    $0x8,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rt_modbus_preset_multiple_registers>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rdi),%rsi
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <swap16@plt>
lea    0x10(%r14),%rsi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <swap16@plt>
cmpl   $0x0,0xc(%rsp)
jle    11d9 <rt_modbus_preset_multiple_registers+0x99>
lea    0x1c(%r14),%r12
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    %r15,%rdi
call   1060 <swap16@plt>
mov    0x14(%rsp),%edi
add    %ebx,%edi
mov    0x10(%rsp),%esi
call   1050 <write_register@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
add    $0x8,%r12
cmp    %rax,%rbx
jl     11a0 <rt_modbus_preset_multiple_registers+0x60>
mov    %r14,%rdi
mov    $0x8,%esi
mov    $0xffff,%edx
call   1040 <add_CRC@plt>
mov    $0x8,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
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)
store i32 0, i32* %10
br label %24
24: ; preds = %42, %2
%25 = load i32, i32* %10
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %45
28: ; preds = %24
%29 = load i32*, i32** %3
%30 = load i32, i32* %10
%31 = mul nsw i32 %30, 2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %29, i64 %32
%34 = getelementptr inbounds i32, i32* %33, i64 7
%35 = call i32 @memcpy(i32* %9, i32* %34, i32 2)
%36 = call i32 @swap16(i32* %9)
%37 = load i32, i32* %7
%38 = load i32, i32* %10
%39 = add nsw i32 %37, %38
%40 = load i32, i32* %9
%41 = call i32 @write_register(i32 %39, i32 %40)
br label %42
42: ; preds = %28
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 1
store i32 %44, i32* %10
br label %24
45: ; preds = %24
%46 = load i32*, i32** %3
%47 = call i32 @add_CRC(i32* %46, i32 8, i32 65535)
ret i32 8
}

LLVM IR (-O2):
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 2
%10 = call i32 @memcpy(i32* %3, i32* %9, i32 2)
%11 = call i32 @swap16(i32* %3)
%12 = getelementptr inbounds i32, i32* %0, i64 4
%13 = call i32 @memcpy(i32* %4, i32* %12, i32 2)
%14 = call i32 @swap16(i32* %4)
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %2
%18 = getelementptr inbounds i32, i32* %0, i64 7
br label %19
19: ; preds = %17, %19
%20 = phi i64 [ 0, %17 ], [ %30, %19 ]
%21 = shl nuw nsw i64 %20, 1
%22 = getelementptr inbounds i32, i32* %18, i64 %21
%23 = call i32 @memcpy(i32* %5, i32* %22, i32 2)
%24 = call i32 @swap16(i32* %5)
%25 = load i32, i32* %3
%26 = trunc i64 %20 to i32
%27 = add nsw i32 %25, %26
%28 = load i32, i32* %5
%29 = call i32 @write_register(i32 %27, i32 %28)
%30 = add nuw nsw i64 %20, 1
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = icmp slt i64 %30, %32
br i1 %33, label %19, label %34
34: ; preds = %19, %2
%35 = call i32 @add_CRC(i32* %0, i32 8, i32 65535)
ret i32 8
}