#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 */

/* Variables and functions */
int SPI_Read_Value(int, unsigned char);
int /*<<< orphan*/ sleep(int);
int SPI_Register_Handler(int farm_fd, unsigned char channel, int collect_time,
                         int response_time, void (*handler)(int)) {
  int value = 0;
  int resCount = 0;
  int average = 0;
  int divider = response_time / collect_time;
  while (1) {
    value = SPI_Read_Value(farm_fd, channel);
    if (resCount == response_time) {
      handler(average / divider);
      average = 0;
      resCount = 0;
    } else {
      resCount++;
      average += value;
    }
    sleep(collect_time);
  }
  return 0;
}


x86 assembly (clang, -O0):
<SPI_Register_Handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
movzbl -0x5(%rbp),%esi
call   1030 <SPI_Read_Value@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    119f <SPI_Register_Handler+0x7f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
cltd
idivl  -0x28(%rbp)
mov    %eax,%edi
mov    -0x30(%rbp),%rax
call   *%rax
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    11b1 <SPI_Register_Handler+0x91>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <sleep@plt>
jmp    115a <SPI_Register_Handler+0x3a>

x86 assembly (clang, -O2):
<SPI_Register_Handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %edi,%ebp
mov    %ecx,%eax
cltd
idiv   %r13d
mov    %eax,0xc(%rsp)
xor    %r12d,%r12d
movzbl %sil,%ebx
xor    %r14d,%r14d
jmp    116f <SPI_Register_Handler+0x4f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
add    %eax,%r14d
mov    %r13d,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <SPI_Read_Value@plt>
cmp    %r15d,%r12d
jne    1160 <SPI_Register_Handler+0x40>
mov    %r14d,%eax
cltd
idivl  0xc(%rsp)
mov    %eax,%edi
call   *0x10(%rsp)
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    1167 <SPI_Register_Handler+0x47>

LLVM IR (-O0):
define i32 @SPI_Register_Handler(i32 %0, i8 zeroext %1, i32 %2, i32 %3, void (i32)* %4) {
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca void (i32)*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i8 %1, i8* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store void (i32)* %4, void (i32)** %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%15 = load i32, i32* %9
%16 = load i32, i32* %8
%17 = sdiv i32 %15, %16
store i32 %17, i32* %14
br label %18
18: ; preds = %5, %36
%19 = load i32, i32* %6
%20 = load i8, i8* %7
%21 = call i32 @SPI_Read_Value(i32 %19, i8 zeroext %20)
store i32 %21, i32* %11
%22 = load i32, i32* %12
%23 = load i32, i32* %9
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load void (i32)*, void (i32)** %10
%27 = load i32, i32* %13
%28 = load i32, i32* %14
%29 = sdiv i32 %27, %28
store i32 0, i32* %13
store i32 0, i32* %12
br label %36
30: ; preds = %18
%31 = load i32, i32* %12
%32 = add nsw i32 %31, 1
store i32 %32, i32* %12
%33 = load i32, i32* %11
%34 = load i32, i32* %13
%35 = add nsw i32 %34, %33
store i32 %35, i32* %13
br label %36
36: ; preds = %30, %25
%37 = load i32, i32* %8
%38 = call i32 @sleep(i32 %37)
br label %18
}

LLVM IR (-O2):
define i32 @SPI_Register_Handler(i32 %0, i8 zeroext %1, i32 %2, i32 %3, void (i32)* nocapture %4) {
%6 = sdiv i32 %3, %2
br label %7
7: ; preds = %17, %5
%8 = phi i32 [ 0, %5 ], [ %18, %17 ]
%9 = phi i32 [ 0, %5 ], [ %19, %17 ]
%10 = tail call i32 @SPI_Read_Value(i32 %0, i8 zeroext %1)
%11 = icmp eq i32 %8, %3
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = sdiv i32 %9, %6
tail call void %4(i32 %13)
br label %17
14: ; preds = %7
%15 = add nsw i32 %8, 1
%16 = add nsw i32 %10, %9
br label %17
17: ; preds = %14, %12
%18 = phi i32 [ 0, %12 ], [ %15, %14 ]
%19 = phi i32 [ 0, %12 ], [ %16, %14 ]
%20 = tail call i32 @sleep(i32 %2)
br label %7
}