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

/* Variables and functions */
int S_IREAD;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
int stat(char *, struct stat *);
char *wholeName(char *, char *);
DIR *fsel_opendir(char *prefix, char *dname) {
  DIR *dfd;
  struct stat s_buf;
  if (prefix != NULL && *prefix != '\0') {
    char *name = wholeName(prefix, dname);
    if (stat(name, &s_buf) == -1 || !(s_buf.st_mode & S_IREAD)) {
      return NULL;
    }
    dfd = opendir(name);
    free(name);
  } else {
    if (stat(dname, &s_buf) == -1 || !(s_buf.st_mode & S_IREAD)) {
      return NULL;
    }
    dfd = opendir(dname);
  }
  return dfd;
}


x86 assembly (clang, -O0):
<fsel_opendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cf <fsel_opendir+0x8f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11cf <fsel_opendir+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <wholeName@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11a7 <fsel_opendir+0x67>
mov    -0x28(%rbp),%eax
mov    0x2e44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11b4 <fsel_opendir+0x74>
movq   $0x0,-0x8(%rbp)
jmp    121c <fsel_opendir+0xdc>
mov    -0x30(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1214 <fsel_opendir+0xd4>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11fa <fsel_opendir+0xba>
mov    -0x28(%rbp),%eax
mov    0x2df1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1207 <fsel_opendir+0xc7>
movq   $0x0,-0x8(%rbp)
jmp    121c <fsel_opendir+0xdc>
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsel_opendir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
test   %rdi,%rdi
je     1192 <fsel_opendir+0x52>
cmpb   $0x0,(%rdi)
je     1192 <fsel_opendir+0x52>
mov    %rbx,%rsi
call   1060 <wholeName@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11bf <fsel_opendir+0x7f>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
je     11bf <fsel_opendir+0x7f>
mov    %rbx,%rdi
call   1040 <opendir@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
jmp    11c1 <fsel_opendir+0x81>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11bf <fsel_opendir+0x7f>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
je     11bf <fsel_opendir+0x7f>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <opendir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IREAD = global i32 0
define i32* @fsel_opendir(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %35
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i8* @wholeName(i8* %17, i8* %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = call i32 @stat(i8* %20, %struct.stat* %7)
%22 = icmp eq i32 %21, -1
br i1 %22, label %29, label %23
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @S_IREAD
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %23, %16
store i32* null, i32** %3
br label %51
30: ; preds = %23
%31 = load i8*, i8** %8
%32 = call i32* @opendir(i8* %31)
store i32* %32, i32** %6
%33 = load i8*, i8** %8
%34 = call i32 @free(i8* %33)
br label %49
35: ; preds = %11, %2
%36 = load i8*, i8** %5
%37 = call i32 @stat(i8* %36, %struct.stat* %7)
%38 = icmp eq i32 %37, -1
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i32, i32* @S_IREAD
%43 = and i32 %41, %42
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %39, %35
store i32* null, i32** %3
br label %51
46: ; preds = %39
%47 = load i8*, i8** %5
%48 = call i32* @opendir(i8* %47)
store i32* %48, i32** %6
br label %49
49: ; preds = %46, %30
%50 = load i32*, i32** %6
store i32* %50, i32** %3
br label %51
51: ; preds = %49, %45, %29
%52 = load i32*, i32** %3
ret i32* %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IREAD = global i32 0
define noalias i32* @fsel_opendir(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %6
%10 = tail call i8* @wholeName(i8* %0, i8* %1)
%11 = call i32 @stat(i8* %10, %struct.stat* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %33, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @S_IREAD
%17 = and i32 %16, %15
%18 = icmp eq i32 %17, 0
br i1 %18, label %33, label %19
19: ; preds = %13
%20 = tail call i32* @opendir(i8* %10)
%21 = tail call i32 @free(i8* %10)
br label %33
22: ; preds = %6, %2
%23 = call i32 @stat(i8* %1, %struct.stat* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %33, label %25
25: ; preds = %22
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @S_IREAD
%29 = and i32 %28, %27
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = tail call i32* @opendir(i8* %1)
br label %33
33: ; preds = %9, %13, %31, %19, %22, %25
%34 = phi i32* [ null, %25 ], [ null, %22 ], [ %20, %19 ], [ %32, %31 ], [ null, %13 ], [ null, %9 ]
ret i32* %34
}