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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int dirfd(int /*<<< orphan*/ *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int stat(char const *, struct stat *);
int open_file_or_dir(const char *fname) {
  int ret;
  struct stat st;
  DIR *dirstream;
  int fd;
  ret = stat(fname, &st);
  if (ret < 0) {
    return -1;
  }
  if (S_ISDIR(st.st_mode)) {
    dirstream = opendir(fname);
    if (!dirstream) {
      return -2;
    }
    fd = dirfd(dirstream);
  } else {
    fd = open(fname, O_RDWR);
  }
  if (fd < 0) {
    return -3;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_file_or_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1182 <open_file_or_dir+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <open_file_or_dir+0xaa>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11c9 <open_file_or_dir+0x79>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <open_file_or_dir+0x68>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fa <open_file_or_dir+0xaa>
mov    -0x20(%rbp),%rdi
call   1050 <dirfd@plt>
mov    %eax,-0x24(%rbp)
jmp    11de <open_file_or_dir+0x8e>
mov    -0x10(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f4 <open_file_or_dir+0xa4>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11fa <open_file_or_dir+0xaa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file_or_dir>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %eax,%eax
js     118b <open_file_or_dir+0x3b>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     1192 <open_file_or_dir+0x42>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11af <open_file_or_dir+0x5f>
mov    %rax,%rdi
call   1050 <dirfd@plt>
jmp    11a3 <open_file_or_dir+0x53>
mov    $0xffffffff,%ecx
jmp    11b4 <open_file_or_dir+0x64>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
mov    $0xfffffffd,%ecx
cmovns %eax,%ecx
jmp    11b4 <open_file_or_dir+0x64>
mov    $0xfffffffe,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
define i32 @open_file_or_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %5)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %37
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISDIR(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = call i32* @opendir(i8* %19)
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %18
store i32 -2, i32* %2
br label %37
24: ; preds = %18
%25 = load i32*, i32** %6
%26 = call i32 @dirfd(i32* %25)
store i32 %26, i32* %7
br label %31
27: ; preds = %13
%28 = load i8*, i8** %3
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %7
br label %31
31: ; preds = %27, %24
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 -3, i32* %2
br label %37
35: ; preds = %31
%36 = load i32, i32* %7
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %34, %23, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
define i32 @open_file_or_dir(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %6
%12 = tail call i32* @opendir(i8* %0)
%13 = icmp eq i32* %12, null
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = tail call i32 @dirfd(i32* %12)
br label %19
16: ; preds = %6
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* %0, i32 %17)
br label %19
19: ; preds = %16, %14
%20 = phi i32 [ %15, %14 ], [ %18, %16 ]
%21 = icmp slt i32 %20, 0
%22 = select i1 %21, i32 -3, i32 %20
br label %23
23: ; preds = %19, %11, %1
%24 = phi i32 [ -1, %1 ], [ -2, %11 ], [ %22, %19 ]
ret i32 %24
}