#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 read_ai(int);
int /*<<< orphan*/ swap16(int *);
int rt_modbus_read_analog_inputs(uint8_t *buf, int sz) {
  uint16_t start_point;
  uint16_t num_points;
  int byte_count;
  uint8_t *p;
  uint16_t tmp;
  int i;
  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 + (0x04);
    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++) {
    tmp = read_ai(start_point + i);
    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_analog_inputs>:
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   1040 <memcpy@plt>
lea    -0x18(%rbp),%rdi
call   1060 <swap16@plt>
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
lea    -0x1c(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x1c(%rbp),%rdi
call   1060 <swap16@plt>
cmpl   $0x78,-0x1c(%rbp)
jle    11cc <rt_modbus_read_analog_inputs+0x8c>
mov    -0x10(%rbp),%rax
movl   $0x84,0x4(%rax)
mov    -0x10(%rbp),%rax
movl   $0x4,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x5,%esi
mov    $0xffff,%edx
call   1050 <add_CRC@plt>
movl   $0x5,-0x4(%rbp)
jmp    125e <rt_modbus_read_analog_inputs+0x11e>
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,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1241 <rt_modbus_read_analog_inputs+0x101>
mov    -0x18(%rbp),%edi
add    -0x30(%rbp),%edi
call   1030 <read_ai@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
call   1060 <swap16@plt>
mov    -0x28(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f2 <rt_modbus_read_analog_inputs+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x5,%esi
mov    $0xffff,%edx
call   1050 <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_analog_inputs>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rdi),%rbx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %rbp,%rdi
call   1060 <swap16@plt>
lea    0x10(%r14),%rsi
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %rbp,%rdi
call   1060 <swap16@plt>
mov    0x4(%rsp),%eax
cmp    $0x79,%eax
jl     11ad <rt_modbus_read_analog_inputs+0x6d>
movabs $0x400000084,%rax
mov    %rax,0x4(%r14)
mov    $0x5,%r15d
jmp    1208 <rt_modbus_read_analog_inputs+0xc8>
lea    (%rax,%rax,1),%r15d
mov    %r15d,(%rbx)
test   %eax,%eax
jle    1204 <rt_modbus_read_analog_inputs+0xc4>
lea    0xc(%r14),%rbx
xor    %ebp,%ebp
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
add    %ebp,%edi
call   1030 <read_ai@plt>
mov    %eax,0x8(%rsp)
mov    %r12,%rdi
call   1060 <swap16@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
add    $0x8,%rbx
add    $0x1,%ebp
cmp    0x4(%rsp),%ebp
jl     11d0 <rt_modbus_read_analog_inputs+0x90>
add    $0x5,%r15d
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0xffff,%edx
call   1050 <add_CRC@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rt_modbus_read_analog_inputs(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 132, 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 %61
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* %11
br label %37
37: ; preds = %51, %29
%38 = load i32, i32* %11
%39 = load i32, i32* %7
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %54
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* %11
%44 = add nsw i32 %42, %43
%45 = call i32 @read_ai(i32 %44)
store i32 %45, i32* %10
%46 = call i32 @swap16(i32* %10)
%47 = load i32*, i32** %9
%48 = call i32 @memcpy(i32* %47, i32* %10, i32 2)
%49 = load i32*, i32** %9
%50 = getelementptr inbounds i32, i32* %49, i64 2
store i32* %50, i32** %9
br label %51
51: ; preds = %41
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
br label %37
54: ; preds = %37
%55 = load i32*, i32** %4
%56 = load i32, i32* %8
%57 = add nsw i32 5, %56
%58 = call i32 @add_CRC(i32* %55, i32 %57, i32 65535)
%59 = load i32, i32* %8
%60 = add nsw i32 5, %59
store i32 %60, i32* %3
br label %61
61: ; preds = %54, %22
%62 = load i32, i32* %3
ret i32 %62
}

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