#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 const *, char *, size_t);
char *realloc(char *, size_t);
char *linux_readlink(const char *link) {
  size_t bufsize = 255;
  ssize_t result;
  char *buf = malloc(bufsize);
  if (buf == NULL)
    return NULL;
  while ((result = readlink(link, buf, bufsize)) >= bufsize) {
    char *tmp = realloc(buf, bufsize * 2);
    if (tmp == NULL)
      goto error;
    buf = tmp;
    bufsize *= 2;
  }
  if (result == -1)
    goto error;
  buf[result] = '\0';
  return buf;
error:
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<linux_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0xff,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1179 <linux_readlink+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1215 <linux_readlink+0xd5>
jmp    117e <linux_readlink+0x3e>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x20(%rbp)
cmp    -0x18(%rbp),%rax
jb     11db <linux_readlink+0x9b>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c2 <linux_readlink+0x82>
jmp    1204 <linux_readlink+0xc4>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    117e <linux_readlink+0x3e>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11eb <linux_readlink+0xab>
jmp    1204 <linux_readlink+0xc4>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1215 <linux_readlink+0xd5>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linux_readlink>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0xff,%ebx
mov    $0xff,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1191 <linux_readlink+0x51>
nopl   0x0(%rax)
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
cmp    %rbx,%rax
jb     1196 <linux_readlink+0x56>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
jne    1160 <linux_readlink+0x20>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    119b <linux_readlink+0x5b>
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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