#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 */
struct stat {
  scalar_t__ st_size;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int lstat(char *, struct stat *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
size_t readlink(char *, char *, scalar_t__);
char *readlink_alloc(char *path, size_t *plen) {
  struct stat s;
  int rv = lstat(path, &s);
  if (rv == -1) {
    perror("lstat: ");
    exit(1);
  }
  char *link = (char *)malloc(s.st_size + 1);
  ssize_t len = readlink(path, link, s.st_size);
  if (plen)
    *plen = len;
  return link;
}


x86 assembly (clang, -O0):
<readlink_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1190 <readlink_alloc+0x40>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <readlink@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cc <readlink_alloc+0x7c>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink_alloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rsi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
je     11a0 <readlink_alloc+0x50>
mov    (%rsp),%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1030 <readlink@plt>
test   %r14,%r14
je     1191 <readlink_alloc+0x41>
mov    %rax,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [8 x i8] c"lstat: \00"
define i8* @readlink_alloc(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %3
store i64* %1, i64** %4
%9 = load i8*, i8** %3
%10 = call i32 @lstat(i8* %9, %struct.stat* %5)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%18 = load i64, i64* %17
%19 = add nsw i64 %18, 1
%20 = call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %7
%22 = load i8*, i8** %3
%23 = load i8*, i8** %7
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%25 = load i64, i64* %24
%26 = call i64 @readlink(i8* %22, i8* %23, i64 %25)
store i64 %26, i64* %8
%27 = load i64*, i64** %4
%28 = icmp ne i64* %27, null
br i1 %28, label %29, label %32
29: ; preds = %16
%30 = load i64, i64* %8
%31 = load i64*, i64** %4
store i64 %30, i64* %31
br label %32
32: ; preds = %29, %16
%33 = load i8*, i8** %7
ret i8* %33
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [8 x i8] c"lstat: \00"
define i8* @readlink_alloc(i8* nocapture %0, i64* writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i64, i64* %11
%13 = add nsw i64 %12, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i64 @readlink(i8* %0, i8* %15, i64 %12)
%17 = icmp eq i64* %1, null
br i1 %17, label %19, label %18
18: ; preds = %10
store i64 %16, i64* %1
br label %19
19: ; preds = %18, %10
ret i8* %15
}