#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 FILENAME_MAX;
char *calloc(size_t, int);
int errno;
int /*<<< orphan*/ free(char *);
size_t readlink(char const *, char *, size_t);
int readlink_malloc(const char *p, char **ret) {
  size_t l = FILENAME_MAX + 1;
  int r;
  for (;;) {
    char *c = calloc(l, sizeof(char));
    if (!c) {
      return -ENOMEM;
    }
    ssize_t n = readlink(p, c, l - 1);
    if (n < 0) {
      r = -errno;
      free(c);
      return r;
    }
    if ((size_t)n < l - 1) {
      c[n] = 0;
      *ret = c;
      return 0;
    }
    free(c);
    l *= 2;
  }
}


x86 assembly (clang, -O0):
<readlink_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1182 <readlink_malloc+0x52>
xor    %eax,%eax
mov    0x2e60(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121a <readlink_malloc+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <readlink@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jae    11c8 <readlink_malloc+0x98>
xor    %eax,%eax
mov    0x2e21(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121a <readlink_malloc+0xea>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1200 <readlink_malloc+0xd0>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    121a <readlink_malloc+0xea>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1152 <readlink_malloc+0x22>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink_malloc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ea2(%rip),%rax
movslq (%rax),%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11a7 <readlink_malloc+0x77>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    -0x1(%rbx),%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <readlink@plt>
cmp    %r13,%rax
jb     11b4 <readlink_malloc+0x84>
mov    %r12,%rdi
call   1030 <free@plt>
add    %rbx,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1170 <readlink_malloc+0x40>
mov    0x2e2a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11be <readlink_malloc+0x8e>
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):
@FILENAME_MAX = global i32 0
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @readlink_malloc(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = load i32, i32* @FILENAME_MAX
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %6
br label %13
13: ; preds = %46, %2
%14 = load i64, i64* %6
%15 = call i8* @calloc(i64 %14, i32 1)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* @ENOMEM
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %51
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = load i8*, i8** %8
%24 = load i64, i64* %6
%25 = sub i64 %24, 1
%26 = call i64 @readlink(i8* %22, i8* %23, i64 %25)
store i64 %26, i64* %9
%27 = load i64, i64* %9
%28 = icmp ult i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %21
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
store i32 %31, i32* %7
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %7
store i32 %34, i32* %3
br label %51
35: ; preds = %21
%36 = load i64, i64* %9
%37 = load i64, i64* %6
%38 = sub i64 %37, 1
%39 = icmp ult i64 %36, %38
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i8*, i8** %8
%42 = load i64, i64* %9
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %8
%45 = load i8**, i8*** %5
store i8* %44, i8** %45
store i32 0, i32* %3
br label %51
46: ; preds = %35
%47 = load i8*, i8** %8
%48 = call i32 @free(i8* %47)
%49 = load i64, i64* %6
%50 = mul i64 %49, 2
store i64 %50, i64* %6
br label %13
51: ; preds = %40, %29, %18
%52 = load i32, i32* %3
ret i32 %52
}

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