#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 fp_fault_injection(double *in, unsigned int len, double *p_e) {
  unsigned int i, b;
  double r;
  for (i = 0; i < len; i++) {
    for (b = 0; b < 64; 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):
<fp_fault_injection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11c1 <fp_fault_injection+0xb1>
movl   $0x0,-0x20(%rbp)
cmpl   $0x40,-0x20(%rbp)
jae    11ae <fp_fault_injection+0x9e>
mov    $0x0,%al
call   1030 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e8e(%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    117f <fp_fault_injection+0x6f>
jmp    119b <fp_fault_injection+0x8b>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
xor    (%rax,%rcx,8),%rdx
mov    %rdx,(%rax,%rcx,8)
jmp    11a0 <fp_fault_injection+0x90>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    113d <fp_fault_injection+0x2d>
jmp    11b3 <fp_fault_injection+0xa3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <fp_fault_injection+0x1a>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fp_fault_injection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     1187 <fp_fault_injection+0x77>
mov    %rdx,%r15
mov    %rdi,%r14
mov    %esi,%r12d
mov    0x2eb9(%rip),%r13
xor    %ebp,%ebp
jmp    1149 <fp_fault_injection+0x39>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbp
cmp    %r12,%rbp
je     1187 <fp_fault_injection+0x77>
xor    %ebx,%ebx
jmp    115a <fp_fault_injection+0x4a>
nopl   (%rax)
add    $0x1,%rbx
cmp    $0x40,%rbx
je     1140 <fp_fault_injection+0x30>
xor    %eax,%eax
call   1030 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  0x0(%r13),%xmm0
ucomisd (%r15,%rbx,8),%xmm0
ja     1150 <fp_fault_injection+0x40>
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%rax
xor    %rax,(%r14,%rbp,8)
jmp    1150 <fp_fault_injection+0x40>
mov    $0x1,%eax
add    $0x8,%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 @fp_fault_injection(double* %0, i32 %1, double* %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
store double* %0, double** %4
store i32 %1, i32* %5
store double* %2, double** %6
store i32 0, i32* %7
br label %10
10: ; preds = %47, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %50
14: ; preds = %10
store i32 0, i32* %8
br label %15
15: ; preds = %43, %14
%16 = load i32, i32* %8
%17 = icmp ult i32 %16, 64
br i1 %17, label %18, label %46
18: ; preds = %15
%19 = call i64 (...) @rand()
%20 = sitofp i64 %19 to double
%21 = load double, double* @RAND_MAX
%22 = fdiv double %20, %21
store double %22, double* %9
%23 = load double, double* %9
%24 = load double*, double** %6
%25 = load i32, i32* %8
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds double, double* %24, i64 %26
%28 = load double, double* %27
%29 = fcmp ogt double %23, %28
br i1 %29, label %30, label %31
30: ; preds = %18
br label %42
31: ; preds = %18
%32 = load i32, i32* %8
%33 = zext i32 %32 to i64
%34 = shl i64 1, %33
%35 = load double*, double** %4
%36 = load i32, i32* %7
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds double, double* %35, i64 %37
%39 = bitcast double* %38 to i64*
%40 = load i64, i64* %39
%41 = xor i64 %40, %34
store i64 %41, i64* %39
br label %42
42: ; preds = %31, %30
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add i32 %44, 1
store i32 %45, i32* %8
br label %15
46: ; preds = %15
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = add i32 %48, 1
store i32 %49, i32* %7
br label %10
50: ; preds = %10
ret i32 1
}

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