#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int RAND_MAX;
double atof(char *);
int /*<<< orphan*/ fwrite(double *, int, int, int /*<<< orphan*/ *);
int rand();
void generate_double(FILE *fp) {
  double d;
  int k = rand();
  if (k == RAND_MAX) {
    d = atof((rand() % 2) ? "inf" : "nan");
  } else {
    double f = ((double)k) / RAND_MAX;
    int i = rand() % 1024;
    d = i + f;
  }
  if (rand() % 2)
    d = -d;
  fwrite(&d, sizeof(double), 1, fp);
}


x86 assembly (clang, -O0):
<generate_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e98(%rip),%rcx
cmp    (%rcx),%eax
jne    118b <generate_double+0x5b>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
lea    0xe96(%rip),%rdi
lea    0xe8b(%rip),%rax
cmp    $0x0,%edx
cmovne %rax,%rdi
call   1030 <atof@plt>
movsd  %xmm0,-0x10(%rbp)
jmp    11c5 <generate_double+0x95>
cvtsi2sdl -0x14(%rbp),%xmm0
mov    0x2e51(%rip),%rax
cvtsi2sdl (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
cvtsi2sdl -0x24(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11fe <generate_double+0xce>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1040 <fwrite@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_double>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <rand@plt>
mov    0x2ea2(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ecx,%eax
jne    116e <generate_double+0x3e>
xor    %eax,%eax
call   1050 <rand@plt>
test   $0x1,%al
lea    0xeb8(%rip),%rax
lea    0xead(%rip),%rdi
cmove  %rax,%rdi
call   1030 <atof@plt>
jmp    11a7 <generate_double+0x77>
cvtsi2sd %eax,%xmm1
cvtsi2sd %ecx,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
xor    %eax,%eax
call   1050 <rand@plt>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  0x10(%rsp),%xmm0
movapd %xmm0,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1050 <rand@plt>
test   $0x1,%al
je     11d2 <generate_double+0xa2>
movapd 0x10(%rsp),%xmm0
xorpd  0xe34(%rip),%xmm0
movlpd %xmm0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fwrite@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
@.str = [4 x i8] c"inf\00"
@.str.1 = [4 x i8] c"nan\00"
define void @generate_double(i32* %0) {
%2 = alloca i32*
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
store i32* %0, i32** %2
%7 = call i32 (...) @rand()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = load i32, i32* @RAND_MAX
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = call i32 (...) @rand()
%13 = srem i32 %12, 2
%14 = icmp ne i32 %13, 0
%15 = zext i1 %14 to i64
%16 = select i1 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%17 = call double @atof(i8* %16)
store double %17, double* %3
br label %30
18: ; preds = %1
%19 = load i32, i32* %4
%20 = sitofp i32 %19 to double
%21 = load i32, i32* @RAND_MAX
%22 = sitofp i32 %21 to double
%23 = fdiv double %20, %22
store double %23, double* %5
%24 = call i32 (...) @rand()
%25 = srem i32 %24, 1024
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = sitofp i32 %26 to double
%28 = load double, double* %5
%29 = fadd double %27, %28
store double %29, double* %3
br label %30
30: ; preds = %18, %11
%31 = call i32 (...) @rand()
%32 = srem i32 %31, 2
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load double, double* %3
%36 = fneg double %35
store double %36, double* %3
br label %37
37: ; preds = %34, %30
%38 = load i32*, i32** %2
%39 = call i32 @fwrite(double* %3, i32 8, i32 1, i32* %38)
ret void
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
@.str = [4 x i8] c"inf\00"
@.str.1 = [4 x i8] c"nan\00"
define void @generate_double(i32* nocapture %0) {
%2 = alloca double
%3 = bitcast double* %2 to i8*
%4 = tail call i32 (...) @rand()
%5 = load i32, i32* @RAND_MAX
%6 = icmp eq i32 %4, %5
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = tail call i32 (...) @rand()
%9 = and i32 %8, 1
%10 = icmp eq i32 %9, 0
%11 = select i1 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0)
%12 = tail call double @atof(i8* %11)
br label %21
13: ; preds = %1
%14 = sitofp i32 %4 to double
%15 = sitofp i32 %5 to double
%16 = fdiv double %14, %15
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 1024
%19 = sitofp i32 %18 to double
%20 = fadd double %16, %19
br label %21
21: ; preds = %13, %7
%22 = phi double [ %20, %13 ], [ %12, %7 ]
store double %22, double* %2
%23 = tail call i32 (...) @rand()
%24 = and i32 %23, 1
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = fneg double %22
store double %27, double* %2
br label %28
28: ; preds = %26, %21
%29 = call i32 @fwrite(double* %2, i32 8, i32 1, i32* %0)
ret void
}