#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 ENOMEM;
int errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
size_t readlink(char const *, char *, size_t);
int readlink_alloc(const char *path, char **buf) {
  size_t l = 100;
  for (;;) {
    char *c;
    ssize_t n;
    c = malloc(l * sizeof(char));
    if (!c)
      return -ENOMEM;
    if ((n = readlink(path, c, l - 1)) < 0) {
      int ret = -errno;
      free(c);
      return ret;
    }
    if ((size_t)n < l - 1) {
      c[n] = 0;
      *buf = c;
      return 0;
    }
    free(c);
    l *= 2;
  }
}


x86 assembly (clang, -O0):
<readlink_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x64,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1177 <readlink_alloc+0x47>
xor    %eax,%eax
mov    0x2e73(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <readlink_alloc+0xde>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <readlink@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jae    11bc <readlink_alloc+0x8c>
xor    %eax,%eax
mov    0x2e35(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <readlink_alloc+0xde>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11f4 <readlink_alloc+0xc4>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    120e <readlink_alloc+0xde>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1148 <readlink_alloc+0x18>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink_alloc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x64,%ebx
mov    $0x64,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1192 <readlink_alloc+0x62>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
lea    -0x1(%rbx),%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <readlink@plt>
cmp    %r13,%rax
jb     119f <readlink_alloc+0x6f>
mov    %r12,%rdi
call   1030 <free@plt>
add    %rbx,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1160 <readlink_alloc+0x30>
mov    0x2e47(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11a9 <readlink_alloc+0x79>
movb   $0x0,(%r12,%rax,1)
mov    %r12,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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