#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *const,
                           char const *);
int /*<<< orphan*/ perror(char *);
int readlinkat(int /*<<< orphan*/, char const *const, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *const);
scalar_t__ strncmp(char *, char const *const, int /*<<< orphan*/);
void test_readlinkat(const char *const cwd, const char *const label,
                     const char *const path) {
  char buf[512];
  const char *p;
  int n;
  if ((n = readlinkat(AT_FDCWD, path, buf, sizeof(buf) - 1)) >= 0) {
    buf[n] = 0;
    p = buf;
    if (strncmp(buf, cwd, strlen(cwd)) == 0)
      p += strlen(cwd);
    fprintf(stderr, "Result of readlinkat(\"%s\"): %s\n", label, p);
  } else
    perror("readlinkat");
}


x86 assembly (clang, -O0):
<test_readlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0x220(%rbp),%rdx
mov    $0x1ff,%ecx
call   1050 <readlinkat@plt>
mov    %eax,-0x22c(%rbp)
cmp    $0x0,%eax
jl     1236 <test_readlinkat+0xe6>
movslq -0x22c(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x240(%rbp),%rdi
mov    -0x238(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1211 <test_readlinkat+0xc1>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x228(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x228(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x228(%rbp),%rcx
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1242 <test_readlinkat+0xf2>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_readlinkat>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rbx
mov    %rdx,%rsi
mov    %rbx,%rdx
mov    $0x1ff,%ecx
call   1050 <readlinkat@plt>
test   %eax,%eax
js     11d8 <test_readlinkat+0x88>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <test_readlinkat+0x60>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rbx
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
jmp    11cb <test_readlinkat+0x7b>

LLVM IR (-O0):
@AT_FDCWD = global i32 0
@stderr = 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 [512 x i8]
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i32, i32* @AT_FDCWD
%11 = load i8*, i8** %6
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%13 = call i32 @readlinkat(i32 %10, i8* %11, i8* %12, i32 511)
store i32 %13, i32* %9
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %37
15: ; preds = %3
%16 = load i32, i32* %9
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
store i8* %19, i8** %8
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%21 = load i8*, i8** %4
%22 = load i8*, i8** %4
%23 = call i32 @strlen(i8* %22)
%24 = call i64 @strncmp(i8* %20, i8* %21, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %15
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
%29 = load i8*, i8** %8
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %8
br label %32
32: ; preds = %26, %15
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %5
%35 = load i8*, i8** %8
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %35)
br label %39
37: ; preds = %3
%38 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
ret void
}

LLVM IR (-O2):
@AT_FDCWD = global i32 0
@stderr = 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 [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @AT_FDCWD
%7 = call i32 @readlinkat(i32 %6, i8* %2, i8* %5, i32 511)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %23
9: ; preds = %3
%10 = zext i32 %7 to i64
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 @strlen(i8* %0)
%13 = call i64 @strncmp(i8* %5, i8* %0, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = call i32 @strlen(i8* %0)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %17
br label %19
19: ; preds = %15, %9
%20 = phi i8* [ %18, %15 ], [ %5, %9 ]
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %20)
br label %25
23: ; preds = %3
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
ret void
}