#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 */

/* Variables and functions */
int filename_length_without_suffix(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void generate_bookmark_name(char *name, char *bookmark_fmt, char *in_fn,
                            int page) {
  bool meta = 0;
  int len;
  while (*bookmark_fmt) {
    if (meta) {
      meta = 0;
      switch (*bookmark_fmt) {
      case '%':
        *(name++) = '%';
        break;
      case 'F':
        len = filename_length_without_suffix(in_fn);
        strncpy(name, in_fn, len);
        name += len;
        break;
      case 'p':
        sprintf(name, "%d", page);
        name += strlen(name);
        break;
      default:
        break;
      }
    } else
      switch (*bookmark_fmt) {
      case '%':
        meta = 1;
        break;
      default:
        *(name++) = *bookmark_fmt;
      }
    bookmark_fmt++;
  }
  *name = '\0';
}


x86 assembly (clang, -O0):
<generate_bookmark_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1286 <generate_bookmark_name+0x146>
cmpl   $0x0,-0x20(%rbp)
je     123a <generate_bookmark_name+0xfa>
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x25,%eax
je     11b6 <generate_bookmark_name+0x76>
jmp    1194 <generate_bookmark_name+0x54>
mov    -0x28(%rbp),%eax
sub    $0x46,%eax
je     11cd <generate_bookmark_name+0x8d>
jmp    11a5 <generate_bookmark_name+0x65>
mov    -0x28(%rbp),%eax
sub    $0x70,%eax
je     11ff <generate_bookmark_name+0xbf>
jmp    1230 <generate_bookmark_name+0xf0>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x25,(%rax)
jmp    1235 <generate_bookmark_name+0xf5>
mov    -0x18(%rbp),%rdi
call   1050 <filename_length_without_suffix@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1235 <generate_bookmark_name+0xf5>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xdf3(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1235 <generate_bookmark_name+0xf5>
jmp    1235 <generate_bookmark_name+0xf5>
jmp    1275 <generate_bookmark_name+0x135>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
sub    $0x25,%al
jne    1259 <generate_bookmark_name+0x119>
jmp    124d <generate_bookmark_name+0x10d>
movl   $0x1,-0x20(%rbp)
jmp    1270 <generate_bookmark_name+0x130>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1275 <generate_bookmark_name+0x135>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    115e <generate_bookmark_name+0x1e>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_bookmark_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rsi),%al
test   %al,%al
je     11ec <generate_bookmark_name+0xac>
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
add    $0x1,%rbp
xor    %r12d,%r12d
jmp    1194 <generate_bookmark_name+0x54>
mov    %r15,%rdi
call   1050 <filename_length_without_suffix@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
movslq %r13d,%rax
add    %rax,%rbx
movzbl 0x0(%rbp),%eax
add    $0x1,%rbp
test   %al,%al
je     11ec <generate_bookmark_name+0xac>
test   %r12d,%r12d
jne    11b0 <generate_bookmark_name+0x70>
mov    $0x1,%r12d
cmp    $0x25,%al
je     1188 <generate_bookmark_name+0x48>
mov    %al,(%rbx)
add    $0x1,%rbx
xor    %r12d,%r12d
jmp    1188 <generate_bookmark_name+0x48>
xchg   %ax,%ax
movsbl %al,%eax
xor    %r12d,%r12d
cmp    $0x70,%eax
je     11ce <generate_bookmark_name+0x8e>
cmp    $0x46,%eax
je     116a <generate_bookmark_name+0x2a>
cmp    $0x25,%eax
jne    1188 <generate_bookmark_name+0x48>
movb   $0x25,(%rbx)
add    $0x1,%rbx
jmp    1188 <generate_bookmark_name+0x48>
mov    %rbx,%rdi
lea    0xe28(%rip),%rsi
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
jmp    1185 <generate_bookmark_name+0x45>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define void @generate_bookmark_name(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %58, %4
%12 = load i8*, i8** %6
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %61
15: ; preds = %11
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %15
store i32 0, i32* %9
%19 = load i8*, i8** %6
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
switch i32 %21, label %45 [
i32 37, label %22
i32 70, label %25
i32 112, label %36
]
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
store i8 37, i8* %23
br label %46
25: ; preds = %18
%26 = load i8*, i8** %7
%27 = call i32 @filename_length_without_suffix(i8* %26)
store i32 %27, i32* %10
%28 = load i8*, i8** %5
%29 = load i8*, i8** %7
%30 = load i32, i32* %10
%31 = call i32 @strncpy(i8* %28, i8* %29, i32 %30)
%32 = load i32, i32* %10
%33 = load i8*, i8** %5
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %5
br label %46
36: ; preds = %18
%37 = load i8*, i8** %5
%38 = load i32, i32* %8
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %5
%41 = call i32 @strlen(i8* %40)
%42 = load i8*, i8** %5
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8* %44, i8** %5
br label %46
45: ; preds = %18
br label %46
46: ; preds = %45, %36, %25, %22
br label %58
47: ; preds = %15
%48 = load i8*, i8** %6
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
switch i32 %50, label %52 [
i32 37, label %51
]
51: ; preds = %47
store i32 1, i32* %9
br label %57
52: ; preds = %47
%53 = load i8*, i8** %6
%54 = load i8, i8* %53
%55 = load i8*, i8** %5
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %5
store i8 %54, i8* %55
br label %57
57: ; preds = %52, %51
br label %58
58: ; preds = %57, %46
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %6
br label %11
61: ; preds = %11
%62 = load i8*, i8** %5
store i8 0, i8* %62
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define void @generate_bookmark_name(i8* %0, i8* nocapture %1, i8* %2, i32 %3) {
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %37, label %7
7: ; preds = %4, %31
%8 = phi i8 [ %35, %31 ], [ %5, %4 ]
%9 = phi i8* [ %33, %31 ], [ %0, %4 ]
%10 = phi i32 [ %32, %31 ], [ 0, %4 ]
%11 = phi i8* [ %34, %31 ], [ %1, %4 ]
%12 = icmp eq i32 %10, 0
br i1 %12, label %27, label %13
13: ; preds = %7
%14 = sext i8 %8 to i32
switch i32 %14, label %31 [
i32 37, label %15
i32 70, label %17
i32 112, label %22
]
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %9, i64 1
store i8 37, i8* %9
br label %31
17: ; preds = %13
%18 = tail call i32 @filename_length_without_suffix(i8* %2)
%19 = tail call i32 @strncpy(i8* %9, i8* %2, i32 %18)
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %9, i64 %20
br label %31
22: ; preds = %13
%23 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3)
%24 = tail call i32 @strlen(i8* %9)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %9, i64 %25
br label %31
27: ; preds = %7
%28 = icmp eq i8 %8, 37
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %8, i8* %9
br label %31
31: ; preds = %27, %29, %15, %17, %22, %13
%32 = phi i32 [ 0, %13 ], [ 0, %22 ], [ 0, %17 ], [ 0, %15 ], [ 0, %29 ], [ 1, %27 ]
%33 = phi i8* [ %9, %13 ], [ %26, %22 ], [ %21, %17 ], [ %16, %15 ], [ %30, %29 ], [ %9, %27 ]
%34 = getelementptr inbounds i8, i8* %11, i64 1
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %37, label %7
37: ; preds = %31, %4
%38 = phi i8* [ %0, %4 ], [ %33, %31 ]
store i8 0, i8* %38
ret void
}