#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 RAND_MAX;
scalar_t__ rand();
int binary_fault_injection(int *in, unsigned int len, unsigned int bits,
                           double *p_e) {
  unsigned int i, b;
  double r;
  for (i = 0; i < len; i++) {
    for (b = 0; b < bits; b++) {
      r = ((double)rand()) / RAND_MAX;
      if (r > p_e[b]) {
      } else {
        *(unsigned long *)&in[i] ^= ((unsigned long)1 << b);
      }
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<binary_fault_injection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11c6 <binary_fault_injection+0xb6>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    11b3 <binary_fault_injection+0xa3>
mov    $0x0,%al
call   1030 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e89(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
ucomisd (%rax,%rcx,8),%xmm0
jbe    1184 <binary_fault_injection+0x74>
jmp    11a0 <binary_fault_injection+0x90>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
xor    (%rax,%rcx,4),%rdx
mov    %rdx,(%rax,%rcx,4)
jmp    11a5 <binary_fault_injection+0x95>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1140 <binary_fault_injection+0x30>
jmp    11b8 <binary_fault_injection+0xa8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112d <binary_fault_injection+0x1d>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_fault_injection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0xc(%rsp)
test   %esi,%esi
je     11a6 <binary_fault_injection+0x96>
mov    %rcx,%r13
mov    %rdi,%r15
mov    %esi,%eax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%ebx
mov    0x2ea6(%rip),%r14
xor    %r12d,%r12d
jmp    115b <binary_fault_injection+0x4b>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    0x10(%rsp),%r12
je     11a6 <binary_fault_injection+0x96>
cmpl   $0x0,0xc(%rsp)
je     1150 <binary_fault_injection+0x40>
xor    %ebp,%ebp
jmp    1179 <binary_fault_injection+0x69>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
je     1150 <binary_fault_injection+0x40>
xor    %eax,%eax
call   1030 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
ucomisd 0x0(%r13,%rbp,8),%xmm0
ja     1170 <binary_fault_injection+0x60>
mov    $0x1,%eax
mov    %ebp,%ecx
shl    %cl,%rax
xor    %rax,(%r15,%r12,4)
jmp    1170 <binary_fault_injection+0x60>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
define i32 @binary_fault_injection(i32* %0, i32 %1, i32 %2, double* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store double* %3, double** %8
store i32 0, i32* %9
br label %12
12: ; preds = %50, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %6
%15 = icmp ult i32 %13, %14
br i1 %15, label %16, label %53
16: ; preds = %12
store i32 0, i32* %10
br label %17
17: ; preds = %46, %16
%18 = load i32, i32* %10
%19 = load i32, i32* %7
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %49
21: ; preds = %17
%22 = call i64 (...) @rand()
%23 = sitofp i64 %22 to double
%24 = load double, double* @RAND_MAX
%25 = fdiv double %23, %24
store double %25, double* %11
%26 = load double, double* %11
%27 = load double*, double** %8
%28 = load i32, i32* %10
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
%32 = fcmp ogt double %26, %31
br i1 %32, label %33, label %34
33: ; preds = %21
br label %45
34: ; preds = %21
%35 = load i32, i32* %10
%36 = zext i32 %35 to i64
%37 = shl i64 1, %36
%38 = load i32*, i32** %5
%39 = load i32, i32* %9
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = bitcast i32* %41 to i64*
%43 = load i64, i64* %42
%44 = xor i64 %43, %37
store i64 %44, i64* %42
br label %45
45: ; preds = %34, %33
br label %46
46: ; preds = %45
%47 = load i32, i32* %10
%48 = add i32 %47, 1
store i32 %48, i32* %10
br label %17
49: ; preds = %17
br label %50
50: ; preds = %49
%51 = load i32, i32* %9
%52 = add i32 %51, 1
store i32 %52, i32* %9
br label %12
53: ; preds = %12
ret i32 1
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
define i32 @binary_fault_injection(i32* nocapture %0, i32 %1, i32 %2, double* nocapture %3) {
%5 = icmp eq i32 %1, 0
br i1 %5, label %34, label %6
6: ; preds = %4
%7 = icmp eq i32 %2, 0
%8 = zext i32 %1 to i64
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %6, %31
%11 = phi i64 [ 0, %6 ], [ %32, %31 ]
br i1 %7, label %31, label %12
12: ; preds = %10
%13 = getelementptr inbounds i32, i32* %0, i64 %11
%14 = bitcast i32* %13 to i64*
br label %15
15: ; preds = %12, %28
%16 = phi i64 [ 0, %12 ], [ %29, %28 ]
%17 = tail call i64 (...) @rand()
%18 = sitofp i64 %17 to double
%19 = load double, double* @RAND_MAX
%20 = fdiv double %18, %19
%21 = getelementptr inbounds double, double* %3, i64 %16
%22 = load double, double* %21
%23 = fcmp ogt double %20, %22
br i1 %23, label %28, label %24
24: ; preds = %15
%25 = shl nuw i64 1, %16
%26 = load i64, i64* %14
%27 = xor i64 %26, %25
store i64 %27, i64* %14
br label %28
28: ; preds = %24, %15
%29 = add nuw nsw i64 %16, 1
%30 = icmp eq i64 %29, %9
br i1 %30, label %31, label %15
31: ; preds = %28, %10
%32 = add nuw nsw i64 %11, 1
%33 = icmp eq i64 %32, %8
br i1 %33, label %34, label %10
34: ; preds = %31, %4
ret i32 1
}