#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 */
void *htonl(unsigned int);
long ntohl(long);
double htond(double host_double) {
  union {
    double whole_thing;
    unsigned int words[2];
    unsigned char bytes[8];
  } conv_rec;
  unsigned char scratch;
  int i;
  if (ntohl(1L) == 1L) {
    return (host_double);
  }
  conv_rec.whole_thing = host_double;
  for (i = 0; i <= 3; i++) {
    scratch = conv_rec.bytes[i];
    conv_rec.bytes[i] = conv_rec.bytes[7 - i];
    conv_rec.bytes[7 - i] = scratch;
  }
  conv_rec.words[0] = htonl(conv_rec.words[0]);
  conv_rec.words[1] = htonl(conv_rec.words[1]);
  return (conv_rec.whole_thing);
}


x86 assembly (clang, -O0):
<htond>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    $0x1,%edi
call   1040 <ntohl@plt>
cmp    $0x1,%rax
jne    1150 <htond+0x30>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11cb <htond+0xab>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jg     11ab <htond+0x8b>
movslq -0x20(%rbp),%rax
mov    -0x18(%rbp,%rax,1),%al
mov    %al,-0x19(%rbp)
mov    $0x7,%eax
sub    -0x20(%rbp),%eax
cltq
mov    -0x18(%rbp,%rax,1),%cl
movslq -0x20(%rbp),%rax
mov    %cl,-0x18(%rbp,%rax,1)
mov    -0x19(%rbp),%cl
mov    $0x7,%eax
sub    -0x20(%rbp),%eax
cltq
mov    %cl,-0x18(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1161 <htond+0x41>
mov    -0x18(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x14(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htond>:
push   %r14
push   %rbx
push   %rax
movsd  %xmm0,(%rsp)
mov    $0x1,%edi
call   1040 <ntohl@plt>
cmp    $0x1,%rax
jne    1140 <htond+0x20>
movq   (%rsp),%xmm0
jmp    116e <htond+0x4e>
movq   (%rsp),%xmm0
movq   %xmm0,%rbx
bswap  %rbx
mov    %ebx,%edi
call   1030 <htonl@plt>
mov    %eax,%r14d
shr    $0x20,%rbx
mov    %ebx,%edi
call   1030 <htonl@plt>
shl    $0x20,%rax
or     %r14,%rax
movq   %rax,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%union.anon = type { double }
define double @htond(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca %union.anon
%5 = alloca i8
%6 = alloca i32
store double %0, double* %3
%7 = call i64 @ntohl(i64 1)
%8 = icmp eq i64 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load double, double* %3
store double %10, double* %2
br label %59
11: ; preds = %1
%12 = load double, double* %3
%13 = bitcast %union.anon* %4 to double*
store double %12, double* %13
store i32 0, i32* %6
br label %14
14: ; preds = %39, %11
%15 = load i32, i32* %6
%16 = icmp sle i32 %15, 3
br i1 %16, label %17, label %42
17: ; preds = %14
%18 = bitcast %union.anon* %4 to [8 x i8]*
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %18, i64 0, i64 %20
%22 = load i8, i8* %21
store i8 %22, i8* %5
%23 = bitcast %union.anon* %4 to [8 x i8]*
%24 = load i32, i32* %6
%25 = sub nsw i32 7, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %23, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = bitcast %union.anon* %4 to [8 x i8]*
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %29, i64 0, i64 %31
store i8 %28, i8* %32
%33 = load i8, i8* %5
%34 = bitcast %union.anon* %4 to [8 x i8]*
%35 = load i32, i32* %6
%36 = sub nsw i32 7, %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %34, i64 0, i64 %37
store i8 %33, i8* %38
br label %39
39: ; preds = %17
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %14
42: ; preds = %14
%43 = bitcast %union.anon* %4 to [2 x i32]*
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %43, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i8* @htonl(i32 %45)
%47 = ptrtoint i8* %46 to i32
%48 = bitcast %union.anon* %4 to [2 x i32]*
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %48, i64 0, i64 0
store i32 %47, i32* %49
%50 = bitcast %union.anon* %4 to [2 x i32]*
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %50, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i8* @htonl(i32 %52)
%54 = ptrtoint i8* %53 to i32
%55 = bitcast %union.anon* %4 to [2 x i32]*
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %55, i64 0, i64 1
store i32 %54, i32* %56
%57 = bitcast %union.anon* %4 to double*
%58 = load double, double* %57
store double %58, double* %2
br label %59
59: ; preds = %42, %9
%60 = load double, double* %2
ret double %60
}

LLVM IR (-O2):
define double @htond(double %0) {
%2 = tail call i64 @ntohl(i64 1)
%3 = icmp eq i64 %2, 1
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = bitcast double %0 to i64
%6 = call i64 @llvm.bswap.i64(i64 %5)
%7 = trunc i64 %6 to i32
%8 = tail call i8* @htonl(i32 %7)
%9 = ptrtoint i8* %8 to i64
%10 = and i64 %9, 4294967295
%11 = lshr i64 %6, 32
%12 = trunc i64 %11 to i32
%13 = tail call i8* @htonl(i32 %12)
%14 = ptrtoint i8* %13 to i64
%15 = shl i64 %14, 32
%16 = or i64 %10, %15
%17 = bitcast i64 %16 to double
br label %18
18: ; preds = %1, %4
%19 = phi double [ %17, %4 ], [ %0, %1 ]
ret double %19
}