#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 errno;
int readlink(char *, char *, int);
int hostfs_do_readlink(char *file, char *buf, int size) {
  int n;
  n = readlink(file, buf, size);
  if (n < 0)
    return -errno;
  if (n < size)
    buf[n] = '\0';
  return n;
}


x86 assembly (clang, -O0):
<hostfs_do_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <readlink@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1153 <hostfs_do_readlink+0x43>
xor    %eax,%eax
mov    0x2e97(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1171 <hostfs_do_readlink+0x61>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    116b <hostfs_do_readlink+0x5b>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostfs_do_readlink>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
call   1030 <readlink@plt>
test   %eax,%eax
js     112d <hostfs_do_readlink+0x1d>
cmp    %ebp,%eax
jge    1138 <hostfs_do_readlink+0x28>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
jmp    1138 <hostfs_do_readlink+0x28>
mov    0x2eac(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
define i32 @hostfs_do_readlink(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @readlink(i8* %0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %14
9: ; preds = %3
%10 = icmp slt i32 %4, %2
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = zext i32 %4 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %9, %11, %6
%15 = phi i32 [ %8, %6 ], [ %4, %11 ], [ %4, %9 ]
ret i32 %15
}