#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(double *, void const *, int);
double endian_read_double(const void *ptr, int read_le) {
  double ret;
  char r[8];
  const void *p;
  int i;
  if (1 == read_le) {
    p = ptr;
  } else {
    for (i = 0; i < 8; i++) {
      r[i] = ((char *)ptr)[7 - i];
    }
    p = r;
  }
  memcpy(&ret, p, 8);
  return ret;
}


x86 assembly (clang, -O0):
<endian_read_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x1,%eax
cmp    -0xc(%rbp),%eax
jne    113a <endian_read_double+0x2a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117b <endian_read_double+0x6b>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x8,-0x2c(%rbp)
jge    1173 <endian_read_double+0x63>
mov    -0x8(%rbp),%rax
mov    $0x7,%ecx
sub    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
movslq -0x2c(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1141 <endian_read_double+0x31>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x8,%edx
call   1030 <memcpy@plt>
movsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<endian_read_double>:
sub    $0x18,%rsp
mov    %rdi,%rax
cmp    $0x1,%esi
je     115b <endian_read_double+0x4b>
mov    0x7(%rax),%cl
mov    %cl,0x8(%rsp)
mov    0x6(%rax),%cl
mov    %cl,0x9(%rsp)
mov    0x5(%rax),%cl
mov    %cl,0xa(%rsp)
mov    0x4(%rax),%cl
mov    %cl,0xb(%rsp)
mov    0x3(%rax),%cl
mov    %cl,0xc(%rsp)
mov    0x2(%rax),%cl
mov    %cl,0xd(%rsp)
mov    0x1(%rax),%cl
mov    %cl,0xe(%rsp)
mov    (%rax),%al
lea    0x8(%rsp),%rcx
mov    %al,0xf(%rsp)
mov    %rcx,%rax
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
movsd  0x10(%rsp),%xmm0
add    $0x18,%rsp
ret

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

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