#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 read_di(int);
int /*<<< orphan*/ swap16(int *);
int rt_modbus_read_input_table(uint8_t *buf, int n) {
  uint16_t start_point;
  uint16_t num_points;
  int byte_count;
  uint8_t *p;
  int i;
  uint8_t val;
  int byte, bit;
  uint8_t mask;
  printf("In Read Input Table, n = %d\n", n);
  memcpy(&start_point, buf + 2, 2);
  swap16(&start_point);
  memcpy(&num_points, buf + 4, 2);
  swap16(&num_points);
  if (num_points > 128) {
    printf("*** Too many points specified!\n");
    printf("Start %u, Num %u\n", start_point, num_points);
    buf[1] = 128 + (0x02);
    buf[2] = 4;
    add_CRC(buf, 5, 0xffff);
    return 5;
  }
  byte_count = num_points / 8;
  if ((num_points % 8) > 0) {
    byte_count++;
  }
  buf[2] = byte_count;
  printf("In Read Input Table, byte count = %d, num_points = %d\n", byte_count,
         num_points);
  p = buf + 3;
  for (i = 0; i < byte_count; i++) {
    *(p++) = 0x00;
  }
  for (i = 0; i < num_points; i++) {
    val = read_di(i + start_point);
    if (val) {
      byte = i / 8;
      bit = i % 8;
      mask = 1 << bit;
      buf[byte + 3] |= mask;
    }
  }
  add_CRC(buf, 5 + byte_count, 0xffff);
  return (5 + byte_count);
}


x86 assembly (clang, -O0):
<rt_modbus_read_input_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
lea    -0x18(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
lea    -0x18(%rbp),%rdi
call   1070 <swap16@plt>
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
lea    -0x1c(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <swap16@plt>
cmpl   $0x80,-0x1c(%rbp)
jle    1212 <rt_modbus_read_input_table+0xc2>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0x82,0x4(%rax)
mov    -0x10(%rbp),%rax
movl   $0x4,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x5,%esi
mov    $0xffff,%edx
call   1060 <add_CRC@plt>
movl   $0x5,-0x4(%rbp)
jmp    1337 <rt_modbus_read_input_table+0x1e7>
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jle    123d <rt_modbus_read_input_table+0xed>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    129d <rt_modbus_read_input_table+0x14d>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
movl   $0x0,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    126e <rt_modbus_read_input_table+0x11e>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    131a <rt_modbus_read_input_table+0x1ca>
mov    -0x2c(%rbp),%edi
add    -0x18(%rbp),%edi
call   1040 <read_di@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1307 <rt_modbus_read_input_table+0x1b7>
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
or     (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
jmp    130c <rt_modbus_read_input_table+0x1bc>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12a4 <rt_modbus_read_input_table+0x154>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x5,%esi
mov    $0xffff,%edx
call   1060 <add_CRC@plt>
mov    -0x20(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rt_modbus_read_input_table>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x8(%rbx),%r15
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1090 <swap16@plt>
lea    0x10(%rbx),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1090 <swap16@plt>
mov    (%rsp),%edx
cmp    $0x81,%edx
jl     120a <rt_modbus_read_input_table+0x9a>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
lea    0xe33(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0x400000082,%rax
mov    %rax,0x4(%rbx)
mov    $0x5,%r14d
jmp    1292 <rt_modbus_read_input_table+0x122>
lea    0x7(%rdx),%r14d
test   %edx,%edx
cmovns %edx,%r14d
sar    $0x3,%r14d
xor    %ebp,%ebp
test   $0x80000007,%edx
setg   %bpl
lea    (%r14,%rbp,1),%esi
mov    %esi,(%r15)
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    %ebp,%r14d
jle    1255 <rt_modbus_read_input_table+0xe5>
lea    0xc(%rbx),%rdi
lea    -0x1(%r14),%eax
lea    0x4(,%rax,4),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
cmpl   $0x0,(%rsp)
jle    128e <rt_modbus_read_input_table+0x11e>
xor    %ebp,%ebp
jmp    1268 <rt_modbus_read_input_table+0xf8>
nop
add    $0x1,%ebp
cmp    (%rsp),%ebp
jge    128e <rt_modbus_read_input_table+0x11e>
mov    0x4(%rsp),%edi
add    %ebp,%edi
call   1060 <read_di@plt>
test   %eax,%eax
je     1260 <rt_modbus_read_input_table+0xf0>
mov    %ebp,%eax
mov    %ebp,%ecx
and    $0x7,%cl
mov    $0x1,%edx
shl    %cl,%edx
shr    $0x3,%eax
or     %edx,0xc(%rbx,%rax,4)
jmp    1260 <rt_modbus_read_input_table+0xf0>
add    $0x5,%r14d
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0xffff,%edx
call   1080 <add_CRC@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"In Read Input Table, n = %d\0A\00"
@.str.1 = [32 x i8] c"*** Too many points specified!\0A\00"
@.str.2 = [18 x i8] c"Start %u, Num %u\0A\00"
@.str.3 = [55 x i8] c"In Read Input Table, byte count = %d, num_points = %d\0A\00"
define i32 @rt_modbus_read_input_table(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
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%15 = load i32, i32* %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %4
%18 = getelementptr inbounds i32, i32* %17, i64 2
%19 = call i32 @memcpy(i32* %6, i32* %18, i32 2)
%20 = call i32 @swap16(i32* %6)
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i64 4
%23 = call i32 @memcpy(i32* %7, i32* %22, i32 2)
%24 = call i32 @swap16(i32* %7)
%25 = load i32, i32* %7
%26 = icmp sgt i32 %25, 128
br i1 %26, label %27, label %38
27: ; preds = %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i32*, i32** %4
%33 = getelementptr inbounds i32, i32* %32, i64 1
store i32 130, i32* %33
%34 = load i32*, i32** %4
%35 = getelementptr inbounds i32, i32* %34, i64 2
store i32 4, i32* %35
%36 = load i32*, i32** %4
%37 = call i32 @add_CRC(i32* %36, i32 5, i32 65535)
store i32 5, i32* %3
br label %104
38: ; preds = %2
%39 = load i32, i32* %7
%40 = sdiv i32 %39, 8
store i32 %40, i32* %8
%41 = load i32, i32* %7
%42 = srem i32 %41, 8
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %47
47: ; preds = %44, %38
%48 = load i32, i32* %8
%49 = load i32*, i32** %4
%50 = getelementptr inbounds i32, i32* %49, i64 2
store i32 %48, i32* %50
%51 = load i32, i32* %8
%52 = load i32, i32* %7
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %52)
%54 = load i32*, i32** %4
%55 = getelementptr inbounds i32, i32* %54, i64 3
store i32* %55, i32** %9
store i32 0, i32* %10
br label %56
56: ; preds = %63, %47
%57 = load i32, i32* %10
%58 = load i32, i32* %8
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %66
60: ; preds = %56
%61 = load i32*, i32** %9
%62 = getelementptr inbounds i32, i32* %61, i32 1
store i32* %62, i32** %9
store i32 0, i32* %61
br label %63
63: ; preds = %60
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 1
store i32 %65, i32* %10
br label %56
66: ; preds = %56
store i32 0, i32* %10
br label %67
67: ; preds = %94, %66
%68 = load i32, i32* %10
%69 = load i32, i32* %7
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %97
71: ; preds = %67
%72 = load i32, i32* %10
%73 = load i32, i32* %6
%74 = add nsw i32 %72, %73
%75 = call i32 @read_di(i32 %74)
store i32 %75, i32* %11
%76 = load i32, i32* %11
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %93
78: ; preds = %71
%79 = load i32, i32* %10
%80 = sdiv i32 %79, 8
store i32 %80, i32* %12
%81 = load i32, i32* %10
%82 = srem i32 %81, 8
store i32 %82, i32* %13
%83 = load i32, i32* %13
%84 = shl i32 1, %83
store i32 %84, i32* %14
%85 = load i32, i32* %14
%86 = load i32*, i32** %4
%87 = load i32, i32* %12
%88 = add nsw i32 %87, 3
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i32, i32* %86, i64 %89
%91 = load i32, i32* %90
%92 = or i32 %91, %85
store i32 %92, i32* %90
br label %93
93: ; preds = %78, %71
br label %94
94: ; preds = %93
%95 = load i32, i32* %10
%96 = add nsw i32 %95, 1
store i32 %96, i32* %10
br label %67
97: ; preds = %67
%98 = load i32*, i32** %4
%99 = load i32, i32* %8
%100 = add nsw i32 5, %99
%101 = call i32 @add_CRC(i32* %98, i32 %100, i32 65535)
%102 = load i32, i32* %8
%103 = add nsw i32 5, %102
store i32 %103, i32* %3
br label %104
104: ; preds = %97, %27
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
@.str = [29 x i8] c"In Read Input Table, n = %d\0A\00"
@.str.2 = [18 x i8] c"Start %u, Num %u\0A\00"
@.str.3 = [55 x i8] c"In Read Input Table, byte count = %d, num_points = %d\0A\00"
@str = [31 x i8] c"*** Too many points specified!\00"
define i32 @rt_modbus_read_input_table(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %1)
%8 = getelementptr inbounds i32, i32* %0, i64 2
%9 = call i32 @memcpy(i32* %3, i32* %8, i32 2)
%10 = call i32 @swap16(i32* %3)
%11 = getelementptr inbounds i32, i32* %0, i64 4
%12 = call i32 @memcpy(i32* %4, i32* %11, i32 2)
%13 = call i32 @swap16(i32* %4)
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 128
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
%21 = getelementptr inbounds i32, i32* %0, i64 1
store i32 130, i32* %21
store i32 4, i32* %8
br label %61
22: ; preds = %2
%23 = sdiv i32 %14, 8
%24 = and i32 %14, -2147483641
%25 = icmp sgt i32 %24, 0
%26 = zext i1 %25 to i32
%27 = add nsw i32 %23, %26
store i32 %27, i32* %8
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %14)
%29 = icmp sgt i32 %27, 0
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = getelementptr i32, i32* %0, i64 3
%32 = bitcast i32* %31 to i8*
%33 = add nsw i32 %27, -1
%34 = zext i32 %33 to i64
%35 = shl nuw nsw i64 %34, 2
%36 = add nuw nsw i64 %35, 4
br label %37
37: ; preds = %30, %22
%38 = load i32, i32* %4
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %59
40: ; preds = %37, %55
%41 = phi i32 [ %56, %55 ], [ 0, %37 ]
%42 = load i32, i32* %3
%43 = add nsw i32 %42, %41
%44 = call i32 @read_di(i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %55, label %46
46: ; preds = %40
%47 = lshr i32 %41, 3
%48 = and i32 %41, 7
%49 = shl nuw nsw i32 1, %48
%50 = add nuw nsw i32 %47, 3
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %0, i64 %51
%53 = load i32, i32* %52
%54 = or i32 %53, %49
store i32 %54, i32* %52
br label %55
55: ; preds = %40, %46
%56 = add nuw nsw i32 %41, 1
%57 = load i32, i32* %4
%58 = icmp slt i32 %56, %57
br i1 %58, label %40, label %59
59: ; preds = %55, %37
%60 = add nsw i32 %27, 5
br label %61
61: ; preds = %59, %16
%62 = phi i32 [ %60, %59 ], [ 5, %16 ]
%63 = call i32 @add_CRC(i32* %0, i32 %62, i32 65535)
ret i32 %62
}