#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);
char *realloc(char *, int);
char *readLink(const char *path) {
  int len = 32;
  char *buf = NULL;
  while (1) {
    buf = realloc(buf, len);
    int num = readlink(path, buf, len);
    if (num < 0) {
      free(buf);
      return NULL;
    }
    if (num < len) {
      buf = realloc(buf, num + 1);
      buf[num] = 0;
      break;
    }
    len *= 2;
  }
  return buf;
}


x86 assembly (clang, -O0):
<readLink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x20,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@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+0x5e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <readLink+0xa4>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11be <readLink+0x8e>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11cc <readLink+0x9c>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114b <readLink+0x1b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLink>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x20,%ebp
xor    %edi,%edi
mov    $0x20,%esi
call   1050 <realloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x20,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     1196 <readLink+0x66>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %ebp,%ebx
jl     11a2 <readLink+0x72>
add    %ebp,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <readlink@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1170 <readLink+0x40>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11b3 <readLink+0x83>
lea    0x1(%rbx),%esi
mov    %r15,%rdi
call   1050 <realloc@plt>
mov    %ebx,%ecx
movb   $0x0,(%rax,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @readLink(i8* nocapture %0) {
%2 = tail call i8* @realloc(i8* null, i32 32)
%3 = tail call i32 @readlink(i8* %0, i8* %2, i32 32)
%4 = icmp slt i32 %3, 0
br i1 %4, label %9, label %12
5: ; preds = %12
%6 = tail call i8* @realloc(i8* %14, i32 %17)
%7 = tail call i32 @readlink(i8* %0, i8* %6, i32 %17)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %5, %1
%10 = phi i8* [ %2, %1 ], [ %6, %5 ]
%11 = tail call i32 @free(i8* %10)
br label %23
12: ; preds = %1, %5
%13 = phi i32 [ %7, %5 ], [ %3, %1 ]
%14 = phi i8* [ %6, %5 ], [ %2, %1 ]
%15 = phi i32 [ %17, %5 ], [ 32, %1 ]
%16 = icmp slt i32 %13, %15
%17 = shl nsw i32 %15, 1
br i1 %16, label %18, label %5
18: ; preds = %12
%19 = add nuw nsw i32 %13, 1
%20 = tail call i8* @realloc(i8* %14, i32 %19)
%21 = zext i32 %13 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %9, %18
%24 = phi i8* [ %20, %18 ], [ null, %9 ]
ret i8* %24
}