#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 */
scalar_t__ checked_add(size_t *, size_t, size_t);
int /*<<< orphan*/ ssh_set_error(char *);
int check_advance(size_t pos, size_t adv, size_t len) {
  size_t new_pos;
  if (checked_add(&new_pos, pos, adv) < 0) {
    ssh_set_error("data too large to read");
    return -1;
  }
  if (new_pos > len) {
    ssh_set_error("read past end of buffer");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_advance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   1030 <checked_add@plt>
cmp    $0x0,%rax
jge    1167 <check_advance+0x47>
lea    0xeaa(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <check_advance+0x74>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    118d <check_advance+0x6d>
lea    0xe9b(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <check_advance+0x74>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_advance>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%rdx
mov    %rdi,%rsi
lea    0x8(%rsp),%rdi
call   1030 <checked_add@plt>
test   %rax,%rax
js     114f <check_advance+0x2f>
xor    %eax,%eax
cmp    %rbx,0x8(%rsp)
jbe    1160 <check_advance+0x40>
lea    0xeca(%rip),%rdi
jmp    1156 <check_advance+0x36>
lea    0xeaa(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"data too large to read\00"
@.str.1 = [24 x i8] c"read past end of buffer\00"
define i32 @check_advance(i64 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i64, i64* %5
%10 = load i64, i64* %6
%11 = call i64 @checked_add(i64* %8, i64 %9, i64 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @ssh_set_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %22
15: ; preds = %3
%16 = load i64, i64* %8
%17 = load i64, i64* %7
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @ssh_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %22
21: ; preds = %15
store i32 0, i32* %4
br label %22
22: ; preds = %21, %19, %13
%23 = load i32, i32* %4
ret i32 %23
}

LLVM IR (-O2):
@.str = [23 x i8] c"data too large to read\00"
@.str.1 = [24 x i8] c"read past end of buffer\00"
define i32 @check_advance(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = call i64 @checked_add(i64* %4, i64 %0, i64 %1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = load i64, i64* %4
%10 = icmp ugt i64 %9, %2
br i1 %10, label %11, label %14
11: ; preds = %8, %3
%12 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %8 ]
%13 = call i32 @ssh_set_error(i8* %12)
br label %14
14: ; preds = %11, %8
%15 = phi i32 [ 0, %8 ], [ -1, %11 ]
ret i32 %15
}