#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 rt_modbus_read_registers(uint8_t *buf, int n) {
  uint16_t start_point;
  uint16_t num_points;
  int byte_count;
  uint8_t *p;
  int i;
  uint16_t tmp;
  memcpy(&start_point, buf + 2, 2);
  swap16(&start_point);
  memcpy(&num_points, buf + 4, 2);
  swap16(&num_points);
  if (num_points > 120) {
    buf[1] = 128 + (0x03);
    buf[2] = 4;
    add_CRC(buf, 5, 0xffff);
    return 5;
  }
  byte_count = num_points * 2;
  buf[2] = byte_count;
  p = buf + 3;
  for (i = 0; i < num_points; i++) {
    if ((start_point + i) % 2) {
      tmp = 0xAAAA;
    } else {
      tmp = 0x5555;
    }
    swap16(&tmp);
    memcpy(p, &tmp, 2);
    p += 2;
  }
  add_CRC(buf, 5 + byte_count, 0xffff);
  return (5 + byte_count);
}


x86 assembly (clang, -O0):
<rt_modbus_read_registers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
lea    -0x18(%rbp),%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
lea    -0x18(%rbp),%rdi
call   1050 <swap16@plt>
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
lea    -0x1c(%rbp),%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
lea    -0x1c(%rbp),%rdi
call   1050 <swap16@plt>
cmpl   $0x78,-0x1c(%rbp)
jle    11bc <rt_modbus_read_registers+0x8c>
mov    -0x10(%rbp),%rax
movl   $0x83,0x4(%rax)
mov    -0x10(%rbp),%rax
movl   $0x4,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x5,%esi
mov    $0xffff,%edx
call   1040 <add_CRC@plt>
movl   $0x5,-0x4(%rbp)
jmp    126a <rt_modbus_read_registers+0x13a>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    124d <rt_modbus_read_registers+0x11d>
mov    -0x18(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1211 <rt_modbus_read_registers+0xe1>
movl   $0xaaaa,-0x30(%rbp)
jmp    1218 <rt_modbus_read_registers+0xe8>
movl   $0x5555,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <swap16@plt>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e2 <rt_modbus_read_registers+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x5,%esi
mov    $0xffff,%edx
call   1040 <add_CRC@plt>
mov    -0x20(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rt_modbus_read_registers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rdi),%rbx
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    %rbp,%rdi
call   1050 <swap16@plt>
lea    0x10(%r14),%rsi
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    %rbp,%rdi
call   1050 <swap16@plt>
mov    0xc(%rsp),%eax
cmp    $0x79,%eax
jl     119f <rt_modbus_read_registers+0x6f>
movabs $0x400000083,%rax
mov    %rax,0x4(%r14)
mov    $0x5,%r15d
jmp    11fe <rt_modbus_read_registers+0xce>
lea    (%rax,%rax,1),%r15d
mov    %r15d,(%rbx)
test   %eax,%eax
jle    11fa <rt_modbus_read_registers+0xca>
lea    0xc(%r14),%rbx
xor    %ebp,%ebp
mov    $0x5555,%r13d
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    0x14(%rsp),%eax
add    %ebp,%eax
test   $0x1,%al
mov    $0xaaaa,%eax
cmove  %r13d,%eax
mov    %eax,0x10(%rsp)
mov    %r12,%rdi
call   1050 <swap16@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
call   1030 <memcpy@plt>
add    $0x8,%rbx
add    $0x1,%ebp
cmp    0xc(%rsp),%ebp
jl     11c0 <rt_modbus_read_registers+0x90>
add    $0x5,%r15d
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0xffff,%edx
call   1040 <add_CRC@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rt_modbus_read_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
%11 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%12 = load i32*, i32** %4
%13 = getelementptr inbounds i32, i32* %12, i64 2
%14 = call i32 @memcpy(i32* %6, i32* %13, i32 2)
%15 = call i32 @swap16(i32* %6)
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i64 4
%18 = call i32 @memcpy(i32* %7, i32* %17, i32 2)
%19 = call i32 @swap16(i32* %7)
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 120
br i1 %21, label %22, label %29
22: ; preds = %2
%23 = load i32*, i32** %4
%24 = getelementptr inbounds i32, i32* %23, i64 1
store i32 131, i32* %24
%25 = load i32*, i32** %4
%26 = getelementptr inbounds i32, i32* %25, i64 2
store i32 4, i32* %26
%27 = load i32*, i32** %4
%28 = call i32 @add_CRC(i32* %27, i32 5, i32 65535)
store i32 5, i32* %3
br label %65
29: ; preds = %2
%30 = load i32, i32* %7
%31 = mul nsw i32 %30, 2
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = load i32*, i32** %4
%34 = getelementptr inbounds i32, i32* %33, i64 2
store i32 %32, i32* %34
%35 = load i32*, i32** %4
%36 = getelementptr inbounds i32, i32* %35, i64 3
store i32* %36, i32** %9
store i32 0, i32* %10
br label %37
37: ; preds = %55, %29
%38 = load i32, i32* %10
%39 = load i32, i32* %7
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* %10
%44 = add nsw i32 %42, %43
%45 = srem i32 %44, 2
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 43690, i32* %11
br label %49
48: ; preds = %41
store i32 21845, i32* %11
br label %49
49: ; preds = %48, %47
%50 = call i32 @swap16(i32* %11)
%51 = load i32*, i32** %9
%52 = call i32 @memcpy(i32* %51, i32* %11, i32 2)
%53 = load i32*, i32** %9
%54 = getelementptr inbounds i32, i32* %53, i64 2
store i32* %54, i32** %9
br label %55
55: ; preds = %49
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %37
58: ; preds = %37
%59 = load i32*, i32** %4
%60 = load i32, i32* %8
%61 = add nsw i32 5, %60
%62 = call i32 @add_CRC(i32* %59, i32 %61, i32 65535)
%63 = load i32, i32* %8
%64 = add nsw i32 5, %63
store i32 %64, i32* %3
br label %65
65: ; preds = %58, %22
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
define i32 @rt_modbus_read_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, 120
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = getelementptr inbounds i32, i32* %0, i64 1
store i32 131, i32* %18
store i32 4, i32* %9
br label %40
19: ; preds = %2
%20 = shl nsw i32 %15, 1
store i32 %20, i32* %9
%21 = icmp sgt i32 %15, 0
br i1 %21, label %22, label %38
22: ; preds = %19
%23 = getelementptr inbounds i32, i32* %0, i64 3
br label %24
24: ; preds = %22, %24
%25 = phi i32 [ %35, %24 ], [ 0, %22 ]
%26 = phi i32* [ %34, %24 ], [ %23, %22 ]
%27 = load i32, i32* %3
%28 = add nsw i32 %27, %25
%29 = and i32 %28, 1
%30 = icmp eq i32 %29, 0
%31 = select i1 %30, i32 21845, i32 43690
store i32 %31, i32* %5
%32 = call i32 @swap16(i32* %5)
%33 = call i32 @memcpy(i32* %26, i32* %5, i32 2)
%34 = getelementptr inbounds i32, i32* %26, i64 2
%35 = add nuw nsw i32 %25, 1
%36 = load i32, i32* %4
%37 = icmp slt i32 %35, %36
br i1 %37, label %24, label %38
38: ; preds = %24, %19
%39 = add nsw i32 %20, 5
br label %40
40: ; preds = %38, %17
%41 = phi i32 [ %39, %38 ], [ 5, %17 ]
%42 = call i32 @add_CRC(i32* %0, i32 %41, i32 65535)
ret i32 %41
}