#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 /*<<< orphan*/ uint64_t;
typedef void *uint32_t;

/* Variables and functions */
long htonl(int);
void *ntohl(void *);
uint64_t ntoh64(uint64_t val) {
  union {
    uint64_t ll;
    uint32_t l[2];
  } w, r;
  if (htonl(1) == 1L)
    return val;
  w.ll = val;
  r.l[0] = ntohl(w.l[1]);
  r.l[1] = ntohl(w.l[0]);
  return r.ll;
}


x86 assembly (clang, -O0):
<ntoh64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x1,%edi
call   1030 <htonl@plt>
cmp    $0x1,%rax
jne    114a <ntoh64+0x2a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1170 <ntoh64+0x50>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ntohl@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <ntohl@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ntoh64>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x1,%edi
call   1030 <htonl@plt>
cmp    $0x1,%rax
je     1149 <ntoh64+0x29>
mov    %ebx,%r14d
call   1040 <ntohl@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <ntohl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%union.anon = type { [2 x i8*] }
define i32 @ntoh64(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %union.anon
%5 = alloca %union.anon
store i32 %0, i32* %3
%6 = call i64 @htonl(i32 1)
%7 = icmp eq i64 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* %3
store i32 %9, i32* %2
br label %27
10: ; preds = %1
%11 = load i32, i32* %3
%12 = bitcast %union.anon* %4 to i32*
store i32 %11, i32* %12
%13 = bitcast %union.anon* %4 to [2 x i8*]*
%14 = getelementptr inbounds [2 x i8*], [2 x i8*]* %13, i64 0, i64 1
%15 = load i8*, i8** %14
%16 = call i8* @ntohl(i8* %15)
%17 = bitcast %union.anon* %5 to [2 x i8*]*
%18 = getelementptr inbounds [2 x i8*], [2 x i8*]* %17, i64 0, i64 0
store i8* %16, i8** %18
%19 = bitcast %union.anon* %4 to [2 x i8*]*
%20 = getelementptr inbounds [2 x i8*], [2 x i8*]* %19, i64 0, i64 0
%21 = load i8*, i8** %20
%22 = call i8* @ntohl(i8* %21)
%23 = bitcast %union.anon* %5 to [2 x i8*]*
%24 = getelementptr inbounds [2 x i8*], [2 x i8*]* %23, i64 0, i64 1
store i8* %22, i8** %24
%25 = bitcast %union.anon* %5 to i32*
%26 = load i32, i32* %25
store i32 %26, i32* %2
br label %27
27: ; preds = %10, %8
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
define i32 @ntoh64(i32 %0) {
%2 = tail call i64 @htonl(i32 1)
%3 = icmp eq i64 %2, 1
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = zext i32 %0 to i64
%6 = inttoptr i64 %5 to i8*
%7 = tail call i8* @ntohl(i8* undef)
%8 = tail call i8* @ntohl(i8* %6)
%9 = ptrtoint i8* %7 to i64
%10 = trunc i64 %9 to i32
br label %11
11: ; preds = %1, %4
%12 = phi i32 [ %10, %4 ], [ %0, %1 ]
ret i32 %12
}