#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 scalar_t__ ns_sect;

/* Variables and functions */
int /*<<< orphan*/ EMSGSIZE;
int /*<<< orphan*/ NS_GET16(int, unsigned char const *);
int NS_INT16SZ;
int NS_INT32SZ;
int /*<<< orphan*/ errno;
int local_ns_dn_skipname(unsigned char const *, unsigned char const *);
scalar_t__ ns_s_qd;
int local_ns_skiprr(const unsigned char *ptr, const unsigned char *eom,
                    ns_sect section, int count) {
  const unsigned char *optr = ptr;
  for (; count > 0; count--) {
    int b, rdlength;
    b = local_ns_dn_skipname(ptr, eom);
    if (b < 0) {
      errno = EMSGSIZE;
      return -1;
    }
    ptr += b + NS_INT16SZ + NS_INT16SZ;
    if (section != ns_s_qd) {
      if (ptr + NS_INT32SZ + NS_INT16SZ > eom) {
        errno = EMSGSIZE;
        return -1;
      }
      ptr += NS_INT32SZ;
      NS_GET16(rdlength, ptr);
      ptr += rdlength;
    }
  }
  if (ptr > eom) {
    errno = EMSGSIZE;
    return -1;
  }
  return ptr - optr;
}


x86 assembly (clang, -O0):
<local_ns_skiprr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1245 <local_ns_skiprr+0x125>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <local_ns_dn_skipname@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1181 <local_ns_skiprr+0x61>
mov    0x2e76(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <local_ns_skiprr+0x15f>
mov    -0x34(%rbp),%ecx
mov    0x2e35(%rip),%rax
add    (%rax),%ecx
mov    0x2e2c(%rip),%rax
add    (%rax),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
je     1232 <local_ns_skiprr+0x112>
mov    -0x10(%rbp),%rax
mov    0x2e25(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    0x2df0(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    11fe <local_ns_skiprr+0xde>
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd8(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <local_ns_skiprr+0x15f>
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <NS_GET16@plt>
mov    -0x38(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1237 <local_ns_skiprr+0x117>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    113f <local_ns_skiprr+0x1f>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1271 <local_ns_skiprr+0x151>
mov    0x2d86(%rip),%rax
mov    (%rax),%ecx
mov    0x2d65(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <local_ns_skiprr+0x15f>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local_ns_skiprr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rdi,%rax
mov    %rdi,(%rsp)
test   %ecx,%ecx
jle    11a7 <local_ns_skiprr+0x87>
mov    %ecx,%ebp
mov    %rdx,%r12
add    $0x1,%ebp
mov    0x2e75(%rip),%r13
mov    0x2e86(%rip),%r14
mov    (%rsp),%rbx
jmp    1170 <local_ns_skiprr+0x50>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rsi
call   1030 <NS_GET16@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jle    11a7 <local_ns_skiprr+0x87>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <local_ns_dn_skipname@plt>
test   %eax,%eax
js     11ac <local_ns_skiprr+0x8c>
movslq 0x0(%r13),%rcx
lea    (%rax,%rcx,2),%eax
cltq
add    %rax,%rbx
cmp    %r12,(%r14)
je     1168 <local_ns_skiprr+0x48>
mov    0x2e51(%rip),%rax
movslq (%rax),%rax
add    %rax,%rbx
add    %rbx,%rcx
cmp    %r15,%rcx
jbe    1160 <local_ns_skiprr+0x40>
jmp    11ac <local_ns_skiprr+0x8c>
cmp    %r15,%rbx
jbe    11c5 <local_ns_skiprr+0xa5>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    0x2e0c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    11c8 <local_ns_skiprr+0xa8>
sub    (%rsp),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EMSGSIZE = global i32 0
@errno = global i32 0
@NS_INT16SZ = global i32 0
@ns_s_qd = global i64 0
@NS_INT32SZ = global i32 0
define i32 @local_ns_skiprr(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
store i8* %13, i8** %10
br label %14
14: ; preds = %62, %4
%15 = load i32, i32* %9
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %65
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i32 @local_ns_dn_skipname(i8* %18, i8* %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @EMSGSIZE
store i32 %24, i32* @errno
store i32 -1, i32* %5
br label %78
25: ; preds = %17
%26 = load i32, i32* %11
%27 = load i32, i32* @NS_INT16SZ
%28 = add nsw i32 %26, %27
%29 = load i32, i32* @NS_INT16SZ
%30 = add nsw i32 %28, %29
%31 = load i8*, i8** %6
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %6
%34 = load i64, i64* %8
%35 = load i64, i64* @ns_s_qd
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %61
37: ; preds = %25
%38 = load i8*, i8** %6
%39 = load i32, i32* @NS_INT32SZ
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i32, i32* @NS_INT16SZ
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8*, i8** %7
%46 = icmp ugt i8* %44, %45
br i1 %46, label %47, label %49
47: ; preds = %37
%48 = load i32, i32* @EMSGSIZE
store i32 %48, i32* @errno
store i32 -1, i32* %5
br label %78
49: ; preds = %37
%50 = load i32, i32* @NS_INT32SZ
%51 = load i8*, i8** %6
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8* %53, i8** %6
%54 = load i32, i32* %12
%55 = load i8*, i8** %6
%56 = call i32 @NS_GET16(i32 %54, i8* %55)
%57 = load i32, i32* %12
%58 = load i8*, i8** %6
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %6
br label %61
61: ; preds = %49, %25
br label %62
62: ; preds = %61
%63 = load i32, i32* %9
%64 = add nsw i32 %63, -1
store i32 %64, i32* %9
br label %14
65: ; preds = %14
%66 = load i8*, i8** %6
%67 = load i8*, i8** %7
%68 = icmp ugt i8* %66, %67
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = load i32, i32* @EMSGSIZE
store i32 %70, i32* @errno
store i32 -1, i32* %5
br label %78
71: ; preds = %65
%72 = load i8*, i8** %6
%73 = load i8*, i8** %10
%74 = ptrtoint i8* %72 to i64
%75 = ptrtoint i8* %73 to i64
%76 = sub i64 %74, %75
%77 = trunc i64 %76 to i32
store i32 %77, i32* %5
br label %78
78: ; preds = %71, %69, %47, %23
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
@EMSGSIZE = global i32 0
@errno = global i32 0
@NS_INT16SZ = global i32 0
@ns_s_qd = global i64 0
@NS_INT32SZ = global i32 0
define i32 @local_ns_skiprr(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %36
6: ; preds = %4, %32
%7 = phi i8* [ %33, %32 ], [ %0, %4 ]
%8 = phi i32 [ %34, %32 ], [ %3, %4 ]
%9 = tail call i32 @local_ns_dn_skipname(i8* %7, i8* %1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = load i32, i32* @EMSGSIZE
store i32 %12, i32* @errno
br label %46
13: ; preds = %6
%14 = load i32, i32* @NS_INT16SZ
%15 = shl i32 %14, 1
%16 = add i32 %15, %9
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = load i64, i64* @ns_s_qd
%20 = icmp eq i64 %19, %2
br i1 %20, label %32, label %21
21: ; preds = %13
%22 = load i32, i32* @NS_INT32SZ
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %18, i64 %23
%25 = sext i32 %14 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = icmp ugt i8* %26, %1
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32, i32* @EMSGSIZE
store i32 %29, i32* @errno
br label %46
30: ; preds = %21
%31 = tail call i32 @NS_GET16(i32 undef, i8* %24)
br label %32
32: ; preds = %30, %13
%33 = phi i8* [ %18, %13 ], [ %24, %30 ]
%34 = add nsw i32 %8, -1
%35 = icmp sgt i32 %8, 1
br i1 %35, label %6, label %36
36: ; preds = %32, %4
%37 = phi i8* [ %0, %4 ], [ %33, %32 ]
%38 = icmp ugt i8* %37, %1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i32, i32* @EMSGSIZE
store i32 %40, i32* @errno
br label %46
41: ; preds = %36
%42 = ptrtoint i8* %37 to i64
%43 = ptrtoint i8* %0 to i64
%44 = sub i64 %42, %43
%45 = trunc i64 %44 to i32
br label %46
46: ; preds = %11, %28, %41, %39
%47 = phi i32 [ -1, %39 ], [ %45, %41 ], [ -1, %28 ], [ -1, %11 ]
ret i32 %47
}