#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ initial_buf;

/* Variables and functions */
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, scalar_t__);
scalar_t__ readlink(char const *, char *, size_t);
scalar_t__ realloc(char *, scalar_t__);
int /*<<< orphan*/ xalloc_die();
scalar_t__ xmalloc(size_t);
char *xreadlink(char const *filename) {
  char initial_buf[1024];
  char *buffer = initial_buf;
  size_t buf_size = sizeof(initial_buf);
  while (1) {
    ssize_t link_length = readlink(filename, buffer, buf_size);
    if (link_length < 0) {
      if (buffer != initial_buf) {
        int saved_errno = errno;
        free(buffer);
        errno = saved_errno;
      }
      return NULL;
    }
    if ((size_t)link_length < buf_size) {
      buffer[link_length++] = '\0';
      if (buffer == initial_buf) {
        buffer = (char *)xmalloc(link_length);
        memcpy(buffer, initial_buf, link_length);
      } else {
        if ((size_t)link_length < buf_size) {
          char *smaller_buffer = (char *)realloc(buffer, link_length);
          if (smaller_buffer != NULL)
            buffer = smaller_buffer;
        }
      }
      return buffer;
    }
    if (buffer != initial_buf)
      free(buffer);
    buf_size *= 2;
    if (((ssize_t)(((size_t)-1) / 2)) < buf_size ||
        (((size_t)-1) / 2 < ((ssize_t)(((size_t)-1) / 2)) && buf_size == 0))
      xalloc_die();
    buffer = (char *)xmalloc(buf_size);
  }
}


x86 assembly (clang, -O0):
<xreadlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
movq   $0x400,-0x420(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x418(%rbp),%rsi
mov    -0x420(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jge    1202 <xreadlink+0xa2>
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
je     11f5 <xreadlink+0x95>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
mov    -0x42c(%rbp),%ecx
mov    0x2ded(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    134e <xreadlink+0x1ee>
mov    -0x428(%rbp),%rax
cmp    -0x420(%rbp),%rax
jae    12e3 <xreadlink+0x183>
mov    -0x418(%rbp),%rax
mov    -0x428(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x428(%rbp)
movb   $0x0,(%rax,%rcx,1)
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
jne    127f <xreadlink+0x11f>
mov    -0x428(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1070 <memcpy@plt>
jmp    12d3 <xreadlink+0x173>
mov    -0x428(%rbp),%rax
cmp    -0x420(%rbp),%rax
jae    12ce <xreadlink+0x16e>
mov    -0x418(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
je     12c9 <xreadlink+0x169>
mov    -0x438(%rbp),%rax
mov    %rax,-0x418(%rbp)
jmp    12ce <xreadlink+0x16e>
jmp    12d3 <xreadlink+0x173>
mov    -0x418(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    134e <xreadlink+0x1ee>
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
je     1306 <xreadlink+0x1a6>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
mov    -0x420(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x420(%rbp)
movabs $0x7fffffffffffffff,%rax
ff ff 7f
cmp    -0x420(%rbp),%rax
jae    1336 <xreadlink+0x1d6>
mov    $0x0,%al
call   1060 <xalloc_die@plt>
mov    -0x420(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x418(%rbp)
jmp    1188 <xreadlink+0x28>
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xreadlink>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r15
mov    %rsp,%r12
mov    $0x400,%r13d
mov    $0x400,%edx
mov    %r12,%rsi
call   1040 <readlink@plt>
test   %rax,%rax
js     1226 <xreadlink+0xc6>
mov    %rax,%rbx
mov    %r12,%r14
jmp    11c1 <xreadlink+0x61>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1040 <readlink@plt>
mov    %rax,%rbx
test   %rax,%rax
js     120b <xreadlink+0xab>
cmp    %r13,%rbx
jb     11e1 <xreadlink+0x81>
cmp    %r12,%r14
je     11d3 <xreadlink+0x73>
mov    %r14,%rdi
call   1030 <free@plt>
add    %r13,%r13
jns    11a0 <xreadlink+0x40>
xor    %eax,%eax
call   1060 <xalloc_die@plt>
jmp    11a0 <xreadlink+0x40>
movb   $0x0,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %rsp,%rax
cmp    %rax,%r14
je     123e <xreadlink+0xde>
cmp    %r13,%rbx
jae    1229 <xreadlink+0xc9>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
cmovne %rax,%r14
jmp    1229 <xreadlink+0xc9>
mov    %rsp,%rax
cmp    %r14,%rax
je     1226 <xreadlink+0xc6>
mov    0x2dc6(%rip),%rbx
mov    (%rbx),%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,(%rbx)
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %rsp,%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
jmp    1229 <xreadlink+0xc9>

LLVM IR (-O0):
@errno = global i32 0
define i8* @xreadlink(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8* %10, i8** %5
store i64 1024, i64* %6
br label %11
11: ; preds = %1, %79
%12 = load i8*, i8** %3
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @readlink(i8* %12, i8* %13, i64 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = icmp ne i8* %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* @errno
store i32 %23, i32* %8
%24 = load i8*, i8** %5
%25 = call i32 @free(i8* %24)
%26 = load i32, i32* %8
store i32 %26, i32* @errno
br label %27
27: ; preds = %22, %18
store i8* null, i8** %2
br label %83
28: ; preds = %11
%29 = load i64, i64* %7
%30 = load i64, i64* %6
%31 = icmp ult i64 %29, %30
br i1 %31, label %32, label %65
32: ; preds = %28
%33 = load i8*, i8** %5
%34 = load i64, i64* %7
%35 = add nsw i64 %34, 1
store i64 %35, i64* %7
%36 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %36
%37 = load i8*, i8** %5
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %48
40: ; preds = %32
%41 = load i64, i64* %7
%42 = call i64 @xmalloc(i64 %41)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %5
%44 = load i8*, i8** %5
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%46 = load i64, i64* %7
%47 = call i32 @memcpy(i8* %44, i8* %45, i64 %46)
br label %63
48: ; preds = %32
%49 = load i64, i64* %7
%50 = load i64, i64* %6
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %62
52: ; preds = %48
%53 = load i8*, i8** %5
%54 = load i64, i64* %7
%55 = call i64 @realloc(i8* %53, i64 %54)
%56 = inttoptr i64 %55 to i8*
store i8* %56, i8** %9
%57 = load i8*, i8** %9
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %61
59: ; preds = %52
%60 = load i8*, i8** %9
store i8* %60, i8** %5
br label %61
61: ; preds = %59, %52
br label %62
62: ; preds = %61, %48
br label %63
63: ; preds = %62, %40
%64 = load i8*, i8** %5
store i8* %64, i8** %2
br label %83
65: ; preds = %28
%66 = load i8*, i8** %5
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%68 = icmp ne i8* %66, %67
br i1 %68, label %69, label %72
69: ; preds = %65
%70 = load i8*, i8** %5
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %69, %65
%73 = load i64, i64* %6
%74 = mul i64 %73, 2
store i64 %74, i64* %6
%75 = load i64, i64* %6
%76 = icmp ult i64 9223372036854775807, %75
br i1 %76, label %77, label %79
77: ; preds = %72
%78 = call i32 (...) @xalloc_die()
br label %79
79: ; preds = %77, %72
%80 = load i64, i64* %6
%81 = call i64 @xmalloc(i64 %80)
%82 = inttoptr i64 %81 to i8*
store i8* %82, i8** %5
br label %11
83: ; preds = %63, %27
%84 = load i8*, i8** %2
ret i8* %84
}

LLVM IR (-O2):
@errno = global i32 0
define i8* @xreadlink(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i64 @readlink(i8* %0, i8* %3, i64 1024)
%5 = icmp slt i64 %4, 0
br i1 %5, label %46, label %12
6: ; preds = %41
%7 = inttoptr i64 %42 to i8*
%8 = icmp eq i8* %3, %7
br i1 %8, label %46, label %9
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = call i32 @free(i8* %7)
store i32 %10, i32* @errno
br label %46
12: ; preds = %1, %41
%13 = phi i64 [ %44, %41 ], [ %4, %1 ]
%14 = phi i8* [ %43, %41 ], [ %3, %1 ]
%15 = phi i64 [ %37, %41 ], [ 1024, %1 ]
%16 = icmp ult i64 %13, %15
br i1 %16, label %17, label %32
17: ; preds = %12
%18 = add nuw nsw i64 %13, 1
%19 = getelementptr inbounds i8, i8* %14, i64 %13
store i8 0, i8* %19
%20 = icmp eq i8* %14, %3
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i64 @xmalloc(i64 %18)
%23 = inttoptr i64 %22 to i8*
%24 = call i32 @memcpy(i8* %23, i8* %3, i64 %18)
br label %46
25: ; preds = %17
%26 = icmp ult i64 %18, %15
br i1 %26, label %27, label %46
27: ; preds = %25
%28 = call i64 @realloc(i8* %14, i64 %18)
%29 = icmp eq i64 %28, 0
%30 = inttoptr i64 %28 to i8*
%31 = select i1 %29, i8* %14, i8* %30
br label %46
32: ; preds = %12
%33 = icmp eq i8* %14, %3
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = call i32 @free(i8* %14)
br label %36
36: ; preds = %34, %32
%37 = shl i64 %15, 1
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 (...) @xalloc_die()
br label %41
41: ; preds = %36, %39
%42 = call i64 @xmalloc(i64 %37)
%43 = inttoptr i64 %42 to i8*
%44 = call i64 @readlink(i8* %0, i8* %43, i64 %37)
%45 = icmp slt i64 %44, 0
br i1 %45, label %6, label %12
46: ; preds = %1, %9, %6, %25, %27, %21
%47 = phi i8* [ %14, %25 ], [ %31, %27 ], [ %23, %21 ], [ null, %6 ], [ null, %9 ], [ null, %1 ]
ret i8* %47
}