#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 */
struct stat {
  size_t st_size;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ lstat(char const *, struct stat *);
char *malloc(size_t);
size_t readlink(char const *, char *, size_t);
char *lbs_posix_readlink(const char *filename) {
  struct stat st;
  if (lstat(filename, &st) < 0) {
    return NULL;
  }
  size_t bufsize = st.st_size ? st.st_size : 8192;
  char *buf = malloc(bufsize + 1);
  if (buf == NULL) {
    return NULL;
  }
  ssize_t written = readlink(filename, buf, bufsize);
  if (written < 0) {
    free(buf);
    return NULL;
  }
  buf[written] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<lbs_posix_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    1170 <lbs_posix_readlink+0x30>
movq   $0x0,-0x8(%rbp)
jmp    1211 <lbs_posix_readlink+0xd1>
cmpq   $0x0,-0x18(%rbp)
je     1188 <lbs_posix_readlink+0x48>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1196 <lbs_posix_readlink+0x56>
mov    $0x2000,%eax
mov    %rax,-0x38(%rbp)
jmp    1196 <lbs_posix_readlink+0x56>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c7 <lbs_posix_readlink+0x87>
movq   $0x0,-0x8(%rbp)
jmp    1211 <lbs_posix_readlink+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    11fd <lbs_posix_readlink+0xbd>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1211 <lbs_posix_readlink+0xd1>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lbs_posix_readlink>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1040 <lstat@plt>
test   %rax,%rax
js     1182 <lbs_posix_readlink+0x52>
mov    0x8(%rsp),%rax
test   %rax,%rax
mov    $0x2000,%r15d
cmovne %rax,%r15
lea    0x1(%r15),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1182 <lbs_posix_readlink+0x52>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %r15,%rdx
call   1030 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
jmp    1184 <lbs_posix_readlink+0x54>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
define i8* @lbs_posix_readlink(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @lstat(i8* %8, %struct.stat* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %43
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%14 = load i64, i64* %13
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i64, i64* %17
br label %20
19: ; preds = %12
br label %20
20: ; preds = %19, %16
%21 = phi i64 [ %18, %16 ], [ 8192, %19 ]
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = add i64 %22, 1
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %20
store i8* null, i8** %2
br label %43
28: ; preds = %20
%29 = load i8*, i8** %3
%30 = load i8*, i8** %6
%31 = load i64, i64* %5
%32 = call i64 @readlink(i8* %29, i8* %30, i64 %31)
store i64 %32, i64* %7
%33 = load i64, i64* %7
%34 = icmp ult i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
store i8* null, i8** %2
br label %43
38: ; preds = %28
%39 = load i8*, i8** %6
%40 = load i64, i64* %7
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %43
43: ; preds = %38, %35, %27, %11
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
%struct.stat = type { i64 }
define noalias i8* @lbs_posix_readlink(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i64 8192, i64 %8
%11 = add i64 %10, 1
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %6
%15 = tail call i64 @readlink(i8* %0, i8* %12, i64 %10)
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %14, %6, %1
%18 = phi i8* [ null, %1 ], [ %12, %14 ], [ null, %6 ]
ret i8* %18
}