#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*/ GC_FREE(char *);
scalar_t__ GC_MALLOC_ATOMIC(int);
int readlink(char const *, char *, int);
char *readlink_malloc(const char *filename) {
  int size = 100;
  char *buffer = NULL;
  while (1) {
    buffer = (char *)GC_MALLOC_ATOMIC(size);
    int nchars = readlink(filename, buffer, size);
    if (nchars < 0) {
      GC_FREE(buffer);
      return NULL;
    }
    if (nchars < size)
      return buffer;
    GC_FREE(buffer);
    size *= 2;
  }
}


x86 assembly (clang, -O0):
<readlink_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x64,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <GC_MALLOC_ATOMIC@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118a <readlink_malloc+0x5a>
mov    -0x20(%rbp),%rdi
call   1040 <GC_FREE@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ba <readlink_malloc+0x8a>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a3 <readlink_malloc+0x73>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ba <readlink_malloc+0x8a>
mov    -0x20(%rbp),%rdi
call   1040 <GC_FREE@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114b <readlink_malloc+0x1b>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink_malloc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x64,%edi
call   1050 <GC_MALLOC_ATOMIC@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x64,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     119b <readlink_malloc+0x6b>
cmp    $0x64,%eax
jl     11a5 <readlink_malloc+0x75>
mov    $0x64,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <GC_FREE@plt>
add    %ebp,%ebp
mov    %ebp,%edi
call   1050 <GC_MALLOC_ATOMIC@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     119b <readlink_malloc+0x6b>
cmp    %ebp,%eax
jge    1170 <readlink_malloc+0x40>
jmp    11a5 <readlink_malloc+0x75>
mov    %rbx,%rdi
call   1040 <GC_FREE@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @readlink_malloc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 100, i32* %4
store i8* null, i8** %5
br label %7
7: ; preds = %1, %26
%8 = load i32, i32* %4
%9 = call i64 @GC_MALLOC_ATOMIC(i32 %8)
%10 = inttoptr i64 %9 to i8*
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 @GC_FREE(i8* %18)
store i8* null, i8** %2
br label %31
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 %26
24: ; preds = %20
%25 = load i8*, i8** %5
store i8* %25, i8** %2
br label %31
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = call i32 @GC_FREE(i8* %27)
%29 = load i32, i32* %4
%30 = mul nsw i32 %29, 2
store i32 %30, i32* %4
br label %7
31: ; preds = %24, %17
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
define i8* @readlink_malloc(i8* nocapture %0) {
%2 = tail call i64 @GC_MALLOC_ATOMIC(i32 100)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @readlink(i8* %0, i8* %3, i32 100)
%5 = icmp slt i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = icmp slt i32 %4, 100
br i1 %7, label %24, label %15
8: ; preds = %15
%9 = inttoptr i64 %20 to i8*
br label %10
10: ; preds = %8, %1
%11 = phi i8* [ %3, %1 ], [ %9, %8 ]
%12 = tail call i32 @GC_FREE(i8* %11)
br label %27
13: ; preds = %15
%14 = icmp slt i32 %22, %19
br i1 %14, label %24, label %15
15: ; preds = %6, %13
%16 = phi i32 [ %19, %13 ], [ 100, %6 ]
%17 = phi i8* [ %21, %13 ], [ %3, %6 ]
%18 = tail call i32 @GC_FREE(i8* %17)
%19 = shl nsw i32 %16, 1
%20 = tail call i64 @GC_MALLOC_ATOMIC(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @readlink(i8* %0, i8* %21, i32 %19)
%23 = icmp slt i32 %22, 0
br i1 %23, label %8, label %13
24: ; preds = %13, %6
%25 = phi i64 [ %2, %6 ], [ %20, %13 ]
%26 = inttoptr i64 %25 to i8*
br label %27
27: ; preds = %24, %10
%28 = phi i8* [ null, %10 ], [ %26, %24 ]
ret i8* %28
}