#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*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *appendCwd(char *path) {
  if (*path == '/') {
    return strdup(path);
  }
  char *cwd = getcwd(NULL, 0);
  fprintf(stderr, "pwd = %s\n", cwd);
  int lc = strlen(cwd);
  char *absPath = malloc(lc + strlen(path) + 2);
  strcpy(absPath, cwd);
  char *w = absPath + lc;
  free(cwd);
  *w++ = '/';
  strcpy(w, path);
  return absPath;
}


x86 assembly (clang, -O0):
<appendCwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    119e <appendCwd+0x2e>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    123e <appendCwd+0xce>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe3f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendCwd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
cmpb   $0x2f,(%rdi)
jne    1193 <appendCwd+0x23>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,%r14
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
movslq %r15d,%r15
mov    %r14,%rdi
call   1030 <free@plt>
lea    (%rbx,%r15,1),%rdi
add    $0x1,%rdi
movb   $0x2f,(%rbx,%r15,1)
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [10 x i8] c"pwd = %s\0A\00"
define i8* @appendCwd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 47
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %2
br label %43
15: ; preds = %1
%16 = call i8* @getcwd(i32* null, i32 0)
store i8* %16, i8** %4
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %4
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = load i8*, i8** %3
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %22, %24
%26 = add nsw i32 %25, 2
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i8*, i8** %4
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %6
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8* %34, i8** %7
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %7
store i8 47, i8* %37
%39 = load i8*, i8** %7
%40 = load i8*, i8** %3
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %43
43: ; preds = %15, %12
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [10 x i8] c"pwd = %s\0A\00"
define i8* @appendCwd(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 47
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i8* @strdup(i8* %0)
br label %21
6: ; preds = %1
%7 = tail call i8* @getcwd(i32* null, i32 0)
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %7)
%10 = tail call i32 @strlen(i8* %7)
%11 = tail call i32 @strlen(i8* %0)
%12 = add i32 %10, 2
%13 = add i32 %12, %11
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 @strcpy(i8* %14, i8* %7)
%16 = sext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = tail call i32 @free(i8* %7)
%19 = getelementptr inbounds i8, i8* %17, i64 1
store i8 47, i8* %17
%20 = tail call i32 @strcpy(i8* %19, i8* %0)
br label %21
21: ; preds = %6, %4
%22 = phi i8* [ %5, %4 ], [ %14, %6 ]
ret i8* %22
}