#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*/ fprintf(int /*<<< orphan*/, char *, char const *const,
                           char const *);
int /*<<< orphan*/ perror(char *);
int readlink(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_readlink(const char *const cwd, const char *const label,
                   const char *const path) {
  char buf[512];
  const char *p;
  int n;
  if ((n = readlink(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 readlink(\"%s\"): %s\n", label, p);
  } else
    perror("readlink");
}


x86 assembly (clang, -O0):
<test_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x1ff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x22c(%rbp)
cmp    $0x0,%eax
jl     122d <test_readlink+0xdd>
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   1050 <strlen@plt>
mov    -0x240(%rbp),%rdi
mov    -0x238(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1208 <test_readlink+0xb8>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x228(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x228(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x228(%rbp),%rcx
lea    0xddd(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1239 <test_readlink+0xe9>
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_readlink>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rbx
mov    %rdx,%rdi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     11cf <test_readlink+0x7f>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a7 <test_readlink+0x57>
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rbx
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe48(%rip),%rdi
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <perror@plt>

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

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