#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 size_t ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int PATH_MAX;
size_t readlink(char const *, char *, int);
char *strdup(char *);
char *readlink_internal(const char *filename) {
  char buffer[PATH_MAX];
  char *p_link = NULL;
  ssize_t len = 0;
  len = readlink(filename, buffer, sizeof(buffer));
  if (len <= 0 || len == (ssize_t)sizeof(buffer)) {
    return NULL;
  }
  buffer[len] = '\0';
  p_link = strdup(buffer);
  if (p_link == NULL) {
    return NULL;
  } else {
    return p_link;
  }
}


x86 assembly (clang, -O0):
<readlink_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
call   1030 <readlink@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    1194 <readlink_internal+0x74>
mov    -0x48(%rbp),%rax
cmp    %rax,-0x30(%rbp)
jne    11a8 <readlink_internal+0x88>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11eb <readlink_internal+0xcb>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1040 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11dc <readlink_internal+0xbc>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11eb <readlink_internal+0xcb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink_internal>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2ec2(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <readlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1168 <readlink_internal+0x48>
cmp    %r14,%rcx
je     1168 <readlink_internal+0x48>
movb   $0x0,(%rbx,%rcx,1)
mov    %rbx,%rdi
call   1040 <strdup@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
define i8* @readlink_internal(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
store i8* null, i8** %6
store i64 0, i64* %7
%13 = load i8*, i8** %3
%14 = trunc i64 %10 to i32
%15 = call i64 @readlink(i8* %13, i8* %12, i32 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp ule i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %1
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, %10
br i1 %20, label %21, label %22
21: ; preds = %18, %1
store i8* null, i8** %2
store i32 1, i32* %8
br label %31
22: ; preds = %18
%23 = load i64, i64* %7
%24 = getelementptr inbounds i8, i8* %12, i64 %23
store i8 0, i8* %24
%25 = call i8* @strdup(i8* %12)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %22
store i8* null, i8** %2
store i32 1, i32* %8
br label %31
29: ; preds = %22
%30 = load i8*, i8** %6
store i8* %30, i8** %2
store i32 1, i32* %8
br label %31
31: ; preds = %29, %28, %21
%32 = load i8*, i8** %4
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
define noalias i8* @readlink_internal(i8* nocapture %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i64 @readlink(i8* %0, i8* %4, i32 %2)
%6 = icmp eq i64 %5, 0
%7 = icmp eq i64 %5, %3
%8 = or i1 %6, %7
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %4, i64 %5
store i8 0, i8* %10
%11 = call i8* @strdup(i8* %4)
br label %12
12: ; preds = %9, %1
%13 = phi i8* [ null, %1 ], [ %11, %9 ]
ret i8* %13
}