#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 */
double onePeriod(int, double);
double encodeByte(unsigned char data, double inRemainder) {
  double remainder = inRemainder;
  for (int j = 0; j < 8; j++) {
    if ((data & 0x00000080) == 0x00000080) {
      remainder = onePeriod(1000, remainder);
    } else {
      remainder = onePeriod(2000, remainder);
    }
    data = (unsigned char)((data << 1) & 0x000000FFL);
  }
  return remainder;
}


x86 assembly (clang, -O0):
<encodeByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    119f <encodeByte+0x8f>
movzbl -0x1(%rbp),%eax
and    $0x80,%eax
cmp    $0x80,%eax
jne    116b <encodeByte+0x5b>
movsd  -0x18(%rbp),%xmm0
mov    $0x3e8,%edi
call   1030 <onePeriod@plt>
movsd  %xmm0,-0x18(%rbp)
jmp    117f <encodeByte+0x6f>
movsd  -0x18(%rbp),%xmm0
mov    $0x7d0,%edi
call   1030 <onePeriod@plt>
movsd  %xmm0,-0x18(%rbp)
movzbl -0x1(%rbp),%eax
shl    $0x1,%eax
cltq
and    $0xff,%rax
mov    %al,-0x1(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1134 <encodeByte+0x24>
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encodeByte>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
test   %bl,%bl
mov    $0x7d0,%ebp
mov    $0x3e8,%r14d
mov    $0x3e8,%edi
cmovns %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x40,%bl
mov    $0x3e8,%edi
cmove  %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x20,%bl
mov    $0x3e8,%edi
cmove  %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x10,%bl
mov    $0x3e8,%edi
cmove  %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x8,%bl
mov    $0x3e8,%edi
cmove  %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x4,%bl
mov    $0x3e8,%edi
cmove  %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x2,%bl
mov    $0x3e8,%edi
cmove  %ebp,%edi
call   1030 <onePeriod@plt>
test   $0x1,%bl
cmove  %ebp,%r14d
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <onePeriod@plt>

LLVM IR (-O0):
define double @encodeByte(i8 zeroext %0, double %1) {
%3 = alloca i8
%4 = alloca double
%5 = alloca double
%6 = alloca i32
store i8 %0, i8* %3
store double %1, double* %4
%7 = load double, double* %4
store double %7, double* %5
store i32 0, i32* %6
br label %8
8: ; preds = %29, %2
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 8
br i1 %10, label %11, label %32
11: ; preds = %8
%12 = load i8, i8* %3
%13 = zext i8 %12 to i32
%14 = and i32 %13, 128
%15 = icmp eq i32 %14, 128
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load double, double* %5
%18 = call double @onePeriod(i32 1000, double %17)
store double %18, double* %5
br label %22
19: ; preds = %11
%20 = load double, double* %5
%21 = call double @onePeriod(i32 2000, double %20)
store double %21, double* %5
br label %22
22: ; preds = %19, %16
%23 = load i8, i8* %3
%24 = zext i8 %23 to i32
%25 = shl i32 %24, 1
%26 = sext i32 %25 to i64
%27 = and i64 %26, 255
%28 = trunc i64 %27 to i8
store i8 %28, i8* %3
br label %29
29: ; preds = %22
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %8
32: ; preds = %8
%33 = load double, double* %5
ret double %33
}

LLVM IR (-O2):
define double @encodeByte(i8 zeroext %0, double %1) {
%3 = icmp sgt i8 %0, -1
%4 = select i1 %3, i32 2000, i32 1000
%5 = tail call double @onePeriod(i32 %4, double %1)
%6 = and i8 %0, 64
%7 = icmp eq i8 %6, 0
%8 = select i1 %7, i32 2000, i32 1000
%9 = tail call double @onePeriod(i32 %8, double %5)
%10 = and i8 %0, 32
%11 = icmp eq i8 %10, 0
%12 = select i1 %11, i32 2000, i32 1000
%13 = tail call double @onePeriod(i32 %12, double %9)
%14 = and i8 %0, 16
%15 = icmp eq i8 %14, 0
%16 = select i1 %15, i32 2000, i32 1000
%17 = tail call double @onePeriod(i32 %16, double %13)
%18 = and i8 %0, 8
%19 = icmp eq i8 %18, 0
%20 = select i1 %19, i32 2000, i32 1000
%21 = tail call double @onePeriod(i32 %20, double %17)
%22 = and i8 %0, 4
%23 = icmp eq i8 %22, 0
%24 = select i1 %23, i32 2000, i32 1000
%25 = tail call double @onePeriod(i32 %24, double %21)
%26 = and i8 %0, 2
%27 = icmp eq i8 %26, 0
%28 = select i1 %27, i32 2000, i32 1000
%29 = tail call double @onePeriod(i32 %28, double %25)
%30 = and i8 %0, 1
%31 = icmp eq i8 %30, 0
%32 = select i1 %31, i32 2000, i32 1000
%33 = tail call double @onePeriod(i32 %32, double %29)
ret double %33
}