#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 int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
long INT_MAX;
scalar_t__ errno;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int jl_readline(int /*<<< orphan*/ *, char **, size_t *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
long strtol(char *, int /*<<< orphan*/ *, int);
int get_pageno(FILE *fp, const char *docpath) {
  char *line = NULL;
  size_t size = 128;
  size_t docpath_len = strlen(docpath);
  long bm_pageno = -1;
  ssize_t read;
  while ((read = jl_readline(fp, &line, &size)) > 0) {
    line[read - 1] = '\0';
    if (strncmp(line, docpath, docpath_len) != 0 ||
        strncmp(line + docpath_len, " = ", 3) != 0)
      continue;
    errno = 0;
    bm_pageno = strtol(line + docpath_len + 3, NULL, 10);
    if (errno != 0) {
      bm_pageno = -1;
      perror("can't read bookmark page number; strtol");
    } else if (bm_pageno > INT_MAX) {
      bm_pageno = -1;
      fputs("bookmark page number is too big\n", stderr);
    } else if (bm_pageno < 1) {
      bm_pageno = -1;
      fputs("bookmark page number is not positive\n", stderr);
    }
    break;
  }
  free(line);
  return bm_pageno;
}


x86 assembly (clang, -O0):
<get_pageno>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x80,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x28(%rbp)
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <jl_readline@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jle    12db <get_pageno+0x16b>
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1213 <get_pageno+0xa3>
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
lea    0xe01(%rip),%rsi
mov    $0x3,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     1218 <get_pageno+0xa8>
jmp    11a5 <get_pageno+0x35>
mov    0x2db1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x3,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d85(%rip),%rax
cmpq   $0x0,(%rax)
je     126e <get_pageno+0xfe>
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
jmp    12d6 <get_pageno+0x166>
mov    -0x30(%rbp),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
jle    12a4 <get_pageno+0x134>
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
lea    0xd92(%rip),%rdi
call   1070 <fputs@plt>
jmp    12d1 <get_pageno+0x161>
cmpq   $0x1,-0x30(%rbp)
jge    12cc <get_pageno+0x15c>
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
lea    0xd86(%rip),%rdi
call   1070 <fputs@plt>
jmp    12d1 <get_pageno+0x161>
jmp    12d6 <get_pageno+0x166>
jmp    12db <get_pageno+0x16b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pageno>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
movq   $0x80,0x10(%rsp)
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %r15,%rdi
call   1040 <jl_readline@plt>
mov    $0xffffffffffffffff,%rbp
test   %eax,%eax
jle    129f <get_pageno+0x12f>
lea    0xe37(%rip),%r13
lea    0x8(%rsp),%rbp
lea    0x10(%rsp),%r14
jmp    11f6 <get_pageno+0x86>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1040 <jl_readline@plt>
test   %eax,%eax
jle    1298 <get_pageno+0x128>
mov    0x8(%rsp),%rcx
add    $0xffffffff,%eax
movb   $0x0,(%rcx,%rax,1)
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    11e0 <get_pageno+0x70>
mov    0x8(%rsp),%rdi
add    %rbx,%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    11e0 <get_pageno+0x70>
mov    0x2d99(%rip),%r14
movq   $0x0,(%r14)
mov    0x8(%rsp),%rax
lea    (%rax,%rbx,1),%rdi
add    $0x3,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
cmpq   $0x0,(%r14)
jne    12ba <get_pageno+0x14a>
mov    %rax,%rbp
mov    0x2d81(%rip),%rax
cmp    (%rax),%rbp
jle    127e <get_pageno+0x10e>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xdb0(%rip),%rdi
jmp    1293 <get_pageno+0x123>
test   %rbp,%rbp
jg     129f <get_pageno+0x12f>
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    0xdba(%rip),%rdi
call   1070 <fputs@plt>
mov    $0xffffffffffffffff,%rbp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd43(%rip),%rdi
call   1090 <perror@plt>
jmp    1298 <get_pageno+0x128>

LLVM IR (-O0):
@.str = [4 x i8] c" = \00"
@errno = global i64 0
@.str.1 = [40 x i8] c"can't read bookmark page number; strtol\00"
@INT_MAX = global i64 0
@.str.2 = [33 x i8] c"bookmark page number is too big\0A\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"bookmark page number is not positive\0A\00"
define i32 @get_pageno(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
store i8* null, i8** %5
store i64 128, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
store i64 -1, i64* %8
br label %12
12: ; preds = %34, %2
%13 = load i32*, i32** %3
%14 = call i32 @jl_readline(i32* %13, i8** %5, i64* %6)
store i32 %14, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %61
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i32, i32* %9
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 0, i8* %21
%22 = load i8*, i8** %5
%23 = load i8*, i8** %4
%24 = load i64, i64* %7
%25 = trunc i64 %24 to i32
%26 = call i64 @strncmp(i8* %22, i8* %23, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %16
%29 = load i8*, i8** %5
%30 = load i64, i64* %7
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28, %16
br label %12
35: ; preds = %28
store i64 0, i64* @errno
%36 = load i8*, i8** %5
%37 = load i64, i64* %7
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = getelementptr inbounds i8, i8* %38, i64 3
%40 = call i64 @strtol(i8* %39, i32* null, i32 10)
store i64 %40, i64* %8
%41 = load i64, i64* @errno
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
store i64 -1, i64* %8
%44 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %60
45: ; preds = %35
%46 = load i64, i64* %8
%47 = load i64, i64* @INT_MAX
%48 = icmp sgt i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
store i64 -1, i64* %8
%50 = load i32, i32* @stderr
%51 = call i32 @fputs(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %50)
br label %59
52: ; preds = %45
%53 = load i64, i64* %8
%54 = icmp slt i64 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %52
store i64 -1, i64* %8
%56 = load i32, i32* @stderr
%57 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %55, %52
br label %59
59: ; preds = %58, %49
br label %60
60: ; preds = %59, %43
br label %61
61: ; preds = %60, %12
%62 = load i8*, i8** %5
%63 = call i32 @free(i8* %62)
%64 = load i64, i64* %8
%65 = trunc i64 %64 to i32
ret i32 %65
}

LLVM IR (-O2):
@.str = [4 x i8] c" = \00"
@errno = global i64 0
@.str.1 = [40 x i8] c"can't read bookmark page number; strtol\00"
@INT_MAX = global i64 0
@.str.2 = [33 x i8] c"bookmark page number is too big\0A\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"bookmark page number is not positive\0A\00"
define i32 @get_pageno(i32* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 128, i64* %4
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = call i32 @jl_readline(i32* %0, i8** %3, i64* %4)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %49
10: ; preds = %2
%11 = trunc i64 %7 to i32
br label %12
12: ; preds = %10, %26
%13 = phi i32 [ %8, %10 ], [ %27, %26 ]
%14 = load i8*, i8** %3
%15 = add nsw i32 %13, -1
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 0, i8* %17
%18 = load i8*, i8** %3
%19 = call i64 @strncmp(i8* %18, i8* %1, i32 %11)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %12
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 %7
%24 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21, %12
%27 = call i32 @jl_readline(i32* %0, i8** %3, i64* %4)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %12, label %49
29: ; preds = %21
store i64 0, i64* @errno
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 %7
%32 = getelementptr inbounds i8, i8* %31, i64 3
%33 = call i64 @strtol(i8* nocapture %32, i32* null, i32 10)
%34 = load i64, i64* @errno
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %49
38: ; preds = %29
%39 = load i64, i64* @INT_MAX
%40 = icmp sgt i64 %33, %39
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = call i32 @fputs(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %42)
br label %49
44: ; preds = %38
%45 = icmp slt i64 %33, 1
br i1 %45, label %46, label %49
46: ; preds = %44
%47 = load i32, i32* @stderr
%48 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %26, %2, %36, %44, %46, %41
%50 = phi i64 [ -1, %36 ], [ -1, %41 ], [ -1, %46 ], [ %33, %44 ], [ -1, %2 ], [ -1, %26 ]
%51 = load i8*, i8** %3
%52 = call i32 @free(i8* %51)
%53 = trunc i64 %50 to i32
ret i32 %53
}