#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 /*<<< orphan*/ AT_FDCWD;
int PATH_MAX;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, char const *);
int readlinkat(int /*<<< orphan*/, char const *, char *, int);
size_t strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, size_t);
void test_readlinkat(const char *cwd, const char *label, const char *path) {
  char buf[PATH_MAX];
  int n;
  if ((n = readlinkat(AT_FDCWD, 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 readlinkat(\"%s\"): %s\n", label, p);
  } else
    perror("readlinkat");
}


x86 assembly (clang, -O0):
<test_readlinkat>:
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,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e39(%rip),%rcx
mov    (%rcx),%edi
mov    -0x18(%rbp),%rsi
sub    $0x1,%rax
mov    %eax,%ecx
call   1060 <readlinkat@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jl     120c <test_readlinkat+0xbc>
mov    -0x8(%rbp),%rdi
call   1040 <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    11f3 <test_readlinkat+0xa3>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
jmp    1218 <test_readlinkat+0xc8>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_readlinkat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    0x2e48(%rip),%rcx
mov    (%rcx),%edi
lea    -0x1(%rax),%ecx
mov    %rdx,%rsi
mov    %rbx,%rdx
call   1060 <readlinkat@plt>
test   %eax,%eax
js     11e8 <test_readlinkat+0x98>
mov    %eax,%r12d
mov    %r15,%rdi
call   1040 <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    0xe2f(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
jmp    11d9 <test_readlinkat+0x89>

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

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