#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 */
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, int);
size_t strlen(char const *);
char *get_workdir(void) {
  struct passwd *pw = getpwuid(getuid());
  const char *homedir = pw->pw_dir;
  size_t workdir_len = strlen(homedir) + strlen("/.lql");
  char *workdir = malloc(workdir_len + 1);
  memcpy(workdir, homedir, strlen(homedir));
  memcpy(workdir + strlen(homedir), "/.lql", strlen("/.lql"));
  memcpy(workdir + strlen(homedir) + strlen("/.lql"), "\0", 1);
  return workdir;
}


x86 assembly (clang, -O0):
<get_workdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x5,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
lea    0xe22(%rip),%rsi
mov    $0x5,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x5,%rdi
lea    0xdfb(%rip),%rsi
mov    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_workdir>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x6(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r14,1),%rdi
lea    0xe62(%rip),%rsi
mov    $0x5,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x5,%rdi
lea    0xe47(%rip),%rsi
mov    $0x1,%edx
call   1060 <memcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [6 x i8] c"/.lql\00"
@.str.1 = [2 x i8] zeroinitializer
define i8* @get_workdir() {
%1 = alloca %struct.passwd*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = call i32 (...) @getuid()
%6 = call %struct.passwd* @getpwuid(i32 %5)
store %struct.passwd* %6, %struct.passwd** %1
%7 = load %struct.passwd*, %struct.passwd** %1
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i32 0, i32 0
%9 = load i8*, i8** %8
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = call i64 @strlen(i8* %10)
%12 = add i64 %11, 5
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = add i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = load i8*, i8** %2
%18 = load i8*, i8** %2
%19 = call i64 @strlen(i8* %18)
%20 = trunc i64 %19 to i32
%21 = call i32 @memcpy(i8* %16, i8* %17, i32 %20)
%22 = load i8*, i8** %4
%23 = load i8*, i8** %2
%24 = call i64 @strlen(i8* %23)
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = call i32 @memcpy(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%27 = load i8*, i8** %4
%28 = load i8*, i8** %2
%29 = call i64 @strlen(i8* %28)
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = getelementptr inbounds i8, i8* %30, i64 5
%32 = call i32 @memcpy(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%33 = load i8*, i8** %4
ret i8* %33
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [6 x i8] c"/.lql\00"
@.str.1 = [2 x i8] zeroinitializer
define i8* @get_workdir() {
%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, 6
%7 = tail call i8* @malloc(i64 %6)
%8 = trunc i64 %5 to i32
%9 = tail call i32 @memcpy(i8* %7, i8* %4, i32 %8)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%11 = getelementptr inbounds i8, i8* %7, i64 %10
%12 = tail call i32 @memcpy(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%14 = getelementptr inbounds i8, i8* %7, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 5
%16 = tail call i32 @memcpy(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
ret i8* %7
}