#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 */
scalar_t__ bernoulli(double);
void binary_symmetric_channel(char *index, double error_prob,
                              int fixed_length) {
  int i;
  for (i = 0; i < fixed_length; i++) {
    if (bernoulli(error_prob)) {
      *index ^= (1 << (i % 8));
    }
    if (i % 8 == 7) {
      index++;
    }
  }
}


x86 assembly (clang, -O0):
<binary_symmetric_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119d <binary_symmetric_channel+0x8d>
movsd  -0x10(%rbp),%xmm0
call   1030 <bernoulli@plt>
cmp    $0x0,%rax
je     116a <binary_symmetric_channel+0x5a>
mov    -0x18(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %edx,%ecx
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x7,%edx
jne    118a <binary_symmetric_channel+0x7a>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    118f <binary_symmetric_channel+0x7f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112b <binary_symmetric_channel+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_symmetric_channel>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movsd  %xmm0,0x8(%rsp)
test   %esi,%esi
jle    1163 <binary_symmetric_channel+0x53>
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %ebp,%ebp
jmp    1143 <binary_symmetric_channel+0x33>
nopl   0x0(%rax)
xor    %eax,%eax
cmp    $0x7,%ecx
sete   %al
add    %rax,%rbx
add    $0x1,%ebp
cmp    %ebp,%r14d
je     1163 <binary_symmetric_channel+0x53>
movsd  0x8(%rsp),%xmm0
call   1030 <bernoulli@plt>
mov    %ebp,%ecx
and    $0x7,%ecx
test   %rax,%rax
je     1130 <binary_symmetric_channel+0x20>
mov    $0x1,%eax
shl    %cl,%eax
xor    %al,(%rbx)
jmp    1130 <binary_symmetric_channel+0x20>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @binary_symmetric_channel(i8* %0, double %1, i32 %2) {
%4 = alloca i8*
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store double %1, double* %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %33, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %36
12: ; preds = %8
%13 = load double, double* %5
%14 = call i64 @bernoulli(double %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = load i32, i32* %7
%18 = srem i32 %17, 8
%19 = shl i32 1, %18
%20 = load i8*, i8** %4
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = xor i32 %22, %19
%24 = trunc i32 %23 to i8
store i8 %24, i8* %20
br label %25
25: ; preds = %16, %12
%26 = load i32, i32* %7
%27 = srem i32 %26, 8
%28 = icmp eq i32 %27, 7
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
br label %32
32: ; preds = %29, %25
br label %33
33: ; preds = %32
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %8
36: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @binary_symmetric_channel(i8* nocapture %0, double %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %22
5: ; preds = %3, %16
%6 = phi i32 [ %20, %16 ], [ 0, %3 ]
%7 = phi i8* [ %19, %16 ], [ %0, %3 ]
%8 = tail call i64 @bernoulli(double %1)
%9 = icmp eq i64 %8, 0
%10 = and i32 %6, 7
br i1 %9, label %16, label %11
11: ; preds = %5
%12 = shl nuw nsw i32 1, %10
%13 = load i8, i8* %7
%14 = trunc i32 %12 to i8
%15 = xor i8 %13, %14
store i8 %15, i8* %7
br label %16
16: ; preds = %5, %11
%17 = icmp eq i32 %10, 7
%18 = zext i1 %17 to i64
%19 = getelementptr i8, i8* %7, i64 %18
%20 = add nuw nsw i32 %6, 1
%21 = icmp eq i32 %20, %2
br i1 %21, label %22, label %5
22: ; preds = %16, %3
ret void
}