#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*/ send_do(int, int);
int /*<<< orphan*/ swap16(int *);
int rt_modbus_force_multiple_outputs(uint8_t *buf, int sz) {
  uint16_t start_point;
  uint16_t num_points;
  int data_bytes;
  int size;
  int i;
  int byte, bit;
  uint8_t mask;
  int sz_check;
  uint8_t val;
  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);
  sz_check = num_points / 8;
  if ((num_points % 8) != 0)
    sz_check++;
  printf("data bytes = %hu, calculated size = %d\n", data_bytes, sz_check);
  if (sz_check == data_bytes) {
    printf("Size matches\n");
  } else {
    printf("*** Data error size does NOT match\n");
  }
  for (i = 0; i < num_points; i++) {
    byte = i / 8;
    bit = i % 8;
    mask = 1 << bit;
    val = (buf[byte + 7] & mask) != 0;
    printf("byte: %d (buf[%d]), bit %d: %c, addr: %d\n", byte, byte + 7, bit,
           val ? 'T' : 'F', ((int)start_point) + i);
    send_do(start_point + i, val);
  }
  add_CRC(buf, 8, 0xffff);
  return 8;
}


x86 assembly (clang, -O0):
<rt_modbus_force_multiple_outputs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x18(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x9,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
add    $0x8,%rsi
lea    -0x10(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x10(%rbp),%rdi
call   1060 <swap16@plt>
mov    -0x8(%rbp),%rsi
add    $0x10,%rsi
lea    -0x14(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x14(%rbp),%rdi
call   1060 <swap16@plt>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11ef <rt_modbus_force_multiple_outputs+0x9f>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1222 <rt_modbus_force_multiple_outputs+0xd2>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1230 <rt_modbus_force_multiple_outputs+0xe0>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12e0 <rt_modbus_force_multiple_outputs+0x190>
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x7,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
and    -0x2c(%rbp),%eax
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x24(%rbp),%edx
add    $0x7,%edx
mov    -0x28(%rbp),%ecx
mov    -0x34(%rbp),%edi
mov    $0x46,%r8d
mov    $0x54,%eax
cmp    $0x0,%edi
cmovne %eax,%r8d
mov    -0x10(%rbp),%r9d
add    -0x20(%rbp),%r9d
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
add    -0x20(%rbp),%edi
mov    -0x34(%rbp),%esi
call   1070 <send_do@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1237 <rt_modbus_force_multiple_outputs+0xe7>
mov    -0x8(%rbp),%rdi
mov    $0x8,%esi
mov    $0xffff,%edx
call   1050 <add_CRC@plt>
mov    $0x8,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rt_modbus_force_multiple_outputs>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r12
mov    0x18(%rdi),%r14d
lea    0x8(%rdi),%rsi
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
call   1070 <swap16@plt>
lea    0x10(%r12),%rsi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
call   1070 <swap16@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%eax
lea    0x7(%rax),%ebp
test   %eax,%eax
cmovns %eax,%ebp
sar    $0x3,%ebp
and    $0x7,%eax
cmp    $0x1,%eax
sbb    $0xffffffff,%ebp
lea    0xe33(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %r14d,%ebp
lea    0xe92(%rip),%rax
lea    0xe68(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
cmpl   $0x0,0x8(%rsp)
jle    127d <rt_modbus_force_multiple_outputs+0x11d>
mov    $0x54,%r15d
lea    0xe21(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%esi
shr    $0x3,%esi
mov    %ebx,%ecx
and    $0x7,%ecx
lea    0x7(%rsi),%edx
mov    0x1c(%r12,%rsi,4),%eax
xor    %ebp,%ebp
bt     %ecx,%eax
setb   %bpl
mov    $0x46,%r8d
cmovb  %r15d,%r8d
mov    0xc(%rsp),%r9d
add    %ebx,%r9d
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
add    %ebx,%edi
mov    %ebp,%esi
call   1080 <send_do@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1230 <rt_modbus_force_multiple_outputs+0xd0>
mov    %r12,%rdi
mov    $0x8,%esi
mov    $0xffff,%edx
call   1060 <add_CRC@plt>
mov    $0x8,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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 = [42 x i8] c"byte: %d (buf[%d]), bit %d: %c, addr: %d\0A\00"
define i32 @rt_modbus_force_multiple_outputs(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** %3
store i32 %1, i32* %4
%15 = load i32*, i32** %3
%16 = getelementptr inbounds i32, i32* %15, i64 6
%17 = load i32, i32* %16
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = add nsw i32 9, %18
store i32 %19, i32* %8
%20 = load i32*, i32** %3
%21 = getelementptr inbounds i32, i32* %20, i64 2
%22 = call i32 @memcpy(i32* %5, i32* %21, i32 2)
%23 = call i32 @swap16(i32* %5)
%24 = load i32*, i32** %3
%25 = getelementptr inbounds i32, i32* %24, i64 4
%26 = call i32 @memcpy(i32* %6, i32* %25, i32 2)
%27 = call i32 @swap16(i32* %6)
%28 = load i32, i32* %5
%29 = load i32, i32* %6
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i32, i32* %6
%32 = sdiv i32 %31, 8
store i32 %32, i32* %13
%33 = load i32, i32* %6
%34 = srem i32 %33, 8
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %2
%37 = load i32, i32* %13
%38 = add nsw i32 %37, 1
store i32 %38, i32* %13
br label %39
39: ; preds = %36, %2
%40 = load i32, i32* %7
%41 = load i32, i32* %13
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %41)
%43 = load i32, i32* %13
%44 = load i32, i32* %7
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %50
48: ; preds = %39
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
store i32 0, i32* %9
br label %51
51: ; preds = %89, %50
%52 = load i32, i32* %9
%53 = load i32, i32* %6
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %92
55: ; preds = %51
%56 = load i32, i32* %9
%57 = sdiv i32 %56, 8
store i32 %57, i32* %10
%58 = load i32, i32* %9
%59 = srem i32 %58, 8
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = shl i32 1, %60
store i32 %61, i32* %12
%62 = load i32*, i32** %3
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %62, i64 %65
%67 = load i32, i32* %66
%68 = load i32, i32* %12
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
%71 = zext i1 %70 to i32
store i32 %71, i32* %14
%72 = load i32, i32* %10
%73 = load i32, i32* %10
%74 = add nsw i32 %73, 7
%75 = load i32, i32* %11
%76 = load i32, i32* %14
%77 = icmp ne i32 %76, 0
%78 = zext i1 %77 to i64
%79 = select i1 %77, i32 84, i32 70
%80 = load i32, i32* %5
%81 = load i32, i32* %9
%82 = add nsw i32 %80, %81
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %72, i32 %74, i32 %75, i32 %79, i32 %82)
%84 = load i32, i32* %5
%85 = load i32, i32* %9
%86 = add nsw i32 %84, %85
%87 = load i32, i32* %14
%88 = call i32 @send_do(i32 %86, i32 %87)
br label %89
89: ; preds = %55
%90 = load i32, i32* %9
%91 = add nsw i32 %90, 1
store i32 %91, i32* %9
br label %51
92: ; preds = %51
%93 = load i32*, i32** %3
%94 = call i32 @add_CRC(i32* %93, 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 = [42 x i8] c"byte: %d (buf[%d]), bit %d: %c, 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_force_multiple_outputs(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds i32, i32* %0, i64 6
%8 = load i32, i32* %7
%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* %3
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
%18 = load i32, i32* %4
%19 = sdiv i32 %18, 8
%20 = and i32 %18, 7
%21 = icmp ne i32 %20, 0
%22 = zext i1 %21 to i32
%23 = add nsw i32 %19, %22
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %23)
%25 = icmp eq i32 %23, %8
%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 %52
30: ; preds = %2, %30
%31 = phi i32 [ %49, %30 ], [ 0, %2 ]
%32 = lshr i32 %31, 3
%33 = and i32 %31, 7
%34 = shl nuw nsw i32 1, %33
%35 = add nuw nsw i32 %32, 7
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %0, i64 %36
%38 = load i32, i32* %37
%39 = and i32 %38, %34
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i32
%42 = select i1 %40, i32 84, i32 70
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %31
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %35, i32 %33, i32 %42, i32 %44)
%46 = load i32, i32* %3
%47 = add nsw i32 %46, %31
%48 = call i32 @send_do(i32 %47, i32 %41)
%49 = add nuw nsw i32 %31, 1
%50 = load i32, i32* %4
%51 = icmp slt i32 %49, %50
br i1 %51, label %30, label %52
52: ; preds = %30, %2
%53 = call i32 @add_CRC(i32* %0, i32 8, i32 65535)
ret i32 8
}