#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;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int PATH_MAX;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int readlink(char const *, char *, int);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *get_smlink(const char *file_path, const struct stat *attr) {
  char *sym_link = NULL;
  ssize_t r, bufsiz;
  ;
  bufsiz = attr->st_size + 1;
  if (S_ISLNK(attr->st_mode)) {
    if (attr->st_size == 0)
      bufsiz = PATH_MAX;
    sym_link = malloc(sizeof(char) * bufsiz);
    if (sym_link == NULL) {
      perror("malloc");
      exit(EXIT_FAILURE);
    }
    while ((r = readlink(file_path, sym_link, bufsiz)) > 1 && (r > bufsiz)) {
      bufsiz *= 2;
      if ((sym_link = realloc(sym_link, sizeof(char) * bufsiz)) == NULL) {
        fprintf(stderr, "Not enough memory to continue\n");
        exit(EXIT_FAILURE);
      }
    }
    if (r == -1) {
      perror("readlink");
      exit(EXIT_FAILURE);
    }
    sym_link[r] = '\0';
    return sym_link;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_smlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     12ce <get_smlink+0x15e>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11c3 <get_smlink+0x53>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fb <get_smlink+0x8b>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1200 <get_smlink+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <readlink@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x29(%rbp)
jle    122f <get_smlink+0xbf>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    123f <get_smlink+0xcf>
jmp    1291 <get_smlink+0x121>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    128c <get_smlink+0x11c>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1200 <get_smlink+0x90>
cmpl   $0xffffffff,-0x24(%rbp)
jne    12b5 <get_smlink+0x145>
lea    0xd84(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d6 <get_smlink+0x166>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_smlink>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%ebp
mov    0x4(%rsi),%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     1199 <get_smlink+0x29>
cmpl   $0x0,(%rbx)
jne    119d <get_smlink+0x2d>
mov    0x2e5b(%rip),%rax
mov    (%rax),%ebp
jmp    11a0 <get_smlink+0x30>
xor    %ebx,%ebx
jmp    1208 <get_smlink+0x98>
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1210 <get_smlink+0xa0>
mov    %rax,%rbx
nop
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <readlink@plt>
cmp    $0x2,%eax
jl     11fd <get_smlink+0x8d>
cmp    %ebp,%eax
jle    11fd <get_smlink+0x8d>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b0 <get_smlink+0x40>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmp    $0xffffffff,%eax
je     1219 <get_smlink+0xa9>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde9(%rip),%rdi
jmp    1220 <get_smlink+0xb0>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
jmp    11ef <get_smlink+0x7f>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Not enough memory to continue\0A\00"
@.str.2 = [9 x i8] c"readlink\00"
define i8* @get_smlink(i8* %0, %struct.stat* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store %struct.stat* %1, %struct.stat** %5
store i8* null, i8** %6
%9 = load %struct.stat*, %struct.stat** %5
%10 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1
store i32 %12, i32* %8
%13 = load %struct.stat*, %struct.stat** %5
%14 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i64 @S_ISLNK(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %79
18: ; preds = %2
%19 = load %struct.stat*, %struct.stat** %5
%20 = getelementptr inbounds %struct.stat, %struct.stat* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i32, i32* @PATH_MAX
store i32 %24, i32* %8
br label %25
25: ; preds = %23, %18
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = mul i64 1, %27
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %25
br label %38
38: ; preds = %65, %37
%39 = load i8*, i8** %4
%40 = load i8*, i8** %6
%41 = load i32, i32* %8
%42 = call i32 @readlink(i8* %39, i8* %40, i32 %41)
store i32 %42, i32* %7
%43 = icmp sgt i32 %42, 1
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = icmp sgt i32 %45, %46
br label %48
48: ; preds = %44, %38
%49 = phi i1 [ false, %38 ], [ %47, %44 ]
br i1 %49, label %50, label %66
50: ; preds = %48
%51 = load i32, i32* %8
%52 = mul nsw i32 %51, 2
store i32 %52, i32* %8
%53 = load i8*, i8** %6
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = mul i64 1, %55
%57 = trunc i64 %56 to i32
%58 = call i8* @realloc(i8* %53, i32 %57)
store i8* %58, i8** %6
%59 = icmp eq i8* %58, null
br i1 %59, label %60, label %65
60: ; preds = %50
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %50
br label %38
66: ; preds = %48
%67 = load i32, i32* %7
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %66
%74 = load i8*, i8** %6
%75 = load i32, i32* %7
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
store i8 0, i8* %77
%78 = load i8*, i8** %6
store i8* %78, i8** %3
br label %80
79: ; preds = %2
store i8* null, i8** %3
br label %80
80: ; preds = %79, %73
%81 = load i8*, i8** %3
ret i8* %81
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Not enough memory to continue\0A\00"
@.str.2 = [9 x i8] c"readlink\00"
define i8* @get_smlink(i8* nocapture %0, %struct.stat* nocapture %1) {
%3 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%6 = load i32, i32* %5
%7 = tail call i64 @S_ISLNK(i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %46, label %9
9: ; preds = %2
%10 = add nsw i32 %4, 1
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
%13 = load i32, i32* @PATH_MAX
%14 = select i1 %12, i32 %13, i32 %10
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %9, %28
%22 = phi i8* [ %30, %28 ], [ %15, %9 ]
%23 = phi i32 [ %29, %28 ], [ %14, %9 ]
%24 = tail call i32 @readlink(i8* %0, i8* %22, i32 %23)
%25 = icmp sgt i32 %24, 1
%26 = icmp sgt i32 %24, %23
%27 = and i1 %25, %26
br i1 %27, label %28, label %37
28: ; preds = %21
%29 = shl nsw i32 %23, 1
%30 = tail call i8* @realloc(i8* %22, i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %21
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %21
%38 = icmp eq i32 %24, -1
br i1 %38, label %39, label %43
39: ; preds = %37
%40 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %37
%44 = sext i32 %24 to i64
%45 = getelementptr inbounds i8, i8* %22, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %2, %43
%47 = phi i8* [ %22, %43 ], [ null, %2 ]
ret i8* %47
}