#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 */
struct passwd {
  char *pw_dir;
};

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *get_bookmark_path() {
  char *home = getenv("HOME");
  if (!home) {
    fputs("env HOME not set\n", stderr);
    struct passwd *passwd;
    errno = 0;
    if ((passwd = getpwuid(getuid())) == NULL) {
      perror("can't get password record; getpwuid");
      return NULL;
    }
    if ((home = passwd->pw_dir) == NULL) {
      fputs("home directory not set in password file\n", stderr);
      return NULL;
    }
  }
  size_t size = strlen(home) + strlen(".mupdf_bookmarks") + 2;
  char *bm_file = malloc(size * sizeof(*bm_file));
  if (bm_file == NULL) {
    perror("malloc");
    return NULL;
  }
  snprintf(bm_file, size, "%s/%s", home, ".mupdf_bookmarks");
  return bm_file;
}


x86 assembly (clang, -O0):
<get_bookmark_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1237 <get_bookmark_path+0xb7>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1080 <fputs@plt>
mov    0x2e19(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11fb <get_bookmark_path+0x7b>
lea    0xe2e(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <get_bookmark_path+0x13b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1232 <get_bookmark_path+0xb2>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
call   1080 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <get_bookmark_path+0x13b>
jmp    1237 <get_bookmark_path+0xb7>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
lea    0xe1a(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1294 <get_bookmark_path+0x114>
lea    0xdf3(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <get_bookmark_path+0x13b>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xdd5(%rip),%rdx
lea    0xdb6(%rip),%r8
call   1070 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bookmark_path>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d7 <get_bookmark_path+0x57>
mov    0x2e40(%rip),%rbx
mov    (%rbx),%esi
lea    0xe5c(%rip),%rdi
call   1080 <fputs@plt>
mov    0x2e23(%rip),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1245 <get_bookmark_path+0xc5>
mov    (%rax),%r14
test   %r14,%r14
je     122a <get_bookmark_path+0xaa>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0xe7c(%rip),%rdi
call   1050 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     123c <get_bookmark_path+0xbc>
mov    %rax,%rbx
lea    0xe70(%rip),%rdx
lea    0xe51(%rip),%r8
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%rbx),%esi
lea    0xe08(%rip),%rdi
call   1080 <fputs@plt>
xor    %ebx,%ebx
jmp    1221 <get_bookmark_path+0xa1>
lea    0xe32(%rip),%rdi
jmp    124c <get_bookmark_path+0xcc>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
xor    %ebx,%ebx
jmp    1221 <get_bookmark_path+0xa1>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [18 x i8] c"env HOME not set\0A\00"
@stderr = global i32 0
@errno = global i64 0
@.str.2 = [36 x i8] c"can't get password record; getpwuid\00"
@.str.3 = [41 x i8] c"home directory not set in password file\0A\00"
@.str.4 = [17 x i8] c".mupdf_bookmarks\00"
@.str.5 = [7 x i8] c"malloc\00"
@.str.6 = [6 x i8] c"%s/%s\00"
define i8* @get_bookmark_path() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca %struct.passwd*
%4 = alloca i64
%5 = alloca i8*
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %26, label %9
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i64 0, i64* @errno
%12 = call i32 (...) @getuid()
%13 = call %struct.passwd* @getpwuid(i32 %12)
store %struct.passwd* %13, %struct.passwd** %3
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %46
17: ; preds = %9
%18 = load %struct.passwd*, %struct.passwd** %3
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %2
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %23)
store i8* null, i8** %1
br label %46
25: ; preds = %17
br label %26
26: ; preds = %25, %0
%27 = load i8*, i8** %2
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%30 = add nsw i32 %28, %29
%31 = add nsw i32 %30, 2
%32 = sext i32 %31 to i64
store i64 %32, i64* %4
%33 = load i64, i64* %4
%34 = mul i64 %33, 1
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %5
%36 = load i8*, i8** %5
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %26
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %1
br label %46
40: ; preds = %26
%41 = load i8*, i8** %5
%42 = load i64, i64* %4
%43 = load i8*, i8** %2
%44 = call i32 @snprintf(i8* %41, i64 %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%45 = load i8*, i8** %5
store i8* %45, i8** %1
br label %46
46: ; preds = %40, %38, %22, %15
%47 = load i8*, i8** %1
ret i8* %47
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [18 x i8] c"env HOME not set\0A\00"
@stderr = global i32 0
@errno = global i64 0
@.str.2 = [36 x i8] c"can't get password record; getpwuid\00"
@.str.3 = [41 x i8] c"home directory not set in password file\0A\00"
@.str.4 = [17 x i8] c".mupdf_bookmarks\00"
@.str.5 = [7 x i8] c"malloc\00"
@.str.6 = [6 x i8] c"%s/%s\00"
define i8* @get_bookmark_path() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %18
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %4)
store i64 0, i64* @errno
%6 = tail call i32 (...) @getuid()
%7 = tail call %struct.passwd* @getpwuid(i32 %6)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %31
11: ; preds = %3
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %31
18: ; preds = %11, %0
%19 = phi i8* [ %1, %0 ], [ %13, %11 ]
%20 = tail call i32 @strlen(i8* %19)
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%22 = add i32 %20, 2
%23 = add i32 %22, %21
%24 = sext i32 %23 to i64
%25 = tail call i8* @malloc(i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %31
29: ; preds = %18
%30 = tail call i32 @snprintf(i8* %25, i64 %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %9, %15, %27, %29
%32 = phi i8* [ null, %27 ], [ %25, %29 ], [ null, %15 ], [ null, %9 ]
ret i8* %32
}