#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 /*<<< orphan*/ free(char *);
int readlink(char const *, char *, int);
scalar_t__ xrealloc(char *, int);
char *readlink_malloc(const char *filename) {
  int size = 100;
  char *buffer = NULL;
  while (1) {
    buffer = (char *)xrealloc(buffer, size);
    int nchars = readlink(filename, buffer, size);
    if (nchars < 0) {
      free(buffer);
      return NULL;
    }
    if (nchars < size)
      return buffer;
    size *= 2;
  }
}


x86 assembly (clang, -O0):
<readlink_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x64,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <xrealloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118e <readlink_malloc+0x5e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <readlink_malloc+0x85>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a7 <readlink_malloc+0x77>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <readlink_malloc+0x85>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114b <readlink_malloc+0x1b>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink_malloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
mov    $0x64,%esi
call   1050 <xrealloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x64,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     1199 <readlink_malloc+0x69>
cmp    $0x64,%eax
jl     11a4 <readlink_malloc+0x74>
mov    $0xc8,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <xrealloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     1199 <readlink_malloc+0x69>
lea    (%rbx,%rbx,1),%ecx
cmp    %ebx,%eax
mov    %ecx,%ebx
jge    1170 <readlink_malloc+0x40>
jmp    11a4 <readlink_malloc+0x74>
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* @readlink_malloc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 100, i32* %4
store i8* null, i8** %5
br label %7
7: ; preds = %1, %27
%8 = load i8*, i8** %5
%9 = load i32, i32* %4
%10 = call i64 @xrealloc(i8* %8, i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %3
%13 = load i8*, i8** %5
%14 = load i32, i32* %4
%15 = call i32 @readlink(i8* %12, i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %7
%19 = load i8*, i8** %5
%20 = call i32 @free(i8* %19)
store i8* null, i8** %2
br label %30
21: ; preds = %7
%22 = load i32, i32* %6
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i8*, i8** %5
store i8* %26, i8** %2
br label %30
27: ; preds = %21
%28 = load i32, i32* %4
%29 = mul nsw i32 %28, 2
store i32 %29, i32* %4
br label %7
30: ; preds = %25, %18
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
define i8* @readlink_malloc(i8* nocapture %0) {
%2 = tail call i64 @xrealloc(i8* null, i32 100)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @readlink(i8* %0, i8* %3, i32 100)
%5 = icmp slt i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %1
%7 = icmp slt i32 %4, 100
br i1 %7, label %23, label %8
8: ; preds = %6, %20
%9 = phi i32 [ %22, %20 ], [ 200, %6 ]
%10 = phi i8* [ %12, %20 ], [ %3, %6 ]
%11 = tail call i64 @xrealloc(i8* %10, i32 %9)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @readlink(i8* %0, i8* %12, i32 %9)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = inttoptr i64 %11 to i8*
br label %17
17: ; preds = %15, %1
%18 = phi i8* [ %3, %1 ], [ %16, %15 ]
%19 = tail call i32 @free(i8* %18)
br label %26
20: ; preds = %8
%21 = icmp slt i32 %13, %9
%22 = shl nsw i32 %9, 1
br i1 %21, label %23, label %8
23: ; preds = %20, %6
%24 = phi i64 [ %2, %6 ], [ %11, %20 ]
%25 = inttoptr i64 %24 to i8*
br label %26
26: ; preds = %23, %17
%27 = phi i8* [ null, %17 ], [ %25, %23 ]
ret i8* %27
}