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

/* Variables and functions */
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int strlen(char const *);
char *xmalloc(int);
char *make_pathname_from_dirent(char const *basedir, struct dirent *ent,
                                struct stat *st_buf) {
  int name_len;
  char *name;
  name_len = strlen(basedir) + strlen("/") + strlen(ent->d_name) + 1;
  name = xmalloc(name_len);
  sprintf(name, "%s/%s", basedir, ent->d_name);
  if (stat(name, st_buf) != 0) {
    struct stat lstat_buf;
    int err = errno;
    if (lstat(name, &lstat_buf) == 0 && S_ISLNK(lstat_buf.st_mode)) {
    } else {
      fprintf(stderr, "stat failed for %s (%s)\n", name, strerror(err));
    }
    free(name);
    name = NULL;
  }
  return name;
}


x86 assembly (clang, -O0):
<make_pathname_from_dirent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x3c(%rbp)
lea    0xe49(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe01(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     129a <make_pathname_from_dirent+0x10a>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
jne    1257 <make_pathname_from_dirent+0xc7>
mov    -0x30(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1257 <make_pathname_from_dirent+0xc7>
jmp    1289 <make_pathname_from_dirent+0xf9>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd84(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_pathname_from_dirent>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%r12d
lea    0xe4c(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%rdi
call   1050 <strlen@plt>
add    %r12d,%ebx
lea    (%rax,%rbx,1),%edi
add    $0x1,%edi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%rcx
lea    0xe22(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     1254 <make_pathname_from_dirent+0xc4>
mov    0x2dd6(%rip),%rax
mov    (%rax),%r14d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <lstat@plt>
test   %rax,%rax
jne    1225 <make_pathname_from_dirent+0x95>
mov    0x8(%rsp),%edi
call   1040 <S_ISLNK@plt>
test   %rax,%rax
jne    124a <make_pathname_from_dirent+0xba>
mov    0x2db4(%rip),%rax
mov    (%rax),%ebp
mov    %r14d,%edi
call   10b0 <strerror@plt>
lea    0xdcb(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [25 x i8] c"stat failed for %s (%s)\0A\00"
define i8* @make_pathname_from_dirent(i8* %0, %struct.dirent* %1, %struct.stat* %2) {
%4 = alloca i8*
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %4
store %struct.dirent* %1, %struct.dirent** %5
store %struct.stat* %2, %struct.stat** %6
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = add nsw i32 %12, %13
%15 = load %struct.dirent*, %struct.dirent** %5
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i32 0, i32 0
%17 = load i8*, i8** %16
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %14, %18
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i8* @xmalloc(i32 %21)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i8*, i8** %4
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %27)
%29 = load i8*, i8** %8
%30 = load %struct.stat*, %struct.stat** %6
%31 = call i64 @stat(i8* %29, %struct.stat* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %53
33: ; preds = %3
%34 = load i32, i32* @errno
store i32 %34, i32* %10
%35 = load i8*, i8** %8
%36 = call i64 @lstat(i8* %35, %struct.stat* %9)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @S_ISLNK(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
br label %50
44: ; preds = %38, %33
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %8
%47 = load i32, i32* %10
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %48)
br label %50
50: ; preds = %44, %43
%51 = load i8*, i8** %8
%52 = call i32 @free(i8* %51)
store i8* null, i8** %8
br label %53
53: ; preds = %50, %3
%54 = load i8*, i8** %8
ret i8* %54
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [25 x i8] c"stat failed for %s (%s)\0A\00"
define i8* @make_pathname_from_dirent(i8* %0, %struct.dirent* nocapture %1, %struct.stat* %2) {
%4 = alloca %struct.stat
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds %struct.dirent, %struct.dirent* %1, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = tail call i32 @strlen(i8* %8)
%10 = add i32 %5, 1
%11 = add i32 %10, %6
%12 = add i32 %11, %9
%13 = tail call i8* @xmalloc(i32 %12)
%14 = load i8*, i8** %7
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %14)
%16 = tail call i64 @stat(i8* %13, %struct.stat* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %34, label %18
18: ; preds = %3
%19 = bitcast %struct.stat* %4 to i8*
%20 = load i32, i32* @errno
%21 = call i64 @lstat(i8* %13, %struct.stat* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%25 = load i32, i32* %24
%26 = tail call i64 @S_ISLNK(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23, %18
%29 = load i32, i32* @stderr
%30 = tail call i8* @strerror(i32 %20)
%31 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %30)
br label %32
32: ; preds = %23, %28
%33 = tail call i32 @free(i8* %13)
br label %34
34: ; preds = %32, %3
%35 = phi i8* [ null, %32 ], [ %13, %3 ]
ret i8* %35
}