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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
int unmarshall_buffer(char *val, unsigned int vallen, const char *buf,
                      unsigned int buflen) {
  if (!val || !buf) {
    errno = EINVAL;
    return -1;
  }
  if (buflen >= vallen) {
    memcpy(val, buf, vallen);
    return vallen;
  }
  return 0;
}


x86 assembly (clang, -O0):
<unmarshall_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     113c <unmarshall_buffer+0x2c>
cmpq   $0x0,-0x20(%rbp)
jne    115a <unmarshall_buffer+0x4a>
mov    0x2e95(%rip),%rax
mov    (%rax),%ecx
mov    0x2e94(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <unmarshall_buffer+0x78>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1181 <unmarshall_buffer+0x71>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <unmarshall_buffer+0x78>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unmarshall_buffer>:
push   %rbx
test   %rdi,%rdi
je     1131 <unmarshall_buffer+0x21>
test   %rdx,%rdx
je     1131 <unmarshall_buffer+0x21>
mov    %esi,%ebx
xor    %eax,%eax
cmp    %esi,%ecx
jb     112f <unmarshall_buffer+0x1f>
mov    %rdx,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    0x2e9f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
ret

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

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