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

/* Variables and functions */
int /*<<< orphan*/ LOCK_SH;
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ file_lock(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ file_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_bookmark_path();
int get_pageno(int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int bm_read_bookmark(const char *docpath) {
  if (docpath == NULL)
    return -1;
  char *bm_file = get_bookmark_path();
  if (bm_file == NULL) {
    fputs("can't get bookmark filename\n", stderr);
    return -1;
  }
  int bm_pageno = -1;
  errno = 0;
  FILE *fp = fopen(bm_file, "r");
  if (fp != NULL) {
    if (file_lock(fp, LOCK_SH)) {
      bm_pageno = get_pageno(fp, docpath);
      file_unlock(fp);
    }
    if (fclose(fp))
      fprintf(stderr, "%s; fclose: %s\n", bm_file, strerror(errno));
  } else
    fprintf(stderr, "%s; fopen: %s\n", bm_file, strerror(errno));
  free(bm_file);
  return bm_pageno;
}


x86 assembly (clang, -O0):
<bm_read_bookmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c3 <bm_read_bookmark+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fc <bm_read_bookmark+0x15c>
mov    $0x0,%al
call   10a0 <get_bookmark_path@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fa <bm_read_bookmark+0x5a>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   1070 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fc <bm_read_bookmark+0x15c>
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2dc8(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12b4 <bm_read_bookmark+0x114>
mov    -0x28(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1090 <file_lock@plt>
cmp    $0x0,%rax
je     1263 <bm_read_bookmark+0xc3>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <get_pageno@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <file_unlock@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     12af <bm_read_bookmark+0x10f>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xd75(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12ed <bm_read_bookmark+0x14d>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d01(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd47(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bm_read_bookmark>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     1287 <bm_read_bookmark+0xe7>
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <get_bookmark_path@plt>
test   %rax,%rax
je     121d <bm_read_bookmark+0x7d>
mov    %rax,%r14
mov    0x2e00(%rip),%r12
movq   $0x0,(%r12)
lea    0xe3e(%rip),%rsi
mov    %rax,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1234 <bm_read_bookmark+0x94>
mov    %rax,%rbp
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <file_lock@plt>
test   %rax,%rax
je     1243 <bm_read_bookmark+0xa3>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <get_pageno@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1060 <file_unlock@plt>
jmp    1249 <bm_read_bookmark+0xa9>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    0xdd3(%rip),%rdi
call   1070 <fputs@plt>
jmp    1287 <bm_read_bookmark+0xe7>
mov    $0xffffffff,%r15d
lea    0xdee(%rip),%rbx
jmp    125d <bm_read_bookmark+0xbd>
mov    $0xffffffff,%r15d
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     127f <bm_read_bookmark+0xdf>
lea    0xdc2(%rip),%rbx
mov    0x2d74(%rip),%rax
mov    (%rax),%ebp
mov    (%r12),%rdi
call   10c0 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"can't get bookmark filename\0A\00"
@stderr = global i32 0
@errno = global i64 0
@.str.1 = [2 x i8] c"r\00"
@LOCK_SH = global i32 0
@.str.2 = [16 x i8] c"%s; fclose: %s\0A\00"
@.str.3 = [15 x i8] c"%s; fopen: %s\0A\00"
define i32 @bm_read_bookmark(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %54
10: ; preds = %1
%11 = call i8* (...) @get_bookmark_path()
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 -1, i32* %2
br label %54
17: ; preds = %10
store i32 -1, i32* %5
store i64 0, i64* @errno
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %44
22: ; preds = %17
%23 = load i32*, i32** %6
%24 = load i32, i32* @LOCK_SH
%25 = call i64 @file_lock(i32* %23, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32*, i32** %6
%29 = load i8*, i8** %3
%30 = call i32 @get_pageno(i32* %28, i8* %29)
store i32 %30, i32* %5
%31 = load i32*, i32** %6
%32 = call i32 @file_unlock(i32* %31)
br label %33
33: ; preds = %27, %22
%34 = load i32*, i32** %6
%35 = call i64 @fclose(i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %4
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* %41)
br label %43
43: ; preds = %37, %33
br label %50
44: ; preds = %17
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %4
%47 = load i64, i64* @errno
%48 = call i8* @strerror(i64 %47)
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %46, i8* %48)
br label %50
50: ; preds = %44, %43
%51 = load i8*, i8** %4
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %5
store i32 %53, i32* %2
br label %54
54: ; preds = %50, %14, %9
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@.str = [29 x i8] c"can't get bookmark filename\0A\00"
@stderr = global i32 0
@errno = global i64 0
@.str.1 = [2 x i8] c"r\00"
@LOCK_SH = global i32 0
@.str.2 = [16 x i8] c"%s; fclose: %s\0A\00"
@.str.3 = [15 x i8] c"%s; fopen: %s\0A\00"
define i32 @bm_read_bookmark(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %33, label %3
3: ; preds = %1
%4 = tail call i8* (...) @get_bookmark_path()
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %33
9: ; preds = %3
store i64 0, i64* @errno
%10 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = load i32, i32* @LOCK_SH
%14 = tail call i64 @file_lock(i32* %10, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @get_pageno(i32* %10, i8* %0)
%18 = tail call i32 @file_unlock(i32* %10)
br label %19
19: ; preds = %16, %12
%20 = phi i32 [ %17, %16 ], [ -1, %12 ]
%21 = tail call i64 @fclose(i32* %10)
%22 = icmp eq i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %9, %19
%24 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %19 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %9 ]
%25 = phi i32 [ %20, %19 ], [ -1, %9 ]
%26 = load i32, i32* @stderr
%27 = load i64, i64* @errno
%28 = tail call i8* @strerror(i64 %27)
%29 = tail call i32 @fprintf(i32 %26, i8* %24, i8* %4, i8* %28)
br label %30
30: ; preds = %23, %19
%31 = phi i32 [ %20, %19 ], [ %25, %23 ]
%32 = tail call i32 @free(i8* %4)
br label %33
33: ; preds = %6, %30, %1
%34 = phi i32 [ -1, %1 ], [ -1, %6 ], [ %31, %30 ]
ret i32 %34
}