#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*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, char const *);
int readlink(char const *, char *, int);
size_t strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, size_t);
void test_readlink(const char *cwd, const char *label, const char *path) {
  char buf[PATH_MAX];
  int n;
  if ((n = readlink(path, buf, sizeof(buf) - 1)) >= 0) {
    const char *p;
    size_t len = strlen(cwd);
    buf[n] = '\0';
    p = buf;
    if (!strncmp(buf, cwd, len))
      p += len;
    printf("Result of readlink(\"%s\"): %s\n", label, p);
  } else
    perror("readlink");
}


x86 assembly (clang, -O0):
<test_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jl     1203 <test_readlink+0xb3>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,-0x40(%rbp)
movslq -0x2c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    %rdi,-0x38(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11ea <test_readlink+0x9a>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xe07(%rip),%rdi
call   1060 <printf@plt>
jmp    120f <test_readlink+0xbf>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_readlink>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e82(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%rbx
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rbx
mov    %rbx,%rsp
lea    -0x1(%rcx),%edx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
test   %eax,%eax
js     11e2 <test_readlink+0x92>
mov    %eax,%r12d
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %r12d,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <strncmp@plt>
xor    %edx,%edx
test   %eax,%eax
cmove  %r13,%rdx
add    %rbx,%rdx
lea    0xe35(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <perror@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [30 x i8] c"Result of readlink(\22%s\22): %s\0A\00"
@.str.1 = [9 x i8] c"readlink\00"
define void @test_readlink(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
%16 = load i8*, i8** %6
%17 = sub i64 %13, 1
%18 = trunc i64 %17 to i32
%19 = call i32 @readlink(i8* %16, i8* %15, i32 %18)
store i32 %19, i32* %9
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %3
%22 = load i8*, i8** %4
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %11
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %15, i64 %25
store i8 0, i8* %26
store i8* %15, i8** %10
%27 = load i8*, i8** %4
%28 = load i64, i64* %11
%29 = call i32 @strncmp(i8* %15, i8* %27, i64 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %21
%32 = load i64, i64* %11
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i64 %32
store i8* %34, i8** %10
br label %35
35: ; preds = %31, %21
%36 = load i8*, i8** %5
%37 = load i8*, i8** %10
%38 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %36, i8* %37)
br label %41
39: ; preds = %3
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [30 x i8] c"Result of readlink(\22%s\22): %s\0A\00"
@.str.1 = [9 x i8] c"readlink\00"
define void @test_readlink(i8* nocapture %0, i8* %1, i8* nocapture %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = add i32 %4, -1
%8 = call i32 @readlink(i8* %2, i8* %6, i32 %7)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %19
10: ; preds = %3
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = zext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %6, i64 %12
store i8 0, i8* %13
%14 = call i32 @strncmp(i8* %6, i8* %0, i64 %11)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i64 %11, i64 0
%17 = getelementptr i8, i8* %6, i64 %16
%18 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %17)
br label %21
19: ; preds = %3
%20 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %10
ret void
}