#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;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *get_name_library(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strndup(char *, int);
char *can_sym_link(char *name, char *path) {
  char name_lib[500];
  char full_path[1000];
  DIR *dir;
  struct dirent *item;
  struct stat item_stat;
  char *p;
  p = get_name_library(name);
  strcpy(name_lib, p);
  dir = opendir(path);
  if (dir == NULL)
    return NULL;
  while ((item = readdir(dir))) {
    if (item->d_name[0] == '.')
      continue;
    snprintf(full_path, 1000, "%s/%s", path, item->d_name);
    stat(full_path, &item_stat);
    if (S_ISDIR(item_stat.st_mode))
      continue;
    p = get_name_library(item->d_name);
    if (strcmp(name_lib, p) == 0) {
      closedir(dir);
      return strndup(item->d_name, 499);
    }
  }
  closedir(dir);
  return NULL;
}


x86 assembly (clang, -O0):
<can_sym_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <get_name_library@plt>
mov    %rax,-0x620(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x620(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x608(%rbp)
cmpq   $0x0,-0x608(%rbp)
jne    1201 <can_sym_link+0x61>
movq   $0x0,-0x8(%rbp)
jmp    1306 <can_sym_link+0x166>
jmp    1206 <can_sym_link+0x66>
mov    -0x608(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x610(%rbp)
cmp    $0x0,%rax
je     12f2 <can_sym_link+0x152>
mov    -0x610(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    123e <can_sym_link+0x9e>
jmp    1206 <can_sym_link+0x66>
lea    -0x600(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x610(%rbp),%rax
mov    (%rax),%r8
mov    $0x3e8,%esi
lea    0xda1(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x600(%rbp),%rdi
lea    -0x618(%rbp),%rsi
call   10a0 <stat@plt>
mov    -0x618(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1291 <can_sym_link+0xf1>
jmp    1206 <can_sym_link+0x66>
mov    -0x610(%rbp),%rax
mov    (%rax),%rdi
call   10c0 <get_name_library@plt>
mov    %rax,-0x620(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x620(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    12ed <can_sym_link+0x14d>
mov    -0x608(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x610(%rbp),%rax
mov    (%rax),%rdi
mov    $0x1f3,%esi
call   1080 <strndup@plt>
mov    %rax,-0x8(%rbp)
jmp    1306 <can_sym_link+0x166>
jmp    1206 <can_sym_link+0x66>
mov    -0x608(%rbp),%rdi
call   1070 <closedir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_sym_link>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5f8,%rsp
mov    %rsi,%r14
call   10c0 <get_name_library@plt>
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     128e <can_sym_link+0xee>
mov    %rax,%r15
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1286 <can_sym_link+0xe6>
mov    %rax,%rbx
lea    0xe0b(%rip),%r12
lea    0x210(%rsp),%r13
lea    0x8(%rsp),%rbp
jmp    1220 <can_sym_link+0x80>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1286 <can_sym_link+0xe6>
mov    (%rbx),%r8
cmpb   $0x2e,(%r8)
je     1210 <can_sym_link+0x70>
mov    %r13,%rdi
mov    $0x3e8,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10a0 <stat@plt>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
jne    1210 <can_sym_link+0x70>
mov    (%rbx),%rdi
call   10c0 <get_name_library@plt>
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1210 <can_sym_link+0x70>
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    (%rbx),%rdi
mov    $0x1f3,%esi
call   1080 <strndup@plt>
jmp    1290 <can_sym_link+0xf0>
mov    %r15,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x5f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
define i8* @can_sym_link(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [500 x i8]
%7 = alloca [1000 x i8]
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca %struct.stat
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = call i8* @get_name_library(i8* %12)
store i8* %13, i8** %11
%14 = getelementptr inbounds [500 x i8], [500 x i8]* %6, i64 0, i64 0
%15 = load i8*, i8** %11
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = load i8*, i8** %5
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %2
store i8* null, i8** %3
br label %70
22: ; preds = %2
br label %23
23: ; preds = %66, %49, %35, %22
%24 = load i32*, i32** %8
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %9
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %67
27: ; preds = %23
%28 = load %struct.dirent*, %struct.dirent** %9
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 46
br i1 %34, label %35, label %36
35: ; preds = %27
br label %23
36: ; preds = %27
%37 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%38 = load i8*, i8** %5
%39 = load %struct.dirent*, %struct.dirent** %9
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %37, i32 1000, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %38, i8* %41)
%43 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%44 = call i32 @stat(i8* %43, %struct.stat* %10)
%45 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i64 @S_ISDIR(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %36
br label %23
50: ; preds = %36
%51 = load %struct.dirent*, %struct.dirent** %9
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i8* @get_name_library(i8* %53)
store i8* %54, i8** %11
%55 = getelementptr inbounds [500 x i8], [500 x i8]* %6, i64 0, i64 0
%56 = load i8*, i8** %11
%57 = call i64 @strcmp(i8* %55, i8* %56)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %50
%60 = load i32*, i32** %8
%61 = call i32 @closedir(i32* %60)
%62 = load %struct.dirent*, %struct.dirent** %9
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i8* @strndup(i8* %64, i32 499)
store i8* %65, i8** %3
br label %70
66: ; preds = %50
br label %23
67: ; preds = %23
%68 = load i32*, i32** %8
%69 = call i32 @closedir(i32* %68)
store i8* null, i8** %3
br label %70
70: ; preds = %67, %59, %21
%71 = load i8*, i8** %3
ret i8* %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define noalias i8* @can_sym_link(i8* %0, i8* %1) {
%3 = alloca [500 x i8]
%4 = alloca [1000 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.stat* %5 to i8*
%9 = tail call i8* @get_name_library(i8* %0)
%10 = call i32 @strcpy(i8* %6, i8* %9)
%11 = call i32* @opendir(i8* %1)
%12 = icmp eq i32* %11, null
br i1 %12, label %44, label %13
13: ; preds = %2
%14 = call %struct.dirent* @readdir(i32* %11)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %42, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %18
18: ; preds = %16, %24
%19 = phi %struct.dirent* [ %14, %16 ], [ %25, %24 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 46
br i1 %23, label %24, label %27
24: ; preds = %18, %27, %33
%25 = call %struct.dirent* @readdir(i32* %11)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %42, label %18
27: ; preds = %18
%28 = call i32 @snprintf(i8* %7, i32 1000, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %21)
%29 = call i32 @stat(i8* %7, %struct.stat* %5)
%30 = load i32, i32* %17
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %24
33: ; preds = %27
%34 = load i8*, i8** %20
%35 = call i8* @get_name_library(i8* %34)
%36 = call i64 @strcmp(i8* %6, i8* %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %24
38: ; preds = %33
%39 = call i32 @closedir(i32* %11)
%40 = load i8*, i8** %20
%41 = call i8* @strndup(i8* %40, i32 499)
br label %44
42: ; preds = %24, %13
%43 = call i32 @closedir(i32* %11)
br label %44
44: ; preds = %2, %42, %38
%45 = phi i8* [ %41, %38 ], [ null, %42 ], [ null, %2 ]
ret i8* %45
}