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

/* Variables and functions */
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
char *alloca(scalar_t__);
char *basename(char *);
char *filename(char *);
int lstat(char *, struct stat *);
char *numerical_suffix(char *);
int /*<<< orphan*/ puts(char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
void lnname(char *name) {
  struct stat stat;
  if (lstat(name, &stat) == -1) {
    puts("1");
  } else {
    if (!S_ISLNK(stat.st_mode)) {
      char *fname, *suffix;
      fname = filename(name);
      suffix = numerical_suffix(fname);
      puts(suffix);
    } else {
      char buf[10240];
      char *new_name;
      char *bname;
      int nb;
      nb = readlink(name, buf, 10240);
      buf[nb] = 0;
      new_name = alloca(strlen(name) + nb + 1);
      bname = basename(name);
      sprintf(new_name, "%s/%s", bname, buf);
      lnname(new_name);
    }
  }
}


x86 assembly (clang, -O0):
<lnname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2840,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
jne    11e6 <lnname+0x36>
lea    0xe24(%rip),%rdi
call   1040 <puts@plt>
jmp    12b1 <lnname+0x101>
mov    -0x10(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    121f <lnname+0x6f>
mov    -0x8(%rbp),%rdi
call   10c0 <filename@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <numerical_suffix@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <puts@plt>
jmp    12ac <lnname+0xfc>
mov    -0x8(%rbp),%rdi
lea    -0x2820(%rbp),%rsi
mov    $0x2800,%edx
call   1060 <readlink@plt>
mov    %eax,-0x2834(%rbp)
movslq -0x2834(%rbp),%rax
movb   $0x0,-0x2820(%rbp,%rax,1)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,%rdi
movslq -0x2834(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
call   10b0 <alloca@plt>
mov    %rax,-0x2828(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <basename@plt>
mov    %rax,-0x2830(%rbp)
mov    -0x2828(%rbp),%rdi
mov    -0x2830(%rbp),%rdx
lea    -0x2820(%rbp),%rcx
lea    0xd67(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x2828(%rbp),%rdi
call   1050 <lnname@plt>
jmp    12b1 <lnname+0x101>
add    $0x2840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lnname>:
push   %r15
push   %r14
push   %rbx
sub    $0x2810,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     1243 <lnname+0x93>
mov    0x8(%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %eax,%eax
je     124c <lnname+0x9c>
lea    0x10(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x2800,%edx
call   1060 <readlink@plt>
movslq %eax,%r15
movb   $0x0,0x10(%rsp,%r15,1)
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    (%r15,%rax,1),%rdi
add    $0x1,%rdi
call   10b0 <alloca@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   10a0 <basename@plt>
lea    0xde2(%rip),%rsi
mov    %r15,%rdi
mov    %rax,%rdx
mov    %r14,%rcx
call   10d0 <sprintf@plt>
mov    %r15,%rdi
call   1050 <lnname@plt>
add    $0x2810,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdb6(%rip),%rdi
jmp    125f <lnname+0xaf>
mov    %rbx,%rdi
call   10c0 <filename@plt>
mov    %rax,%rdi
call   1090 <numerical_suffix@plt>
mov    %rax,%rdi
add    $0x2810,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <puts@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"1\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define void @lnname(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [10240 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @lstat(i8* %10, %struct.stat* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %49
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @S_ISLNK(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = load i8*, i8** %2
%22 = call i8* @filename(i8* %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = call i8* @numerical_suffix(i8* %23)
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = call i32 @puts(i8* %25)
br label %48
27: ; preds = %15
%28 = load i8*, i8** %2
%29 = getelementptr inbounds [10240 x i8], [10240 x i8]* %6, i64 0, i64 0
%30 = call i32 @readlink(i8* %28, i8* %29, i32 10240)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [10240 x i8], [10240 x i8]* %6, i64 0, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %2
%35 = call i64 @strlen(i8* %34)
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = add nsw i64 %35, %37
%39 = add nsw i64 %38, 1
%40 = call i8* @alloca(i64 %39)
store i8* %40, i8** %7
%41 = load i8*, i8** %2
%42 = call i8* @basename(i8* %41)
store i8* %42, i8** %8
%43 = load i8*, i8** %7
%44 = load i8*, i8** %8
%45 = getelementptr inbounds [10240 x i8], [10240 x i8]* %6, i64 0, i64 0
%46 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %44, i8* %45)
%47 = load i8*, i8** %7
br label %48
48: ; preds = %27, %20
br label %49
49: ; preds = %48, %13
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"1\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define void @lnname(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [10240 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i32 @lstat(i8* %0, %struct.stat* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %29
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @S_ISLNK(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = tail call i8* @filename(i8* %0)
%16 = tail call i8* @numerical_suffix(i8* %15)
%17 = tail call i32 @puts(i8* dereferenceable(1) %16)
br label %29
18: ; preds = %9
%19 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%20 = call i32 @readlink(i8* %0, i8* %19, i32 10240)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 %21
store i8 0, i8* %22
%23 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%24 = add nsw i64 %21, 1
%25 = add i64 %24, %23
%26 = tail call i8* @alloca(i64 %25)
%27 = tail call i8* @basename(i8* %0)
%28 = call i32 @sprintf(i8* dereferenceable(1) %26, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %19)
br label %29
29: ; preds = %14, %18, %7
ret void
}