#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 int /*<<< orphan*/ tmpbuff;
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, char *, char *);
int readlink(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
char *strerror(int /*<<< orphan*/);
int file_isdir(char *d_name) {
  struct stat estado;
  int res = -1;
  char tmpbuff[256];
  if (stat(d_name, &estado) >= 0) {
    if (S_ISDIR(estado.st_mode)) {
      if (readlink(d_name, tmpbuff, sizeof(tmpbuff)) == -1) {
        res = 0;
      }
    }
  } else {
    printf("stat %s (%s)\n", strerror(errno), d_name);
  }
  return res;
}


x86 assembly (clang, -O0):
<file_isdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jl     11be <file_isdir+0x6e>
mov    -0x10(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b9 <file_isdir+0x69>
mov    -0x8(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
jne    11b4 <file_isdir+0x64>
movl   $0x0,-0x14(%rbp)
jmp    11b9 <file_isdir+0x69>
jmp    11df <file_isdir+0x8f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
mov    -0x8(%rbp),%rdx
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_isdir>:
push   %rbx
sub    $0x110,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1198 <file_isdir+0x48>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     11b8 <file_isdir+0x68>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x100,%edx
call   1030 <readlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
setne  %al
neg    %eax
jmp    11bd <file_isdir+0x6d>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe53(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"stat %s (%s)\0A\00"
@errno = global i32 0
define i32 @file_isdir(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca [256 x i8]
store i8* %0, i8** %2
store i32 -1, i32* %4
%6 = load i8*, i8** %2
%7 = call i64 @stat(i8* %6, %struct.stat* %3)
%8 = icmp sge i64 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i64 @S_ISDIR(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = load i8*, i8** %2
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = call i32 @readlink(i8* %15, i8* %16, i32 256)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 0, i32* %4
br label %20
20: ; preds = %19, %14
br label %21
21: ; preds = %20, %9
br label %27
22: ; preds = %1
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = load i8*, i8** %2
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25)
br label %27
27: ; preds = %22, %21
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"stat %s (%s)\0A\00"
@errno = global i32 0
define i32 @file_isdir(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [256 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp sgt i64 %6, -1
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = call i32 @readlink(i8* %0, i8* %5, i32 256)
%15 = icmp ne i32 %14, -1
%16 = sext i1 %15 to i32
br label %21
17: ; preds = %1
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %0)
br label %21
21: ; preds = %13, %8, %17
%22 = phi i32 [ -1, %8 ], [ -1, %17 ], [ %16, %13 ]
ret i32 %22
}