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

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


x86 assembly (clang, -O0):
<marshall_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115d <marshall_double+0x3d>
mov    0x2e92(%rip),%rax
mov    (%rax),%ecx
mov    0x2e91(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <marshall_double+0xaa>
mov    -0x1c(%rbp),%eax
cmp    $0x8,%rax
jb     11c3 <marshall_double+0xa3>
lea    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    $0x4,%rdi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movl   $0x8,-0x4(%rbp)
jmp    11ca <marshall_double+0xaa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<marshall_double>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1180 <marshall_double+0x60>
xor    %eax,%eax
cmp    $0x8,%esi
jb     1197 <marshall_double+0x77>
mov    %rdi,%rbx
movq   %xmm0,%r14
mov    %r14,%rdi
shr    $0x20,%rdi
call   1030 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htonl@plt>
mov    %eax,(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
add    $0x4,%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    $0x8,%eax
jmp    1197 <marshall_double+0x77>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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