#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*/ path;
typedef int /*<<< orphan*/ cwd;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ getcwd(char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ test_readlink(char *, char *, char *);
int /*<<< orphan*/ test_readlinkat(char *, char *, char *);
int main(void) {
  char cwd[PATH_MAX];
  char path[PATH_MAX];
  cwd[0] = '\0';
  if (!getcwd(cwd, sizeof(cwd) - 1))
    perror("getcwd");
  strcat(cwd, "/");
  snprintf(path, sizeof(path), "/proc/%ld/path/a.out", (long)getpid());
  test_readlink(cwd, "/proc/self/path/a.out", "/proc/self/path/a.out");
  test_readlink(cwd, "/proc/<pid>/path/a.out", path);
  test_readlinkat(cwd, "/proc/self/path/a.out", "/proc/self/path/a.out");
  test_readlinkat(cwd, "/proc/<pid>/path/a.out", path);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rdi)
sub    $0x1,%rax
mov    %eax,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%eax
jne    11f0 <main+0x80>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
mov    -0x38(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x3c(%rbp),%esi
mov    -0x28(%rbp),%rdi
mov    %rax,%rcx
lea    0xdea(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xdef(%rip),%rdx
mov    %rdx,%rsi
call   1090 <test_readlink@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1090 <test_readlink@plt>
mov    -0x38(%rbp),%rdi
lea    0xdc8(%rip),%rdx
mov    %rdx,%rsi
call   1060 <test_readlinkat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdc7(%rip),%rsi
call   1060 <test_readlinkat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
movb   $0x0,(%rbx)
lea    -0x1(%r15),%esi
mov    %rbx,%rdi
call   1050 <getcwd@plt>
test   %eax,%eax
je     1239 <main+0xc9>
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe33(%rip),%rdx
mov    %r14,%rdi
mov    %r15d,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
lea    0xe33(%rip),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r15,%rdx
call   1090 <test_readlink@plt>
lea    0xe34(%rip),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1090 <test_readlink@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r15,%rdx
call   1060 <test_readlinkat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <test_readlinkat@plt>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
call   1070 <perror@plt>
jmp    11b9 <main+0x49>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [7 x i8] c"getcwd\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [21 x i8] c"/proc/%ld/path/a.out\00"
@.str.3 = [22 x i8] c"/proc/self/path/a.out\00"
@.str.4 = [23 x i8] c"/proc/<pid>/path/a.out\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %2
%8 = alloca i8, i64 %6
store i64 %6, i64* %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
store i64 %10, i64* %4
%12 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %12
%13 = sub i64 %6, 1
%14 = trunc i64 %13 to i32
%15 = call i32 @getcwd(i8* %8, i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = trunc i64 %10 to i32
%22 = call i64 (...) @getpid()
%23 = call i32 @snprintf(i8* %11, i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %22)
%24 = call i32 @test_readlink(i8* %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @test_readlink(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%26 = call i32 @test_readlinkat(i8* %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @test_readlinkat(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %11)
store i32 0, i32* %1
%28 = load i8*, i8** %2
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [7 x i8] c"getcwd\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [21 x i8] c"/proc/%ld/path/a.out\00"
@.str.3 = [22 x i8] c"/proc/self/path/a.out\00"
@.str.4 = [23 x i8] c"/proc/<pid>/path/a.out\00"
define i32 @main() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = alloca i8, i64 %2
store i8 0, i8* %3
%5 = add i32 %1, -1
%6 = call i32 @getcwd(i8* %3, i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i64 (...) @getpid()
%13 = call i32 @snprintf(i8* %4, i32 %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%14 = call i32 @test_readlink(i8* %3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @test_readlink(i8* %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%16 = call i32 @test_readlinkat(i8* %3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @test_readlinkat(i8* %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %4)
ret i32 0
}