#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 */
struct gbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
size_t gbuf_size(struct gbuf const *);
int validate_range(struct gbuf const *buf, size_t offset, size_t size) {
  size_t bufsz;
  assert(buf != NULL);
  bufsz = gbuf_size(buf);
  if (bufsz < offset) {
    return -1;
  }
  if (bufsz - offset < size) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<validate_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <gbuf_size@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    116d <validate_range+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <validate_range+0x72>
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    118b <validate_range+0x6b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1192 <validate_range+0x72>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_range>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <gbuf_size@plt>
mov    %rax,%rcx
sub    %r15,%rcx
xor    %edx,%edx
cmp    %r14,%rcx
setae  %dl
cmp    %r15,%rax
lea    -0x2(%rdx,%rdx,1),%ecx
mov    $0xffffffff,%eax
cmovae %ecx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.gbuf = type { i32 }
define i32 @validate_range(%struct.gbuf* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.gbuf*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store %struct.gbuf* %0, %struct.gbuf** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load %struct.gbuf*, %struct.gbuf** %5
%10 = icmp ne %struct.gbuf* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load %struct.gbuf*, %struct.gbuf** %5
%14 = call i64 @gbuf_size(%struct.gbuf* %13)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = load i64, i64* %6
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %27
19: ; preds = %3
%20 = load i64, i64* %8
%21 = load i64, i64* %6
%22 = sub i64 %20, %21
%23 = load i64, i64* %7
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -2, i32* %4
br label %27
26: ; preds = %19
store i32 0, i32* %4
br label %27
27: ; preds = %26, %25, %18
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
%struct.gbuf = type { i32 }
define i32 @validate_range(%struct.gbuf* %0, i64 %1, i64 %2) {
%4 = icmp ne %struct.gbuf* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i64 @gbuf_size(%struct.gbuf* %0)
%8 = icmp ult i64 %7, %1
%9 = sub i64 %7, %1
%10 = icmp ult i64 %9, %2
%11 = select i1 %10, i32 -2, i32 0
%12 = select i1 %8, i32 -1, i32 %11
ret i32 %12
}