#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 {
  int st_size;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
size_t mystrlen(char *);
int /*<<< orphan*/ perror(char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ write(int, char *, size_t);
void read_link(char *path, struct stat sb) {
  char linkname[sb.st_size + 1];
  ssize_t r;
  r = readlink(path, linkname, sb.st_size + 1);
  if (r == -1) {
    perror("readlink error");
    _exit(1);
  }
  if (r > sb.st_size) {
    perror("Error");
    _exit(1);
  }
  linkname[r] = '\0';
  write(1, "\033[1m\x1b[34m", mystrlen("\033[1m\x1b[34m"));
  write(1, linkname, (size_t)mystrlen(linkname));
  write(1, "\x1b[0m", mystrlen("\x1b[0m"));
}


x86 assembly (clang, -O0):
<read_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %esi,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
add    $0x1,%edx
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ba <read_link+0x6a>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11dc <read_link+0x8c>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xe26(%rip),%rdi
call   1060 <mystrlen@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xe12(%rip),%rsi
call   1050 <write@plt>
mov    -0x30(%rbp),%rdi
call   1060 <mystrlen@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
mov    $0x1,%edi
call   1050 <write@plt>
lea    0xdf6(%rip),%rdi
call   1060 <mystrlen@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xde2(%rip),%rsi
call   1050 <write@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_link>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %esi,%r14d
lea    0x1(%r14),%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11e6 <read_link+0x96>
cmp    %r14d,%eax
jg     11ef <read_link+0x9f>
cltq
movb   $0x0,(%rbx,%rax,1)
lea    0xe87(%rip),%r14
mov    %r14,%rdi
call   1060 <mystrlen@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <write@plt>
mov    %rbx,%rdi
call   1060 <mystrlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <write@plt>
lea    0xe5a(%rip),%rbx
mov    %rbx,%rdi
call   1060 <mystrlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <write@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe13(%rip),%rdi
jmp    11f6 <read_link+0xa6>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"readlink error\00"
@.str.1 = [6 x i8] c"Error\00"
@.str.2 = [10 x i8] c"\1B[1m\1B[34m\00"
@.str.3 = [5 x i8] c"\1B[0m\00"
define void @read_link(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
store i32 %1, i32* %8
store i8* %0, i8** %4
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%10 = load i32, i32* %9
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i8*, i8** %4
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = call i32 @readlink(i8* %15, i8* %14, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @_exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i32, i32* %7
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%28 = load i32, i32* %27
%29 = icmp sgt i32 %26, %28
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @_exit(i32 1)
unreachable
33: ; preds = %25
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %14, i64 %35
store i8 0, i8* %36
%37 = call i64 @mystrlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @write(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %37)
%39 = call i64 @mystrlen(i8* %14)
%40 = call i32 @write(i32 1, i8* %14, i64 %39)
%41 = call i64 @mystrlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @write(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %41)
%43 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"readlink error\00"
@.str.1 = [6 x i8] c"Error\00"
@.str.2 = [10 x i8] c"\1B[1m\1B[34m\00"
@.str.3 = [5 x i8] c"\1B[0m\00"
define void @read_link(i8* nocapture %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @readlink(i8* %0, i8* %5, i32 %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @_exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp sgt i32 %6, %1
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @_exit(i32 1)
unreachable
16: ; preds = %11
%17 = sext i32 %6 to i64
%18 = getelementptr inbounds i8, i8* %5, i64 %17
store i8 0, i8* %18
%19 = tail call i64 @mystrlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %19)
%21 = call i64 @mystrlen(i8* %5)
%22 = call i32 @write(i32 1, i8* %5, i64 %21)
%23 = call i64 @mystrlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @write(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %23)
ret void
}