#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 */
size_t BUFSIZ;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
size_t readlink(char const *, char *, size_t);
char *realloc(char *, size_t);
char *readlinka(char const *path) {
  size_t buflen = BUFSIZ;
  char *buf = malloc(buflen);
  ssize_t read;
  while ((read = readlink(path, buf, buflen - 1)) >= buflen - 1) {
    char *newbuf = realloc(buf, buflen * 2);
    if (newbuf == NULL) {
      goto failure;
    }
    buf = newbuf;
    buflen = buflen * 2;
  }
  buf[read] = '\0';
  return buf;
failure:
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<readlinka>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <readlink@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     11cf <readlinka+0x8f>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b6 <readlinka+0x76>
jmp    11e8 <readlinka+0xa8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1167 <readlinka+0x27>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f9 <readlinka+0xb9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlinka>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8e(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
nopl   (%rax)
mov    %rax,%r15
lea    -0x1(%rbx),%r12
mov    %r14,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
call   1040 <readlink@plt>
cmp    %r12,%rax
jb     119a <readlinka+0x5a>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
jne    1160 <readlinka+0x20>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    119f <readlinka+0x5f>
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@BUFSIZ = global i64 0
define i8* @readlinka(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i64, i64* @BUFSIZ
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %5
br label %11
11: ; preds = %28, %1
%12 = load i8*, i8** %3
%13 = load i8*, i8** %5
%14 = load i64, i64* %4
%15 = sub i64 %14, 1
%16 = call i64 @readlink(i8* %12, i8* %13, i64 %15)
store i64 %16, i64* %6
%17 = load i64, i64* %4
%18 = sub i64 %17, 1
%19 = icmp uge i64 %16, %18
br i1 %19, label %20, label %32
20: ; preds = %11
%21 = load i8*, i8** %5
%22 = load i64, i64* %4
%23 = mul i64 %22, 2
%24 = call i8* @realloc(i8* %21, i64 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %20
br label %37
28: ; preds = %20
%29 = load i8*, i8** %7
store i8* %29, i8** %5
%30 = load i64, i64* %4
%31 = mul i64 %30, 2
store i64 %31, i64* %4
br label %11
32: ; preds = %11
%33 = load i8*, i8** %5
%34 = load i64, i64* %6
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %5
store i8* %36, i8** %2
br label %40
37: ; preds = %27
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
store i8* null, i8** %2
br label %40
40: ; preds = %37, %32
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@BUFSIZ = global i64 0
define noalias i8* @readlinka(i8* nocapture %0) {
%2 = load i64, i64* @BUFSIZ
%3 = tail call i8* @malloc(i64 %2)
br label %4
4: ; preds = %10, %1
%5 = phi i8* [ %3, %1 ], [ %12, %10 ]
%6 = phi i64 [ %2, %1 ], [ %11, %10 ]
%7 = add i64 %6, -1
%8 = tail call i64 @readlink(i8* %0, i8* %5, i64 %7)
%9 = icmp ult i64 %8, %7
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = shl i64 %6, 1
%12 = tail call i8* @realloc(i8* %5, i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %4
14: ; preds = %4
%15 = getelementptr inbounds i8, i8* %5, i64 %8
store i8 0, i8* %15
br label %18
16: ; preds = %10
%17 = tail call i32 @free(i8* %5)
br label %18
18: ; preds = %16, %14
%19 = phi i8* [ null, %16 ], [ %5, %14 ]
ret i8* %19
}