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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close_log();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ log_error(char *, int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t strlen(char *);
char *working_dir(void) {
  struct passwd *passwd_entry;
  size_t home_dir_length, suffix_length, working_dir_length;
  uid_t user_id;
  char *working_dir;
  user_id = getuid();
  passwd_entry = getpwuid(user_id);
  home_dir_length = strlen(passwd_entry->pw_dir);
  suffix_length = strlen("/.tomatosaver/");
  working_dir_length = home_dir_length + suffix_length;
  working_dir = (char *)malloc(working_dir_length + 1);
  if (working_dir == NULL) {
    log_error("Unable to allocate enough memory to hold "
              "path to working directory: %s\n",
              strerror(errno));
    close_log();
    exit(EXIT_FAILURE);
  }
  memset(working_dir, 0, working_dir_length + 1);
  strcpy(working_dir, passwd_entry->pw_dir);
  strcat(working_dir, "/.tomatosaver/");
  return working_dir;
}


x86 assembly (clang, -O0):
<working_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe1b(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1247 <working_dir+0x97>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %eax,%esi
lea    0xde2(%rip),%rdi
call   1040 <log_error@plt>
mov    $0x0,%al
call   1080 <close_log@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xd8b(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<working_dir>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,%r14
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%r15
add    $0xf,%r15
mov    %r15,%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1215 <working_dir+0x65>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1090 <memset@plt>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xdfc(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xde5(%rip),%rdi
mov    %eax,%esi
call   1040 <log_error@plt>
xor    %eax,%eax
call   1080 <close_log@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [15 x i8] c"/.tomatosaver/\00"
@.str.1 = [72 x i8] c"Unable to allocate enough memory to hold path to working directory: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @working_dir() {
%1 = alloca %struct.passwd*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = call i32 (...) @getuid()
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = call %struct.passwd* @getpwuid(i32 %8)
store %struct.passwd* %9, %struct.passwd** %1
%10 = load %struct.passwd*, %struct.passwd** %1
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 0
%12 = load i8*, i8** %11
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %2
%14 = call i64 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i64 %14, i64* %3
%15 = load i64, i64* %2
%16 = load i64, i64* %3
%17 = add i64 %15, %16
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = add i64 %18, 1
%20 = call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %31
24: ; preds = %0
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @log_error(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = call i32 (...) @close_log()
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %0
%32 = load i8*, i8** %6
%33 = load i64, i64* %4
%34 = add i64 %33, 1
%35 = call i32 @memset(i8* %32, i32 0, i64 %34)
%36 = load i8*, i8** %6
%37 = load %struct.passwd*, %struct.passwd** %1
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strcpy(i8* %36, i8* %39)
%41 = load i8*, i8** %6
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%43 = load i8*, i8** %6
ret i8* %43
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [15 x i8] c"/.tomatosaver/\00"
@.str.1 = [72 x i8] c"Unable to allocate enough memory to hold path to working directory: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @working_dir() {
%1 = tail call i32 (...) @getuid()
%2 = tail call %struct.passwd* @getpwuid(i32 %1)
%3 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%4 = load i8*, i8** %3
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = add i64 %5, 15
%7 = tail call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %17
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @log_error(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @close_log()
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = tail call i32 @memset(i8* %8, i32 0, i64 %6)
%19 = load i8*, i8** %3
%20 = tail call i32 @strcpy(i8* %8, i8* %19)
%21 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
ret i8* %8
}