#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 size_t off_t;
typedef size_t int64_t;
typedef size_t int32_t;

/* Variables and functions */
size_t EXIT_FAILURE;
size_t EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, ...);
int ntohl(size_t);
int /*<<< orphan*/ stderr;
int32_t io_int_nrecords(int32_t *fmap, int64_t len, int32_t *nrecords) {
  int32_t rfs = 0;
  int32_t res = 0;
  off_t pos = 0;
  off_t end = 0;
  *nrecords = 0;
  if (fmap == NULL || len == 0) {
    return (EXIT_FAILURE);
  }
  while (pos < len) {
    ++(*nrecords);
    rfs = ntohl(fmap[pos]) / sizeof(int32_t);
    end = pos + rfs + 1;
    if (end >= len) {
      fprintf(stderr, "record %d end is beyond mapped file", *nrecords);
      return (EXIT_FAILURE);
    }
    res = ntohl(fmap[end]) / sizeof(int32_t);
    if (rfs != res) {
      fprintf(stderr, "record %d start and end size mismatch (%d != %d)",
              *nrecords, rfs, res);
      return (EXIT_FAILURE);
    }
    if (rfs == 0) {
      fprintf(stderr, "record %d has a size of 0 at %ld", *nrecords, pos);
      return (EXIT_FAILURE);
    }
    pos = end + 1;
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<io_int_nrecords>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1175 <io_int_nrecords+0x55>
cmpq   $0x0,-0x18(%rbp)
jne    1188 <io_int_nrecords+0x68>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <io_int_nrecords+0x1b4>
jmp    118d <io_int_nrecords+0x6d>
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12c6 <io_int_nrecords+0x1a6>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <ntohl@plt>
cltq
shr    $0x3,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1213 <io_int_nrecords+0xf3>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <io_int_nrecords+0x1b4>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <ntohl@plt>
cltq
shr    $0x3,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1275 <io_int_nrecords+0x155>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <io_int_nrecords+0x1b4>
cmpq   $0x0,-0x28(%rbp)
jne    12b5 <io_int_nrecords+0x195>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x38(%rbp),%rcx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <io_int_nrecords+0x1b4>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    118d <io_int_nrecords+0x6d>
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_int_nrecords>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
movq   $0x0,(%rdx)
mov    0x2e9b(%rip),%rax
test   %rdi,%rdi
je     11fc <io_int_nrecords+0xdc>
mov    %rsi,%r15
test   %rsi,%rsi
je     11fc <io_int_nrecords+0xdc>
mov    %rdi,%r12
mov    (%rsp),%rax
movq   $0x1,(%rax)
mov    (%rdi),%rdi
call   1040 <ntohl@plt>
mov    %eax,%ebp
movslq %eax,%r14
shr    $0x3,%r14
lea    0x1(%r14),%rax
cmp    %r15,%rax
jae    11d7 <io_int_nrecords+0xb7>
xor    %r13d,%r13d
mov    %r14,%rbx
mov    (%r12,%rax,8),%rdi
call   1040 <ntohl@plt>
movslq %eax,%r8
shr    $0x3,%r8
cmp    %r8,%rbx
jne    120e <io_int_nrecords+0xee>
cmp    $0x7,%ebp
jbe    1231 <io_int_nrecords+0x111>
lea    0x2(%r14),%r13
cmp    %r15,%r13
jae    1254 <io_int_nrecords+0x134>
mov    (%rsp),%rax
addq   $0x1,(%rax)
mov    0x10(%r12,%r14,8),%rdi
call   1040 <ntohl@plt>
mov    %eax,%ebp
movslq %eax,%rbx
shr    $0x3,%rbx
lea    (%rbx,%r13,1),%r14
lea    (%rbx,%r13,1),%rax
add    $0x1,%rax
cmp    %r15,%rax
jb     1180 <io_int_nrecords+0x60>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rax
mov    (%rax),%rdx
lea    0xe12(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rax
mov    (%rax),%rdx
lea    0xdff(%rip),%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11f5 <io_int_nrecords+0xd5>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rax
mov    (%rax),%rdx
lea    0xe0d(%rip),%rsi
mov    %r13,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11f5 <io_int_nrecords+0xd5>
mov    0x2d75(%rip),%rax
jmp    11fc <io_int_nrecords+0xdc>

LLVM IR (-O0):
@EXIT_FAILURE = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"record %d end is beyond mapped file\00"
@.str.1 = [49 x i8] c"record %d start and end size mismatch (%d != %d)\00"
@.str.2 = [33 x i8] c"record %d has a size of 0 at %ld\00"
@EXIT_SUCCESS = global i64 0
define i64 @io_int_nrecords(i64* %0, i64 %1, i64* %2) {
%4 = alloca i64
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i64* %0, i64** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i64 0, i64* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i64 0, i64* %11
%12 = load i64*, i64** %7
store i64 0, i64* %12
%13 = load i64*, i64** %5
%14 = icmp eq i64* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15, %3
%19 = load i64, i64* @EXIT_FAILURE
store i64 %19, i64* %4
br label %83
20: ; preds = %15
br label %21
21: ; preds = %78, %20
%22 = load i64, i64* %10
%23 = load i64, i64* %6
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %81
25: ; preds = %21
%26 = load i64*, i64** %7
%27 = load i64, i64* %26
%28 = add i64 %27, 1
store i64 %28, i64* %26
%29 = load i64*, i64** %5
%30 = load i64, i64* %10
%31 = getelementptr inbounds i64, i64* %29, i64 %30
%32 = load i64, i64* %31
%33 = call i32 @ntohl(i64 %32)
%34 = sext i32 %33 to i64
%35 = udiv i64 %34, 8
store i64 %35, i64* %8
%36 = load i64, i64* %10
%37 = load i64, i64* %8
%38 = add i64 %36, %37
%39 = add i64 %38, 1
store i64 %39, i64* %11
%40 = load i64, i64* %11
%41 = load i64, i64* %6
%42 = icmp uge i64 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %25
%44 = load i32, i32* @stderr
%45 = load i64*, i64** %7
%46 = load i64, i64* %45
%47 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %46)
%48 = load i64, i64* @EXIT_FAILURE
store i64 %48, i64* %4
br label %83
49: ; preds = %25
%50 = load i64*, i64** %5
%51 = load i64, i64* %11
%52 = getelementptr inbounds i64, i64* %50, i64 %51
%53 = load i64, i64* %52
%54 = call i32 @ntohl(i64 %53)
%55 = sext i32 %54 to i64
%56 = udiv i64 %55, 8
store i64 %56, i64* %9
%57 = load i64, i64* %8
%58 = load i64, i64* %9
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %68
60: ; preds = %49
%61 = load i32, i32* @stderr
%62 = load i64*, i64** %7
%63 = load i64, i64* %62
%64 = load i64, i64* %8
%65 = load i64, i64* %9
%66 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 %63, i64 %64, i64 %65)
%67 = load i64, i64* @EXIT_FAILURE
store i64 %67, i64* %4
br label %83
68: ; preds = %49
%69 = load i64, i64* %8
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = load i64*, i64** %7
%74 = load i64, i64* %73
%75 = load i64, i64* %10
%76 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 %74, i64 %75)
%77 = load i64, i64* @EXIT_FAILURE
store i64 %77, i64* %4
br label %83
78: ; preds = %68
%79 = load i64, i64* %11
%80 = add i64 %79, 1
store i64 %80, i64* %10
br label %21
81: ; preds = %21
%82 = load i64, i64* @EXIT_SUCCESS
store i64 %82, i64* %4
br label %83
83: ; preds = %81, %71, %60, %43, %18
%84 = load i64, i64* %4
ret i64 %84
}

LLVM IR (-O2):
@EXIT_FAILURE = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"record %d end is beyond mapped file\00"
@.str.1 = [49 x i8] c"record %d start and end size mismatch (%d != %d)\00"
@.str.2 = [33 x i8] c"record %d has a size of 0 at %ld\00"
@EXIT_SUCCESS = global i64 0
define i64 @io_int_nrecords(i64* %0, i64 %1, i64* nocapture %2) {
store i64 0, i64* %2
%4 = icmp eq i64* %0, null
%5 = icmp eq i64 %1, 0
%6 = or i1 %4, %5
br i1 %6, label %54, label %7
7: ; preds = %3
store i64 1, i64* %2
%8 = load i64, i64* %0
%9 = tail call i32 @ntohl(i64 %8)
%10 = sext i32 %9 to i64
%11 = lshr i64 %10, 3
%12 = add nuw nsw i64 %11, 1
%13 = icmp ult i64 %12, %1
br i1 %13, label %31, label %27
14: ; preds = %47
%15 = icmp ult i64 %49, %1
br i1 %15, label %16, label %54
16: ; preds = %14
%17 = load i64, i64* %2
%18 = add i64 %17, 1
store i64 %18, i64* %2
%19 = getelementptr inbounds i64, i64* %0, i64 %49
%20 = load i64, i64* %19
%21 = tail call i32 @ntohl(i64 %20)
%22 = sext i32 %21 to i64
%23 = lshr i64 %22, 3
%24 = add i64 %23, %49
%25 = add i64 %24, 1
%26 = icmp ult i64 %25, %1
br i1 %26, label %31, label %27
27: ; preds = %16, %7
%28 = load i32, i32* @stderr
%29 = load i64, i64* %2
%30 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %29)
br label %54
31: ; preds = %7, %16
%32 = phi i64 [ %25, %16 ], [ %12, %7 ]
%33 = phi i64 [ %24, %16 ], [ %11, %7 ]
%34 = phi i64 [ %23, %16 ], [ %11, %7 ]
%35 = phi i32 [ %21, %16 ], [ %9, %7 ]
%36 = phi i64 [ %49, %16 ], [ 0, %7 ]
%37 = getelementptr inbounds i64, i64* %0, i64 %32
%38 = load i64, i64* %37
%39 = tail call i32 @ntohl(i64 %38)
%40 = sext i32 %39 to i64
%41 = lshr i64 %40, 3
%42 = icmp eq i64 %34, %41
br i1 %42, label %47, label %43
43: ; preds = %31
%44 = load i32, i32* @stderr
%45 = load i64, i64* %2
%46 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 %45, i64 %34, i64 %41)
br label %54
47: ; preds = %31
%48 = icmp ult i32 %35, 8
%49 = add i64 %33, 2
br i1 %48, label %50, label %14
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i64, i64* %2
%53 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 %52, i64 %36)
br label %54
54: ; preds = %14, %3, %50, %43, %27
%55 = phi i64* [ @EXIT_FAILURE, %27 ], [ @EXIT_FAILURE, %43 ], [ @EXIT_FAILURE, %50 ], [ @EXIT_FAILURE, %3 ], [ @EXIT_SUCCESS, %14 ]
%56 = load i64, i64* %55
ret i64 %56
}