#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*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char const *);
char *get_proc_cwd(const char *pid) {
  char *cwd_path = NULL;
  char buf[2048];
  cwd_path = malloc(strlen("/proc") + strlen(pid) + 6);
  if (!cwd_path) {
    perror("malloc");
    return NULL;
  }
  strcpy(cwd_path, "/proc");
  strcat(cwd_path, "/");
  strcat(cwd_path, pid);
  strcat(cwd_path, "/cwd");
  readlink(cwd_path, buf, 2047);
  free(cwd_path);
  return strdup(buf);
}


x86 assembly (clang, -O0):
<get_proc_cwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe62(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x828(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x828(%rbp),%rdi
add    %rax,%rdi
add    $0x6,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ee <get_proc_cwd+0x6e>
lea    0xe2a(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1259 <get_proc_cwd+0xd9>
mov    -0x18(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    -0x820(%rbp),%rsi
mov    $0x7ff,%edx
call   1050 <readlink@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    -0x820(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_cwd>:
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r14
call   1060 <strlen@plt>
lea    0xb(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1209 <get_proc_cwd+0x89>
mov    %rax,%rbx
lea    0xe56(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
lea    0xe54(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x7ff,%edx
call   1050 <readlink@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   10a0 <strdup@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    11fe <get_proc_cwd+0x7e>

LLVM IR (-O0):
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [5 x i8] c"/cwd\00"
define i8* @get_proc_cwd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [2048 x i8]
store i8* %0, i8** %3
store i8* null, i8** %4
%6 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %6, %8
%10 = add nsw i64 %9, 6
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %33
16: ; preds = %1
%17 = load i8*, i8** %4
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i32 @strcat(i8* %21, i8* %22)
%24 = load i8*, i8** %4
%25 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = load i8*, i8** %4
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%28 = call i32 @readlink(i8* %26, i8* %27, i32 2047)
%29 = load i8*, i8** %4
%30 = call i32 @free(i8* %29)
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %2
br label %33
33: ; preds = %16, %14
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [5 x i8] c"/cwd\00"
define noalias i8* @get_proc_cwd(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add nsw i64 %4, 11
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %18
10: ; preds = %1
%11 = tail call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @strcat(i8* %6, i8* %0)
%14 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @readlink(i8* %6, i8* %3, i32 2047)
%16 = tail call i32 @free(i8* %6)
%17 = call i8* @strdup(i8* %3)
br label %18
18: ; preds = %10, %8
%19 = phi i8* [ %17, %10 ], [ null, %8 ]
ret i8* %19
}