#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 ntohd(double net_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 (net_double);
  }
  conv_rec.whole_thing = net_double;
  conv_rec.words[0] = htonl(conv_rec.words[0]);
  conv_rec.words[1] = htonl(conv_rec.words[1]);
  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;
  }
  return (conv_rec.whole_thing);
}


x86 assembly (clang, -O0):
<ntohd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    $0x1,%edi
call   1040 <ntohl@plt>
cmp    $0x1,%rax
jne    1150 <ntohd+0x30>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11cb <ntohd+0xab>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jg     11c1 <ntohd+0xa1>
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    1177 <ntohd+0x57>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ntohd>:
push   %r14
push   %rbx
push   %rax
movsd  %xmm0,(%rsp)
mov    $0x1,%edi
call   1040 <ntohl@plt>
cmp    $0x1,%rax
jne    1143 <ntohd+0x23>
movq   (%rsp),%xmm0
jmp    11ce <ntohd+0xae>
movq   (%rsp),%xmm0
movq   %xmm0,%rbx
mov    %ebx,%edi
call   1030 <htonl@plt>
mov    %rax,%r14
shr    $0x20,%rbx
mov    %ebx,%edi
call   1030 <htonl@plt>
mov    %rax,%rcx
shl    $0x20,%rcx
shr    $0x38,%rcx
mov    %r14,%rdx
shl    $0x38,%rdx
or     %rcx,%rdx
mov    %rax,%rcx
shr    $0x8,%rcx
and    $0xff00,%ecx
or     %rdx,%rcx
mov    %r14d,%edx
and    $0xff00,%edx
shl    $0x28,%rdx
or     %rcx,%rdx
mov    %eax,%ecx
shl    $0x8,%ecx
and    $0xff0000,%ecx
or     %rdx,%rcx
mov    %r14d,%edx
and    $0xff0000,%edx
shl    $0x18,%rdx
or     %rcx,%rdx
shl    $0x18,%eax
or     %rdx,%rax
and    $0xff000000,%r14d
shl    $0x8,%r14
or     %rax,%r14
movq   %r14,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define double @ntohd(double %0) {
%2 = tail call i64 @ntohl(i64 1)
%3 = icmp eq i64 %2, 1
br i1 %3, label %36, label %4
4: ; preds = %1
%5 = bitcast double %0 to i64
%6 = trunc i64 %5 to i32
%7 = tail call i8* @htonl(i32 %6)
%8 = ptrtoint i8* %7 to i64
%9 = lshr i64 %5, 32
%10 = trunc i64 %9 to i32
%11 = tail call i8* @htonl(i32 %10)
%12 = ptrtoint i8* %11 to i64
%13 = shl i64 %12, 32
%14 = lshr i64 %13, 56
%15 = shl i64 %8, 56
%16 = lshr i64 %13, 40
%17 = and i64 %16, 65280
%18 = shl i64 %8, 40
%19 = and i64 %18, 71776119061217280
%20 = lshr exact i64 %13, 24
%21 = and i64 %20, 16711680
%22 = shl i64 %8, 24
%23 = and i64 %22, 280375465082880
%24 = shl i64 %12, 24
%25 = and i64 %24, 4278190080
%26 = or i64 %14, %15
%27 = or i64 %26, %17
%28 = or i64 %27, %19
%29 = shl i64 %8, 8
%30 = and i64 %29, 1095216660480
%31 = or i64 %28, %21
%32 = or i64 %31, %23
%33 = or i64 %32, %25
%34 = or i64 %33, %30
%35 = bitcast i64 %34 to double
br label %36
36: ; preds = %1, %4
%37 = phi double [ %35, %4 ], [ %0, %1 ]
ret double %37
}