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

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


x86 assembly (clang, -O0):
<marshall_int8>:
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    114b <marshall_int8+0x3b>
mov    0x2ea4(%rip),%rax
mov    (%rax),%ecx
mov    0x2ea3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    117d <marshall_int8+0x6d>
mov    -0x14(%rbp),%eax
cmp    $0x4,%rax
jb     1176 <marshall_int8+0x66>
mov    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
movl   $0x4,-0x4(%rbp)
jmp    117d <marshall_int8+0x6d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<marshall_int8>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,0xc(%rsp)
test   %rsi,%rsi
je     1141 <marshall_int8+0x31>
xor    %ebx,%ebx
cmp    $0x4,%edx
jb     1158 <marshall_int8+0x48>
lea    0xc(%rsp),%rax
mov    $0x4,%ebx
mov    %rsi,%rdi
mov    %rax,%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
jmp    1158 <marshall_int8+0x48>
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
mov    0x2e8f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @marshall_int8(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
store i32 %0, i32* %4
%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 %13
8: ; preds = %3
%9 = icmp ugt i32 %2, 3
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = bitcast i32* %4 to i8*
%12 = call i32 @memcpy(i8* %1, i8* %11, i32 4)
br label %13
13: ; preds = %8, %10, %6
%14 = phi i32 [ 4, %10 ], [ -1, %6 ], [ 0, %8 ]
ret i32 %14
}