#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*/ 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;
  uid_t user_id;
  size_t home_dir_length, suffix_length, working_dir_length;
  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 name of "
              "working directory: %s\n",
              strerror(errno));
    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,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe2b(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1230 <working_dir+0x90>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xdf2(%rip),%rdi
call   1040 <log_error@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xda2(%rip),%rsi
call   10a0 <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   1090 <malloc@plt>
test   %rax,%rax
je     1205 <working_dir+0x65>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <memset@plt>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdf5(%rip),%rdi
mov    %eax,%esi
call   1040 <log_error@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10b0 <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 name of working directory: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @working_dir() {
%1 = alloca %struct.passwd*
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = call i32 (...) @getuid()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%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* %3
%14 = call i64 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i64 %14, i64* %4
%15 = load i64, i64* %3
%16 = load i64, i64* %4
%17 = add i64 %15, %16
store i64 %17, i64* %5
%18 = load i64, i64* %5
%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 %30
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 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %0
%31 = load i8*, i8** %6
%32 = load i64, i64* %5
%33 = add i64 %32, 1
%34 = call i32 @memset(i8* %31, i32 0, i64 %33)
%35 = load i8*, i8** %6
%36 = load %struct.passwd*, %struct.passwd** %1
%37 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @strcpy(i8* %35, i8* %38)
%40 = load i8*, i8** %6
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%42 = load i8*, i8** %6
ret i8* %42
}

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 name of 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 %16
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 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = tail call i32 @memset(i8* %8, i32 0, i64 %6)
%18 = load i8*, i8** %3
%19 = tail call i32 @strcpy(i8* %8, i8* %18)
%20 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
ret i8* %8
}