#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_mode;
};

/* Variables and functions */
int S_IFLNK;
int S_IFMT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
size_t readlink(char const *, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *);
void getlinkcontents(struct stat *s, const char *path, char *buf,
                     size_t bufsiz) {
  if ((s->st_mode & S_IFMT) == S_IFLNK) {
    sprintf(buf, "-> ");
    size_t ret = readlink(path, &buf[3], bufsiz - 3);
    if (ret == -1) {
      perror("readlink()");
      exit(-1);
    }
    if (ret + 3 < bufsiz)
      buf[ret + 3] = '\0';
    else
      buf[bufsiz - 1] = '\0';
  } else {
    buf[0] = '\0';
  }
}


x86 assembly (clang, -O0):
<getlinkcontents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    0x2e73(%rip),%rcx
and    (%rcx),%eax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
jne    11fd <getlinkcontents+0xbd>
mov    -0x18(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x3,%rsi
mov    -0x20(%rbp),%rdx
sub    $0x3,%rdx
call   1030 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11c4 <getlinkcontents+0x84>
lea    0xe4f(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x3,%rax
cmp    -0x20(%rbp),%rax
jae    11e8 <getlinkcontents+0xa8>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,0x3(%rax,%rcx,1)
jmp    11f8 <getlinkcontents+0xb8>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1204 <getlinkcontents+0xc4>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlinkcontents>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
and    (%rdi),%eax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    0x2e9c(%rip),%rcx
cmp    (%rcx),%eax
jne    1184 <getlinkcontents+0x54>
movl   $0x203e2d,(%rbx)
lea    0x3(%rbx),%rax
lea    -0x3(%r14),%rdx
mov    %rsi,%rdi
mov    %rax,%rsi
call   1030 <readlink@plt>
cmp    $0xffffffffffffffff,%rax
je     118f <getlinkcontents+0x5f>
add    $0x3,%rax
cmp    %r14,%rax
jae    117d <getlinkcontents+0x4d>
add    %rax,%rbx
jmp    1184 <getlinkcontents+0x54>
add    %r14,%rbx
add    $0xffffffffffffffff,%rbx
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6a(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@.str = [4 x i8] c"-> \00"
@.str.1 = [11 x i8] c"readlink()\00"
define void @getlinkcontents(%struct.stat* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca %struct.stat*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store %struct.stat* %0, %struct.stat** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load %struct.stat*, %struct.stat** %5
%11 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @S_IFMT
%14 = and i32 %12, %13
%15 = load i32, i32* @S_IFLNK
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %47
17: ; preds = %4
%18 = load i8*, i8** %7
%19 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 3
%23 = load i64, i64* %8
%24 = sub i64 %23, 3
%25 = call i64 @readlink(i8* %20, i8* %22, i64 %24)
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %17
%32 = load i64, i64* %9
%33 = add i64 %32, 3
%34 = load i64, i64* %8
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = load i64, i64* %9
%39 = add i64 %38, 3
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
br label %46
41: ; preds = %31
%42 = load i8*, i8** %7
%43 = load i64, i64* %8
%44 = sub i64 %43, 1
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %41, %36
br label %50
47: ; preds = %4
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i64 0
store i8 0, i8* %49
br label %50
50: ; preds = %47, %46
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@.str.1 = [11 x i8] c"readlink()\00"
define void @getlinkcontents(%struct.stat* nocapture %0, i8* nocapture %1, i8* nocapture %2, i64 %3) {
%5 = getelementptr inbounds %struct.stat, %struct.stat* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @S_IFMT
%8 = and i32 %7, %6
%9 = load i32, i32* @S_IFLNK
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %28
11: ; preds = %4
%12 = bitcast i8* %2 to i32*
store i32 2113069, i32* %12
%13 = getelementptr inbounds i8, i8* %2, i64 3
%14 = add i64 %3, -3
%15 = tail call i64 @readlink(i8* %1, i8* %13, i64 %14)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %11
%21 = add i64 %15, 3
%22 = icmp ult i64 %21, %3
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = getelementptr inbounds i8, i8* %2, i64 %21
br label %28
25: ; preds = %20
%26 = add i64 %3, -1
%27 = getelementptr inbounds i8, i8* %2, i64 %26
br label %28
28: ; preds = %4, %23, %25
%29 = phi i8* [ %24, %23 ], [ %27, %25 ], [ %2, %4 ]
store i8 0, i8* %29
ret void
}