#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 uint64_t;

/* Variables and functions */
int htons(int);
uint64_t ntohll(uint64_t n) {
  static int is_host_big_endian = -1;
  if (is_host_big_endian == -1) {
    is_host_big_endian = (0xFACE == htons(0xFACE) ? 1 : 0);
  }
  if (is_host_big_endian) {
    return n;
  }
  return (
      ((n & 0x00000000000000FFULL) << 56) |
      ((n & 0x000000000000FF00ULL) << 40) |
      ((n & 0x0000000000FF0000ULL) << 24) | ((n & 0x00000000FF000000ULL) << 8) |
      ((n & 0x000000FF00000000ULL) >> 8) | ((n & 0x0000FF0000000000ULL) >> 24) |
      ((n & 0x00FF000000000000ULL) >> 40) |
      ((n & 0xFF00000000000000ULL) >> 56));
}


x86 assembly (clang, -O0):
<ntohll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xffffffff,0x2f06(%rip)
jne    114b <ntohll+0x3b>
mov    $0xface,%edi
call   1030 <htons@plt>
mov    %eax,%esi
mov    $0xface,%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,0x2edd(%rip)
cmpl   $0x0,0x2ed6(%rip)
je     1163 <ntohll+0x53>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <ntohll+0x100>
movslq -0x8(%rbp),%rax
and    $0xff,%rax
shl    $0x38,%rax
movslq -0x8(%rbp),%rcx
and    $0xff00,%rcx
shl    $0x28,%rcx
or     %rcx,%rax
movslq -0x8(%rbp),%rcx
and    $0xff0000,%rcx
shl    $0x18,%rcx
or     %rcx,%rax
movslq -0x8(%rbp),%rcx
movabs $0xff000000,%rdx
and    %rdx,%rcx
shl    $0x8,%rcx
or     %rcx,%rax
movslq -0x8(%rbp),%rcx
movabs $0xff00000000,%rdx
and    %rdx,%rcx
shr    $0x8,%rcx
or     %rcx,%rax
movslq -0x8(%rbp),%rcx
movabs $0xff0000000000,%rdx
and    %rdx,%rcx
shr    $0x18,%rcx
or     %rcx,%rax
movslq -0x8(%rbp),%rcx
movabs $0xff000000000000,%rdx
and    %rdx,%rcx
shr    $0x28,%rcx
or     %rcx,%rax
movslq -0x8(%rbp),%rcx
movabs $0xff00000000000000,%rdx
and    %rdx,%rcx
shr    $0x38,%rcx
or     %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ntohll>:
push   %rbx
mov    %edi,%ebx
mov    0x2f0f(%rip),%ecx
cmp    $0xffffffff,%ecx
jne    1138 <ntohll+0x28>
mov    $0xface,%edi
call   1030 <htons@plt>
xor    %ecx,%ecx
cmp    $0xface,%eax
sete   %cl
mov    %ecx,0x2ef0(%rip)
test   %ecx,%ecx
movslq %ebx,%rax
mov    %rax,%rcx
bswap  %rcx
cmove  %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@ntohll.is_host_big_endian = internal global i32 -1
define i32 @ntohll(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* @ntohll.is_host_big_endian
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = call i32 @htons(i32 64206)
%8 = icmp eq i32 64206, %7
%9 = zext i1 %8 to i64
%10 = select i1 %8, i32 1, i32 0
store i32 %10, i32* @ntohll.is_host_big_endian
br label %11
11: ; preds = %6, %1
%12 = load i32, i32* @ntohll.is_host_big_endian
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load i32, i32* %3
store i32 %15, i32* %2
br label %57
16: ; preds = %11
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = and i64 %18, 255
%20 = shl i64 %19, 56
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = and i64 %22, 65280
%24 = shl i64 %23, 40
%25 = or i64 %20, %24
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = and i64 %27, 16711680
%29 = shl i64 %28, 24
%30 = or i64 %25, %29
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = and i64 %32, 4278190080
%34 = shl i64 %33, 8
%35 = or i64 %30, %34
%36 = load i32, i32* %3
%37 = sext i32 %36 to i64
%38 = and i64 %37, 1095216660480
%39 = lshr i64 %38, 8
%40 = or i64 %35, %39
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = and i64 %42, 280375465082880
%44 = lshr i64 %43, 24
%45 = or i64 %40, %44
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = and i64 %47, 71776119061217280
%49 = lshr i64 %48, 40
%50 = or i64 %45, %49
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = and i64 %52, -72057594037927936
%54 = lshr i64 %53, 56
%55 = or i64 %50, %54
%56 = trunc i64 %55 to i32
store i32 %56, i32* %2
br label %57
57: ; preds = %16, %14
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@ntohll.is_host_big_endian = internal unnamed_addr global i32 -1
define i32 @ntohll(i32 %0) {
%2 = load i32, i32* @ntohll.is_host_big_endian
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @htons(i32 64206)
%6 = icmp eq i32 %5, 64206
%7 = zext i1 %6 to i32
store i32 %7, i32* @ntohll.is_host_big_endian
br label %8
8: ; preds = %4, %1
%9 = phi i32 [ %7, %4 ], [ %2, %1 ]
%10 = icmp eq i32 %9, 0
%11 = sext i32 %0 to i64
%12 = tail call i64 @llvm.bswap.i64(i64 %11)
%13 = trunc i64 %12 to i32
%14 = select i1 %10, i32 %13, i32 %0
ret i32 %14
}