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

/* Variables and functions */
size_t readlink(char const *, char *, int);
char *strdup(char *);
char *is_a_symlink(const char *argv, int link_level) {
  ssize_t n;
  char link[4096];
  char *tmp_link;
  if (link_level == 20)
    return NULL;
  n = readlink(argv, link, 4096);
  if (n < 0)
    return NULL;
  link[n] = '\0';
  tmp_link = is_a_symlink(link, link_level + 1);
  if (!tmp_link)
    return strdup(link);
  return tmp_link;
}


x86 assembly (clang, -O0):
<is_a_symlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jne    1159 <is_a_symlink+0x29>
movq   $0x0,-0x8(%rbp)
jmp    11dd <is_a_symlink+0xad>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <readlink@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    118a <is_a_symlink+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    11dd <is_a_symlink+0xad>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1030 <is_a_symlink@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    11d2 <is_a_symlink+0xa2>
lea    -0x1020(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11dd <is_a_symlink+0xad>
mov    -0x1028(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_a_symlink>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
cmp    $0x14,%esi
jne    1143 <is_a_symlink+0x13>
xor    %eax,%eax
jmp    1173 <is_a_symlink+0x43>
mov    %esi,%ebx
mov    %rsp,%r14
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <readlink@plt>
movb   $0x0,(%rsp,%rax,1)
add    $0x1,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <is_a_symlink@plt>
test   %rax,%rax
jne    1173 <is_a_symlink+0x43>
mov    %rsp,%rdi
call   1050 <strdup@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @is_a_symlink(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca [4096 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 20
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %33
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%15 = call i64 @readlink(i8* %13, i8* %14, i32 4096)
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp ult i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i8* null, i8** %3
br label %33
19: ; preds = %12
%20 = load i64, i64* %6
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %20
store i8 0, i8* %21
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
%25 = call i8* @is_a_symlink(i8* %22, i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %19
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%30 = call i8* @strdup(i8* %29)
store i8* %30, i8** %3
br label %33
31: ; preds = %19
%32 = load i8*, i8** %8
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %28, %18, %11
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @is_a_symlink(i8* nocapture %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %1, 20
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = call i64 @readlink(i8* %0, i8* %4, i32 4096)
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %7
store i8 0, i8* %8
%9 = add nsw i32 %1, 1
%10 = call i8* @is_a_symlink(i8* %4, i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = call i8* @strdup(i8* %4)
br label %14
14: ; preds = %6, %2, %12
%15 = phi i8* [ %13, %12 ], [ null, %2 ], [ %10, %6 ]
ret i8* %15
}