#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*/ u_int64_t;
typedef int /*<<< orphan*/ u_int32_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(void *, char const *, int);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int unmarshall_u_int64(u_int64_t *val, const char *buf, unsigned int buflen) {
  if (!val || !buf) {
    errno = EINVAL;
    return -1;
  }
  if (buflen >= sizeof(u_int64_t)) {
    u_int32_t word0, word1;
    u_int32_t *ptr = (u_int32_t *)val;
    memcpy((void *)&word0, buf, sizeof(u_int32_t));
    memcpy((void *)&word1, buf + sizeof(u_int32_t), sizeof(u_int32_t));
    ptr[0] = ntohl(word1);
    ptr[1] = ntohl(word0);
    return (sizeof(u_int64_t));
  }
  return 0;
}


x86 assembly (clang, -O0):
<unmarshall_u_int64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <unmarshall_u_int64+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1167 <unmarshall_u_int64+0x47>
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    0x2e87(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <unmarshall_u_int64+0xb8>
mov    -0x1c(%rbp),%eax
cmp    $0x4,%rax
jb     11d1 <unmarshall_u_int64+0xb1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
lea    -0x24(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x24(%rbp),%edi
call   1040 <ntohl@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
call   1040 <ntohl@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x4,-0x4(%rbp)
jmp    11d8 <unmarshall_u_int64+0xb8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unmarshall_u_int64>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1187 <unmarshall_u_int64+0x67>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1187 <unmarshall_u_int64+0x67>
xor    %r14d,%r14d
cmp    $0x4,%edx
jb     119f <unmarshall_u_int64+0x7f>
mov    %rdi,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%r14d
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
add    $0x4,%rbp
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1040 <ntohl@plt>
mov    %eax,(%rbx)
mov    0xc(%rsp),%edi
call   1040 <ntohl@plt>
mov    %eax,0x4(%rbx)
jmp    119f <unmarshall_u_int64+0x7f>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    0x2e49(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @unmarshall_u_int64(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13, %3
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %4
br label %40
18: ; preds = %13
%19 = load i32, i32* %7
%20 = zext i32 %19 to i64
%21 = icmp uge i64 %20, 4
br i1 %21, label %22, label %39
22: ; preds = %18
%23 = load i32*, i32** %5
store i32* %23, i32** %10
%24 = bitcast i32* %8 to i8*
%25 = load i8*, i8** %6
%26 = call i32 @memcpy(i8* %24, i8* %25, i32 4)
%27 = bitcast i32* %9 to i8*
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 4
%30 = call i32 @memcpy(i8* %27, i8* %29, i32 4)
%31 = load i32, i32* %9
%32 = call i32 @ntohl(i32 %31)
%33 = load i32*, i32** %10
%34 = getelementptr inbounds i32, i32* %33, i64 0
store i32 %32, i32* %34
%35 = load i32, i32* %8
%36 = call i32 @ntohl(i32 %35)
%37 = load i32*, i32** %10
%38 = getelementptr inbounds i32, i32* %37, i64 1
store i32 %36, i32* %38
store i32 4, i32* %4
br label %40
39: ; preds = %18
store i32 0, i32* %4
br label %40
40: ; preds = %39, %22, %16
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @unmarshall_u_int64(i32* writeonly %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = icmp ne i32* %0, null
%7 = icmp ne i8* %1, null
%8 = and i1 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %24
11: ; preds = %3
%12 = icmp ugt i32 %2, 3
br i1 %12, label %13, label %24
13: ; preds = %11
%14 = bitcast i32* %4 to i8*
%15 = bitcast i32* %5 to i8*
%16 = call i32 @memcpy(i8* %14, i8* %1, i32 4)
%17 = getelementptr inbounds i8, i8* %1, i64 4
%18 = call i32 @memcpy(i8* %15, i8* %17, i32 4)
%19 = load i32, i32* %5
%20 = call i32 @ntohl(i32 %19)
store i32 %20, i32* %0
%21 = load i32, i32* %4
%22 = call i32 @ntohl(i32 %21)
%23 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %22, i32* %23
br label %24
24: ; preds = %11, %13, %9
%25 = phi i32 [ 4, %13 ], [ -1, %9 ], [ 0, %11 ]
ret i32 %25
}