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

/* Variables and functions */
int PATH_MAX;
size_t readlink(char const *, char *, int);
scalar_t__ strdup(char *);
const char *read_link(const char *filename) {
  char buf[PATH_MAX + 1];
  size_t r = readlink(filename, buf, PATH_MAX + 1);
  if (r == (size_t)-1) {
    return NULL;
  }
  buf[r] = '\0';
  return (const char *)strdup(buf);
}


x86 assembly (clang, -O0):
<read_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
add    $0x1,%edx
call   1030 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1193 <read_link+0x73>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11af <read_link+0x8f>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1040 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_link>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2ec3(%rip),%rax
mov    (%rax),%edx
add    $0x1,%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1030 <readlink@plt>
cmp    $0xffffffffffffffff,%rax
je     115f <read_link+0x3f>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1040 <strdup@plt>
jmp    1161 <read_link+0x41>
xor    %eax,%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
define i8* @read_link(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = add nsw i32 %8, 1
%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
%13 = load i8*, i8** %3
%14 = load i32, i32* @PATH_MAX
%15 = add nsw i32 %14, 1
%16 = call i64 @readlink(i8* %13, i8* %12, i32 %15)
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %7
br label %25
20: ; preds = %1
%21 = load i64, i64* %6
%22 = getelementptr inbounds i8, i8* %12, i64 %21
store i8 0, i8* %22
%23 = call i64 @strdup(i8* %12)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %2
store i32 1, i32* %7
br label %25
25: ; preds = %20, %19
%26 = load i8*, i8** %4
%27 = load i8*, i8** %2
ret i8* %27
}

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