#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 *);
char *malloc(size_t);
size_t readlink(char *, char *, size_t);
char *read_symlink(char *path) {
  char *target;
  size_t max_length = 1024;
  ssize_t length;
  while (1) {
    target = malloc(max_length + 1);
    if (target == 0)
      break;
    length = readlink(path, target, max_length);
    if (length == (ssize_t)-1) {
      free(target);
      target = 0;
      break;
    }
    if (length >= (ssize_t)max_length) {
      free(target);
      max_length += 1024;
      continue;
    }
    target[length] = '\0';
    break;
  }
  return target;
}


x86 assembly (clang, -O0):
<read_symlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1165 <read_symlink+0x35>
jmp    11d1 <read_symlink+0xa1>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    119b <read_symlink+0x6b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    11d1 <read_symlink+0xa1>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11c5 <read_symlink+0x95>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <read_symlink+0x14>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_symlink>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x401,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     118c <read_symlink+0x5c>
mov    %rax,%r15
mov    $0x400,%ebx
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
cmp    $0xffffffffffffffff,%rax
je     1191 <read_symlink+0x61>
cmp    %rbx,%rax
jb     119e <read_symlink+0x6e>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0x401(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
add    $0x400,%rbx
test   %rax,%rax
jne    1150 <read_symlink+0x20>
xor    %r15d,%r15d
jmp    11a3 <read_symlink+0x73>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11a3 <read_symlink+0x73>
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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