#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*/ int32_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, void *, int);
int marshall_int32(int32_t val, char *buf, unsigned int buflen) {
  if (!buf) {
    errno = EINVAL;
    return -1;
  }
  if (buflen >= sizeof(int32_t)) {
    int32_t temp;
    temp = htonl(val);
    memcpy(buf, (void *)&temp, sizeof(int32_t));
    return sizeof(int32_t);
  }
  return 0;
}


x86 assembly (clang, -O0):
<marshall_int32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <marshall_int32+0x3b>
mov    0x2e94(%rip),%rax
mov    (%rax),%ecx
mov    0x2e93(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1198 <marshall_int32+0x78>
mov    -0x14(%rbp),%eax
cmp    $0x4,%rax
jb     1191 <marshall_int32+0x71>
mov    -0x8(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movl   $0x4,-0x4(%rbp)
jmp    1198 <marshall_int32+0x78>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<marshall_int32>:
push   %rbp
push   %rbx
push   %rax
test   %rsi,%rsi
je     1154 <marshall_int32+0x34>
xor    %ebp,%ebp
cmp    $0x4,%edx
jb     116b <marshall_int32+0x4b>
mov    %rsi,%rbx
call   1030 <htonl@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x4,%ebp
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
jmp    116b <marshall_int32+0x4b>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    0x2e7c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @marshall_int32(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i32 -1, i32* %4
br label %24
13: ; preds = %3
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = icmp uge i64 %15, 4
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* %5
%19 = call i32 @htonl(i32 %18)
store i32 %19, i32* %8
%20 = load i8*, i8** %6
%21 = bitcast i32* %8 to i8*
%22 = call i32 @memcpy(i8* %20, i8* %21, i32 4)
store i32 4, i32* %4
br label %24
23: ; preds = %13
store i32 0, i32* %4
br label %24
24: ; preds = %23, %17, %11
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @marshall_int32(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %14
8: ; preds = %3
%9 = icmp ugt i32 %2, 3
br i1 %9, label %10, label %14
10: ; preds = %8
%11 = bitcast i32* %4 to i8*
%12 = tail call i32 @htonl(i32 %0)
store i32 %12, i32* %4
%13 = call i32 @memcpy(i8* %1, i8* %11, i32 4)
br label %14
14: ; preds = %8, %10, %6
%15 = phi i32 [ 4, %10 ], [ -1, %6 ], [ 0, %8 ]
ret i32 %15
}