#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 */
int /*<<< orphan*/ MAXPATHLEN;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int chdir(char const *);
int /*<<< orphan*/ *getcwd(char *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ warn(char *, char const *);
char *chdir_verify_path(const char *path, char *obpath) {
  struct stat sb;
  if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
    if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
      warn("warning: %s", path);
      return (NULL);
    }
    return (obpath);
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<chdir_verify_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11e1 <chdir_verify_path+0x91>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11e1 <chdir_verify_path+0x91>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     11b7 <chdir_verify_path+0x67>
mov    -0x18(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
jne    11d4 <chdir_verify_path+0x84>
mov    -0x10(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <chdir_verify_path+0x99>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <chdir_verify_path+0x99>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chdir_verify_path>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     1174 <chdir_verify_path+0x24>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     1167 <chdir_verify_path+0x17>
mov    %r14,%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     11a4 <chdir_verify_path+0x54>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <getcwd@plt>
test   %rax,%rax
jne    1169 <chdir_verify_path+0x19>
lea    0xe55(%rip),%rdi
mov    %r14,%rsi
call   1030 <warn@plt>
jmp    1167 <chdir_verify_path+0x17>

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

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