#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;

/* Variables and functions */
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ errno;
size_t readlink(char const *, char *, size_t);
ssize_t __readlink(const char *path, char *buf, size_t bufsiz) {
  ssize_t ret;
  ret = readlink(path, buf, bufsiz);
  if (ret >= 0) {
    if (ret >= bufsiz) {
      errno = ENAMETOOLONG;
      return -1;
    }
    buf[ret] = 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<__readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jb     117d <__readlink+0x6d>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1171 <__readlink+0x61>
mov    0x2e7f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7e(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1185 <__readlink+0x75>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__readlink>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
call   1030 <readlink@plt>
cmp    %rbx,%rax
jae    112b <__readlink+0x1b>
movb   $0x0,(%r14,%rax,1)
jmp    1144 <__readlink+0x34>
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    0x2ea5(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i64 @__readlink(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = call i64 @readlink(i8* %9, i8* %10, i64 %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp uge i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %3
%16 = load i64, i64* %8
%17 = load i64, i64* %7
%18 = icmp uge i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* @ENAMETOOLONG
store i32 %20, i32* @errno
store i64 -1, i64* %4
br label %27
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = load i64, i64* %8
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %21, %3
%26 = load i64, i64* %8
store i64 %26, i64* %4
br label %27
27: ; preds = %25, %19
%28 = load i64, i64* %4
ret i64 %28
}

LLVM IR (-O2):
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i64 @__readlink(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @readlink(i8* %0, i8* %1, i64 %2)
%5 = icmp ult i64 %4, %2
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = load i32, i32* @ENAMETOOLONG
store i32 %7, i32* @errno
br label %10
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %1, i64 %4
store i8 0, i8* %9
br label %10
10: ; preds = %8, %6
%11 = phi i64 [ -1, %6 ], [ %4, %8 ]
ret i64 %11
}