#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*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t readlink(char const *, char *, size_t);
char *realloc(char *, size_t);
char *areadlink(const char *path) {
  size_t size = 256;
  char *buf = NULL;
  for (;;) {
    buf = realloc(buf, size);
    memset(buf, 0, size);
    ssize_t result_len = readlink(path, buf, size);
    if (result_len == size) {
      size *= 2;
    } else if (result_len == -1) {
      free(buf);
      return NULL;
    } else {
      break;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<areadlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x100,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11b0 <areadlink+0x70>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11d6 <areadlink+0x96>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11d1 <areadlink+0x91>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <areadlink+0xa3>
jmp    11db <areadlink+0x9b>
jmp    115c <areadlink+0x1c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<areadlink>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x100,%ebx
xor    %r15d,%r15d
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
lea    (%rbx,%rbx,1),%rcx
cmp    %rbx,%rax
mov    %rcx,%rbx
je     1150 <areadlink+0x10>
cmp    $0xffffffffffffffff,%rax
jne    1196 <areadlink+0x56>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @areadlink(i8* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 256, %1 ], [ %9, %2 ]
%4 = phi i8* [ null, %1 ], [ %5, %2 ]
%5 = tail call i8* @realloc(i8* %4, i64 %3)
%6 = tail call i32 @memset(i8* %5, i32 0, i64 %3)
%7 = tail call i64 @readlink(i8* %0, i8* %5, i64 %3)
%8 = icmp eq i64 %7, %3
%9 = shl i64 %3, 1
br i1 %8, label %2, label %10
10: ; preds = %2
%11 = icmp eq i64 %7, -1
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @free(i8* %5)
br label %14
14: ; preds = %10, %12
%15 = phi i8* [ null, %12 ], [ %5, %10 ]
ret i8* %15
}