#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 */
unsigned int ntohl(unsigned int);
char *sig_getdatafromfile(char *filedata, unsigned int filelen,
                          unsigned int *datalen) {
  if (filelen < 8)
    return NULL;
  *datalen = ntohl(*(unsigned int *)filedata);
  if (sizeof(*datalen) + *datalen + sizeof(unsigned int) > filelen)
    return NULL;
  return filedata + sizeof(*datalen);
}


x86 assembly (clang, -O0):
<sig_getdatafromfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x8,-0x14(%rbp)
jae    113a <sig_getdatafromfile+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    1180 <sig_getdatafromfile+0x70>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohl@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x4,%rax
add    $0x4,%rax
mov    -0x14(%rbp),%ecx
cmp    %rcx,%rax
jbe    1174 <sig_getdatafromfile+0x64>
movq   $0x0,-0x8(%rbp)
jmp    1180 <sig_getdatafromfile+0x70>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_getdatafromfile>:
cmp    $0x8,%esi
jae    1118 <sig_getdatafromfile+0x8>
xor    %eax,%eax
ret
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1030 <ntohl@plt>
mov    %eax,(%r14)
mov    %eax,%ecx
add    $0x8,%rcx
mov    %ebp,%edx
add    $0x4,%rbx
xor    %eax,%eax
cmp    %rdx,%rcx
cmovbe %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @sig_getdatafromfile(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%8 = load i32, i32* %6
%9 = icmp ult i32 %8, 8
br i1 %9, label %10, label %11
10: ; preds = %3
store i8* null, i8** %4
br label %29
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
%15 = call i32 @ntohl(i32 %14)
%16 = load i32*, i32** %7
store i32 %15, i32* %16
%17 = load i32*, i32** %7
%18 = load i32, i32* %17
%19 = zext i32 %18 to i64
%20 = add i64 4, %19
%21 = add i64 %20, 4
%22 = load i32, i32* %6
%23 = zext i32 %22 to i64
%24 = icmp ugt i64 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %11
store i8* null, i8** %4
br label %29
26: ; preds = %11
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 4
store i8* %28, i8** %4
br label %29
29: ; preds = %26, %25, %10
%30 = load i8*, i8** %4
ret i8* %30
}

LLVM IR (-O2):
define i8* @sig_getdatafromfile(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = icmp ult i32 %1, 8
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = bitcast i8* %0 to i32*
%7 = load i32, i32* %6
%8 = tail call i32 @ntohl(i32 %7)
store i32 %8, i32* %2
%9 = zext i32 %8 to i64
%10 = add nuw nsw i64 %9, 8
%11 = zext i32 %1 to i64
%12 = icmp ugt i64 %10, %11
%13 = getelementptr inbounds i8, i8* %0, i64 4
%14 = select i1 %12, i8* null, i8* %13
br label %15
15: ; preds = %5, %3
%16 = phi i8* [ null, %3 ], [ %14, %5 ]
ret i8* %16
}