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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int chdir(char const *);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
char *enter_dirfile(const char *dirfilename) {
  struct stat st;
  int status = stat(dirfilename, &st);
  if (status != 0 || S_ISDIR(st.st_mode) == 0)
    return NULL;
  char *cwd = getcwd(NULL, 0);
  if (cwd == NULL)
    return NULL;
  status = chdir(dirfilename);
  if (status != 0) {
    free(cwd);
    return NULL;
  }
  return cwd;
}


x86 assembly (clang, -O0):
<enter_dirfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1188 <enter_dirfile+0x38>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1195 <enter_dirfile+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <enter_dirfile+0xa0>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bc <enter_dirfile+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <enter_dirfile+0xa0>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11e8 <enter_dirfile+0x98>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <enter_dirfile+0xa0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enter_dirfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %eax,%eax
je     1179 <enter_dirfile+0x29>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     116a <enter_dirfile+0x1a>
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     116c <enter_dirfile+0x1c>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <chdir@plt>
test   %eax,%eax
je     11b0 <enter_dirfile+0x60>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    116a <enter_dirfile+0x1a>
mov    %r15,%rbx
jmp    116c <enter_dirfile+0x1c>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
define i8* @enter_dirfile(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %19
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 %19, label %11
11: ; preds = %6
%12 = tail call i8* @getcwd(i32* null, i32 0)
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i32 @chdir(i8* %0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @free(i8* %12)
br label %19
19: ; preds = %17, %11, %14, %1, %6
%20 = phi i8* [ null, %6 ], [ null, %1 ], [ null, %17 ], [ null, %11 ], [ %12, %14 ]
ret i8* %20
}