#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*/ memcpy(void *, char const *, int);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int unmarshall_u_int32(u_int32_t *val, const char *buf, unsigned int buflen) {
  if (!val || !buf) {
    errno = EINVAL;
    return -1;
  }
  if (buflen >= sizeof(u_int32_t)) {
    u_int32_t temp;
    memcpy((void *)&temp, buf, sizeof(u_int32_t));
    *val = ntohl(temp);
    return sizeof(u_int32_t);
  }
  return 0;
}


x86 assembly (clang, -O0):
<unmarshall_u_int32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <unmarshall_u_int32+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1167 <unmarshall_u_int32+0x47>
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    0x2e87(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <unmarshall_u_int32+0x89>
mov    -0x1c(%rbp),%eax
cmp    $0x4,%rax
jb     11a2 <unmarshall_u_int32+0x82>
lea    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%edi
call   1040 <ntohl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x4,-0x4(%rbp)
jmp    11a9 <unmarshall_u_int32+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unmarshall_u_int32>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1158 <unmarshall_u_int32+0x38>
test   %rsi,%rsi
je     1158 <unmarshall_u_int32+0x38>
xor    %ebp,%ebp
cmp    $0x4,%edx
jb     116f <unmarshall_u_int32+0x4f>
mov    %rdi,%rbx
lea    0x4(%rsp),%rdi
mov    $0x4,%ebp
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    0x4(%rsp),%edi
call   1040 <ntohl@plt>
mov    %eax,(%rbx)
jmp    116f <unmarshall_u_int32+0x4f>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    0x2e78(%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 @unmarshall_u_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 i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %11, %3
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 -1, i32* %4
br label %28
16: ; preds = %11
%17 = load i32, i32* %7
%18 = zext i32 %17 to i64
%19 = icmp uge i64 %18, 4
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = bitcast i32* %8 to i8*
%22 = load i8*, i8** %6
%23 = call i32 @memcpy(i8* %21, i8* %22, i32 4)
%24 = load i32, i32* %8
%25 = call i32 @ntohl(i32 %24)
%26 = load i32*, i32** %5
store i32 %25, i32* %26
store i32 4, i32* %4
br label %28
27: ; preds = %16
store i32 0, i32* %4
br label %28
28: ; preds = %27, %20, %14
%29 = load i32, i32* %4
ret i32 %29
}

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