#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_EX;
int /*<<< orphan*/ LOCK_SH;
int /*<<< orphan*/ change_pageno(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 char const *, int);
int /*<<< orphan*/ copy_file(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ file_lock(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ file_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_bookmark_path();
int mkstemp(char *);
int /*<<< orphan*/ *open_create_if_not_exist(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ remove(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void bm_save_bookmark(const char *docpath, int bm_pageno) {
  if (docpath == NULL || bm_pageno == -1)
    return;
  char *bm_file = get_bookmark_path();
  if (bm_file == NULL) {
    fputs("can't get bookmark filename\n", stderr);
    return;
  }
  FILE *fp = open_create_if_not_exist(bm_file);
  if (fp == NULL)
    goto clean1;
  char temp_file[] = "/tmp/mupdf_bookmark.XXXXXX";
  int temp_fd = mkstemp(temp_file);
  if (temp_fd == -1) {
    perror("can't create temporary file; mkstemp");
    goto clean2;
  }
  FILE *tmp = fdopen(temp_fd, "w+");
  if (tmp == NULL) {
    perror("can't get stream for temporary file; fdopen");
    goto clean3;
  }
  if (file_lock(fp, LOCK_SH)) {
    change_pageno(fp, tmp, docpath, bm_pageno);
    file_unlock(fp);
  } else
    goto clean4;
  rewind(fp);
  rewind(tmp);
  if (file_lock(fp, LOCK_EX)) {
    copy_file(tmp, fp);
    file_unlock(fp);
  }
clean4:
  if (fclose(tmp) != 0)
    perror("can't close temporary file; fclose");
clean3:
  if (remove(temp_file) != 0)
    perror("can't remove temporary file; remove");
clean2:
  if (fclose(fp) != 0)
    fprintf(stderr, "%s: fclose: %s\n", bm_file, strerror(errno));
clean1:
  free(bm_file);
}


x86 assembly (clang, -O0):
<bm_save_bookmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1224 <bm_save_bookmark+0x24>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1229 <bm_save_bookmark+0x29>
jmp    142e <bm_save_bookmark+0x22e>
mov    $0x0,%al
call   1100 <get_bookmark_path@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1259 <bm_save_bookmark+0x59>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    0xdb1(%rip),%rdi
call   1090 <fputs@plt>
jmp    142e <bm_save_bookmark+0x22e>
mov    -0x18(%rbp),%rdi
call   10c0 <open_create_if_not_exist@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1276 <bm_save_bookmark+0x76>
jmp    1425 <bm_save_bookmark+0x225>
mov    0xe53(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe4d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe4a(%rip),%ax
mov    %ax,-0x28(%rbp)
mov    0xe42(%rip),%al
mov    %al,-0x26(%rbp)
lea    -0x40(%rbp),%rdi
call   10d0 <mkstemp@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    12d2 <bm_save_bookmark+0xd2>
lea    0xd55(%rip),%rdi
call   1110 <perror@plt>
jmp    13d5 <bm_save_bookmark+0x1d5>
mov    -0x44(%rbp),%edi
lea    0xd66(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1301 <bm_save_bookmark+0x101>
lea    0xd4e(%rip),%rdi
call   1110 <perror@plt>
jmp    13b1 <bm_save_bookmark+0x1b1>
mov    -0x20(%rbp),%rdi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
call   10f0 <file_lock@plt>
cmp    $0x0,%rax
je     133f <bm_save_bookmark+0x13f>
mov    -0x20(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   10a0 <change_pageno@plt>
mov    -0x20(%rbp),%rdi
call   1070 <file_unlock@plt>
jmp    1344 <bm_save_bookmark+0x144>
jmp    138d <bm_save_bookmark+0x18d>
mov    -0x20(%rbp),%rdi
call   1080 <rewind@plt>
mov    -0x50(%rbp),%rdi
call   1080 <rewind@plt>
mov    -0x20(%rbp),%rdi
mov    0x2c87(%rip),%rax
mov    (%rax),%esi
call   10f0 <file_lock@plt>
cmp    $0x0,%rax
je     1388 <bm_save_bookmark+0x188>
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <copy_file@plt>
mov    -0x20(%rbp),%rdi
call   1070 <file_unlock@plt>
jmp    138d <bm_save_bookmark+0x18d>
mov    -0x50(%rbp),%rdi
call   1060 <fclose@plt>
cmp    $0x0,%rax
je     13ac <bm_save_bookmark+0x1ac>
lea    0xcca(%rip),%rdi
call   1110 <perror@plt>
jmp    13b1 <bm_save_bookmark+0x1b1>
lea    -0x40(%rbp),%rdi
call   1050 <remove@plt>
cmp    $0x0,%rax
je     13d0 <bm_save_bookmark+0x1d0>
lea    0xcc9(%rip),%rdi
call   1110 <perror@plt>
jmp    13d5 <bm_save_bookmark+0x1d5>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
cmp    $0x0,%rax
je     1420 <bm_save_bookmark+0x220>
mov    0x2be1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2bc5(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xc9d(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    1425 <bm_save_bookmark+0x225>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bm_save_bookmark>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
test   %rdi,%rdi
je     1355 <bm_save_bookmark+0x155>
mov    %esi,%ebp
cmp    $0xffffffff,%esi
je     1355 <bm_save_bookmark+0x155>
mov    %rdi,%rbx
xor    %eax,%eax
call   1100 <get_bookmark_path@plt>
test   %rax,%rax
je     1362 <bm_save_bookmark+0x162>
mov    %rax,%r14
mov    %rax,%rdi
call   10c0 <open_create_if_not_exist@plt>
test   %rax,%rax
je     134d <bm_save_bookmark+0x14d>
mov    %rax,%r15
movups 0xe8a(%rip),%xmm0
movups %xmm0,0xb(%rsp)
movaps 0xe73(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   10d0 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1383 <bm_save_bookmark+0x183>
lea    0xdc9(%rip),%rsi
mov    %eax,%edi
call   10e0 <fdopen@plt>
test   %rax,%rax
je     138c <bm_save_bookmark+0x18c>
mov    %rax,%r12
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10f0 <file_lock@plt>
test   %rax,%rax
je     12f3 <bm_save_bookmark+0xf3>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   10a0 <change_pageno@plt>
mov    %r15,%rdi
call   1070 <file_unlock@plt>
mov    %r15,%rdi
call   1080 <rewind@plt>
mov    %r12,%rdi
call   1080 <rewind@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10f0 <file_lock@plt>
test   %rax,%rax
je     12f3 <bm_save_bookmark+0xf3>
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <copy_file@plt>
mov    %r15,%rdi
call   1070 <file_unlock@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    1395 <bm_save_bookmark+0x195>
mov    %rsp,%rdi
call   1050 <remove@plt>
test   %rax,%rax
jne    13a6 <bm_save_bookmark+0x1a6>
mov    %r15,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
je     134d <bm_save_bookmark+0x14d>
mov    0x2ca7(%rip),%rax
mov    (%rax),%ebx
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xd78(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   10b0 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c67(%rip),%rax
mov    (%rax),%esi
lea    0xc8e(%rip),%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <fputs@plt>
lea    0xc93(%rip),%rdi
jmp    13ad <bm_save_bookmark+0x1ad>
lea    0xcb2(%rip),%rdi
jmp    139c <bm_save_bookmark+0x19c>
lea    0xcd5(%rip),%rdi
call   1110 <perror@plt>
jmp    1304 <bm_save_bookmark+0x104>
lea    0xce7(%rip),%rdi
call   1110 <perror@plt>
jmp    1315 <bm_save_bookmark+0x115>

LLVM IR (-O0):
@.str = [29 x i8] c"can't get bookmark filename\0A\00"
@stderr = global i32 0
@__const.bm_save_bookmark.temp_file = [27 x i8] c"/tmp/mupdf_bookmark.XXXXXX\00"
@.str.1 = [37 x i8] c"can't create temporary file; mkstemp\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [44 x i8] c"can't get stream for temporary file; fdopen\00"
@LOCK_SH = global i32 0
@LOCK_EX = global i32 0
@.str.4 = [35 x i8] c"can't close temporary file; fclose\00"
@.str.5 = [36 x i8] c"can't remove temporary file; remove\00"
@.str.6 = [16 x i8] c"%s: fclose: %s\0A\00"
@errno = global i32 0
define void @bm_save_bookmark(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [27 x i8]
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %12, %2
br label %102
16: ; preds = %12
%17 = call i8* (...) @get_bookmark_path()
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %102
23: ; preds = %16
%24 = load i8*, i8** %5
%25 = call i32* @open_create_if_not_exist(i8* %24)
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %23
br label %99
29: ; preds = %23
%30 = bitcast [27 x i8]* %7 to i8*
%31 = getelementptr inbounds [27 x i8], [27 x i8]* %7, i64 0, i64 0
%32 = call i32 @mkstemp(i8* %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %88
37: ; preds = %29
%38 = load i32, i32* %8
%39 = call i32* @fdopen(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %39, i32** %9
%40 = load i32*, i32** %9
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %81
44: ; preds = %37
%45 = load i32*, i32** %6
%46 = load i32, i32* @LOCK_SH
%47 = call i64 @file_lock(i32* %45, i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %44
%50 = load i32*, i32** %6
%51 = load i32*, i32** %9
%52 = load i8*, i8** %3
%53 = load i32, i32* %4
%54 = call i32 @change_pageno(i32* %50, i32* %51, i8* %52, i32 %53)
%55 = load i32*, i32** %6
%56 = call i32 @file_unlock(i32* %55)
br label %58
57: ; preds = %44
br label %74
58: ; preds = %49
%59 = load i32*, i32** %6
%60 = call i32 @rewind(i32* %59)
%61 = load i32*, i32** %9
%62 = call i32 @rewind(i32* %61)
%63 = load i32*, i32** %6
%64 = load i32, i32* @LOCK_EX
%65 = call i64 @file_lock(i32* %63, i32 %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %58
%68 = load i32*, i32** %9
%69 = load i32*, i32** %6
%70 = call i32 @copy_file(i32* %68, i32* %69)
%71 = load i32*, i32** %6
%72 = call i32 @file_unlock(i32* %71)
br label %73
73: ; preds = %67, %58
br label %74
74: ; preds = %73, %57
%75 = load i32*, i32** %9
%76 = call i64 @fclose(i32* %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78, %74
br label %81
81: ; preds = %80, %42
%82 = getelementptr inbounds [27 x i8], [27 x i8]* %7, i64 0, i64 0
%83 = call i64 @remove(i8* %82)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %81
%86 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %87
87: ; preds = %85, %81
br label %88
88: ; preds = %87, %35
%89 = load i32*, i32** %6
%90 = call i64 @fclose(i32* %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %98
92: ; preds = %88
%93 = load i32, i32* @stderr
%94 = load i8*, i8** %5
%95 = load i32, i32* @errno
%96 = call i8* @strerror(i32 %95)
%97 = call i32 @fprintf(i32 %93, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %94, i8* %96)
br label %98
98: ; preds = %92, %88
br label %99
99: ; preds = %98, %28
%100 = load i8*, i8** %5
%101 = call i32 @free(i8* %100)
br label %102
102: ; preds = %99, %20, %15
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"can't get bookmark filename\0A\00"
@stderr = global i32 0
@__const.bm_save_bookmark.temp_file = [27 x i8] c"/tmp/mupdf_bookmark.XXXXXX\00"
@.str.1 = [37 x i8] c"can't create temporary file; mkstemp\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [44 x i8] c"can't get stream for temporary file; fdopen\00"
@LOCK_SH = global i32 0
@LOCK_EX = global i32 0
@.str.4 = [35 x i8] c"can't close temporary file; fclose\00"
@.str.5 = [36 x i8] c"can't remove temporary file; remove\00"
@.str.6 = [16 x i8] c"%s: fclose: %s\0A\00"
@errno = global i32 0
define void @bm_save_bookmark(i8* %0, i32 %1) {
%3 = alloca [27 x i8]
%4 = icmp eq i8* %0, null
%5 = icmp eq i32 %1, -1
%6 = or i1 %4, %5
br i1 %6, label %60, label %7
7: ; preds = %2
%8 = tail call i8* (...) @get_bookmark_path()
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %60
13: ; preds = %7
%14 = tail call i32* @open_create_if_not_exist(i8* %8)
%15 = icmp eq i32* %14, null
br i1 %15, label %58, label %16
16: ; preds = %13
%17 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 0
%18 = call i32 @mkstemp(i8* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %47, label %20
20: ; preds = %16
%21 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %41, label %23
23: ; preds = %20
%24 = load i32, i32* @LOCK_SH
%25 = call i64 @file_lock(i32* %14, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %23
%28 = call i32 @change_pageno(i32* %14, i32* %21, i8* %0, i32 %1)
%29 = call i32 @file_unlock(i32* %14)
%30 = call i32 @rewind(i32* %14)
%31 = call i32 @rewind(i32* %21)
%32 = load i32, i32* @LOCK_EX
%33 = call i64 @file_lock(i32* %14, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %27
%36 = call i32 @copy_file(i32* %21, i32* %14)
%37 = call i32 @file_unlock(i32* %14)
br label %38
38: ; preds = %27, %35, %23
%39 = call i64 @fclose(i32* %21)
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38, %20
%42 = phi i8* [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), %20 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), %38 ]
%43 = call i32 @perror(i8* %42)
br label %44
44: ; preds = %41, %38
%45 = call i64 @remove(i8* %17)
%46 = icmp eq i64 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44, %16
%48 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), %44 ]
%49 = call i32 @perror(i8* %48)
br label %50
50: ; preds = %47, %44
%51 = call i64 @fclose(i32* %14)
%52 = icmp eq i64 %51, 0
br i1 %52, label %58, label %53
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %8, i8* %56)
br label %58
58: ; preds = %50, %53, %13
%59 = call i32 @free(i8* %8)
br label %60
60: ; preds = %10, %58, %2
ret void
}