#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 int ssize_t;

/* Variables and functions */
void abort();
int /*<<< orphan*/ free(char *);
int readlinkat(int, char const *, char *, size_t);
char *xmalloc(size_t, int);
const char *ls_readlink(int dirfd, const char *name, size_t size) {
  char *buf = xmalloc(size + 1, 1);
  ssize_t n = readlinkat(dirfd, name, buf, size);
  if (n == -1) {
    free(buf);
    return 0;
  }
  ((size_t)n == size ? (void)0 : abort());
  buf[n] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<ls_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <readlinkat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a0 <ls_readlink+0x60>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cc <ls_readlink+0x8c>
movslq -0x2c(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11b3 <ls_readlink+0x73>
jmp    11b8 <ls_readlink+0x78>
call   1040 <abort@plt>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_readlink>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x1(%rdx),%rdi
mov    $0x1,%esi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %r14,%rcx
call   1060 <readlinkat@plt>
cmp    $0xffffffff,%eax
je     1183 <ls_readlink+0x43>
cltq
cmp    %r14,%rax
jne    119b <ls_readlink+0x5b>
movb   $0x0,(%rbx,%r14,1)
jmp    118d <ls_readlink+0x4d>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
call   1040 <abort@plt>

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

LLVM IR (-O2):
define i8* @ls_readlink(i32 %0, i8* %1, i64 %2) {
%4 = add i64 %2, 1
%5 = tail call i8* @xmalloc(i64 %4, i32 1)
%6 = tail call i32 @readlinkat(i32 %0, i8* %1, i8* %5, i64 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @free(i8* %5)
br label %16
10: ; preds = %3
%11 = sext i32 %6 to i64
%12 = icmp eq i64 %11, %2
br i1 %12, label %14, label %13
13: ; preds = %10
tail call void @abort()
unreachable
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %5, i64 %2
store i8 0, i8* %15
br label %16
16: ; preds = %14, %8
%17 = phi i8* [ null, %8 ], [ %5, %14 ]
ret i8* %17
}