#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 */
int /*<<< orphan*/ memcpy(void *, char *, int);
void endian_write_double(void *ptr, double d, int write_le) {
  int i;
  char *optr = (char *)ptr;
  char *r = (char *)(void *)&d;
  if (1 == write_le) {
    memcpy(ptr, r, 8);
  } else {
    for (i = 0; i < 8; i++) {
      *optr++ = r[7 - i];
    }
  }
}


x86 assembly (clang, -O0):
<endian_write_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x1,%eax
cmp    -0x14(%rbp),%eax
jne    1159 <endian_write_double+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
jmp    11a0 <endian_write_double+0x90>
movl   $0x0,-0x18(%rbp)
cmpl   $0x8,-0x18(%rbp)
jge    119b <endian_write_double+0x8b>
mov    -0x28(%rbp),%rax
mov    $0x7,%ecx
sub    -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1160 <endian_write_double+0x50>
jmp    11a0 <endian_write_double+0x90>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<endian_write_double>:
push   %rax
movq   %xmm0,(%rsp)
cmp    $0x1,%esi
jne    112a <endian_write_double+0x1a>
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
pop    %rax
ret
movq   %xmm0,%rax
bswap  %rax
mov    %rax,(%rdi)
pop    %rax
ret

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

LLVM IR (-O2):
define void @endian_write_double(i8* %0, double %1, i32 %2) {
%4 = alloca double
store double %1, double* %4
%5 = icmp eq i32 %2, 1
%6 = bitcast double %1 to i64
br i1 %5, label %30, label %7
7: ; preds = %3
%8 = trunc i64 %6 to i8
%9 = lshr i64 %6, 8
%10 = trunc i64 %9 to i8
%11 = lshr i64 %6, 16
%12 = trunc i64 %11 to i8
%13 = lshr i64 %6, 24
%14 = trunc i64 %13 to i8
%15 = lshr i64 %6, 32
%16 = trunc i64 %15 to i8
%17 = lshr i64 %6, 40
%18 = trunc i64 %17 to i8
%19 = lshr i64 %6, 48
%20 = trunc i64 %19 to i8
%21 = lshr i64 %6, 56
%22 = trunc i64 %21 to i8
%23 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %22, i8* %0
%24 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %20, i8* %23
%25 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %18, i8* %24
%26 = getelementptr inbounds i8, i8* %0, i64 4
store i8 %16, i8* %25
%27 = getelementptr inbounds i8, i8* %0, i64 5
store i8 %14, i8* %26
%28 = getelementptr inbounds i8, i8* %0, i64 6
store i8 %12, i8* %27
%29 = getelementptr inbounds i8, i8* %0, i64 7
store i8 %10, i8* %28
store i8 %8, i8* %29
br label %33
30: ; preds = %3
%31 = bitcast double* %4 to i8*
%32 = call i32 @memcpy(i8* %0, i8* %31, i32 8)
br label %33
33: ; preds = %7, %30
ret void
}