#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 /*<<< orphan*/ getcwd(char *, int);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strlen(char const *);
const char *expand_path(const char *relpath) {
  char foo[1024], *newpath;
  getcwd(foo, 1024 - 1);
  newpath = (char *)malloc(strlen(foo) + strlen(relpath) + 2);
  strcat(newpath, foo);
  strcat(newpath, "/");
  strcat(newpath, relpath);
  return newpath;
}


x86 assembly (clang, -O0):
<expand_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3ff,%esi
call   1040 <getcwd@plt>
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x420(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x420(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x418(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x418(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x418(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_path>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r15
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x3ff,%esi
call   1040 <getcwd@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @expand_path(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i8*
store i8* %0, i8** %2
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @getcwd(i8* %5, i32 1023)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = call i64 @strlen(i8* %7)
%9 = load i8*, i8** %2
%10 = call i64 @strlen(i8* %9)
%11 = add nsw i64 %8, %10
%12 = add nsw i64 %11, 2
%13 = call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i32 @strcat(i8* %15, i8* %16)
%18 = load i8*, i8** %4
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %4
%21 = load i8*, i8** %2
%22 = call i32 @strcat(i8* %20, i8* %21)
%23 = load i8*, i8** %4
ret i8* %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @expand_path(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @getcwd(i8* %3, i32 1023)
%5 = call i64 @strlen(i8* %3)
%6 = call i64 @strlen(i8* dereferenceable(1) %0)
%7 = add i64 %5, 2
%8 = add i64 %7, %6
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
%11 = call i32 @strcat(i8* %10, i8* %3)
%12 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @strcat(i8* %10, i8* %0)
ret i8* %10
}