#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 */

/* Variables and functions */
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ ENOTDIR;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fd2path(int, char *);
int open(char *, int);
char *strrchr(char *, char);
int openparent(int fd, int oflag) {
  char fullpath[PATH_MAX + 1];
  if (!fd2path(fd, fullpath)) {
    errno = ENOENT;
    return -1;
  }
  char *filename = strrchr(fullpath, '/');
  assert(filename != NULL);
  if (fullpath == filename) {
    errno = ENOTDIR;
    return -1;
  }
  filename[0] = '\0';
  return open(fullpath, oflag);
}


x86 assembly (clang, -O0):
<openparent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <fd2path@plt>
cmp    $0x0,%eax
jne    11b3 <openparent+0x73>
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e32(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1227 <openparent+0xe7>
mov    -0x38(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    120a <openparent+0xca>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1227 <openparent+0xe7>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xc(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openparent>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    0x2e94(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1050 <fd2path@plt>
test   %eax,%eax
je     11aa <openparent+0x6a>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
cmp    %r15,%rbx
je     11b3 <openparent+0x73>
movb   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <open@plt>
jmp    11ca <openparent+0x8a>
mov    0x2e0f(%rip),%rax
jmp    11ba <openparent+0x7a>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
@ENOTDIR = global i32 0
define i32 @openparent(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* @PATH_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i32, i32* %4
%16 = call i32 @fd2path(i32 %15, i8* %14)
%17 = icmp ne i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %2
%19 = load i32, i32* @ENOENT
store i32 %19, i32* @errno
store i32 -1, i32* %3
store i32 1, i32* %8
br label %35
20: ; preds = %2
%21 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = icmp ne i8* %22, null
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i8*, i8** %9
%27 = icmp eq i8* %14, %26
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = load i32, i32* @ENOTDIR
store i32 %29, i32* @errno
store i32 -1, i32* %3
store i32 1, i32* %8
br label %35
30: ; preds = %20
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 0, i8* %32
%33 = load i32, i32* %5
%34 = call i32 @open(i8* %14, i32 %33)
store i32 %34, i32* %3
store i32 1, i32* %8
br label %35
35: ; preds = %30, %28, %18
%36 = load i8*, i8** %6
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
@ENOTDIR = global i32 0
define i32 @openparent(i32 %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @fd2path(i32 %0, i8* %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @ENOENT
store i32 %10, i32* @errno
br label %21
11: ; preds = %2
%12 = call i8* @strrchr(i8* %6, i8 signext 47)
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = icmp eq i8* %6, %12
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i32, i32* @ENOTDIR
store i32 %18, i32* @errno
br label %21
19: ; preds = %11
store i8 0, i8* %12
%20 = call i32 @open(i8* %6, i32 %1)
br label %21
21: ; preds = %17, %19, %9
%22 = phi i32 [ -1, %9 ], [ -1, %17 ], [ %20, %19 ]
ret i32 %22
}