#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*/ int64_t;
typedef int /*<<< orphan*/ int32_t;

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


x86 assembly (clang, -O0):
<marshall_int64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <marshall_int64+0x3b>
mov    0x2e94(%rip),%rax
mov    (%rax),%ecx
mov    0x2e93(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <marshall_int64+0xa8>
mov    -0x14(%rbp),%eax
cmp    $0x4,%rax
jb     11c1 <marshall_int64+0xa1>
lea    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movl   $0x4,-0x4(%rbp)
jmp    11c8 <marshall_int64+0xa8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<marshall_int64>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     117e <marshall_int64+0x5e>
xor    %ebp,%ebp
cmp    $0x4,%edx
jb     1195 <marshall_int64+0x75>
mov    %rsi,%rbx
mov    %edi,%r14d
call   1030 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x4,%ebp
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
add    $0x4,%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
jmp    1195 <marshall_int64+0x75>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @marshall_int64(i32 %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 %19
9: ; preds = %3
%10 = icmp ugt i32 %2, 3
br i1 %10, label %11, label %19
11: ; preds = %9
%12 = bitcast i32* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = tail call i32 @htonl(i32 poison)
store i32 %14, i32* %4
%15 = tail call i32 @htonl(i32 %0)
store i32 %15, i32* %5
%16 = call i32 @memcpy(i8* %1, i8* %12, i32 4)
%17 = getelementptr inbounds i8, i8* %1, i64 4
%18 = call i32 @memcpy(i8* %17, i8* %13, i32 4)
br label %19
19: ; preds = %9, %11, %7
%20 = phi i32 [ 4, %11 ], [ -1, %7 ], [ 0, %9 ]
ret i32 %20
}