#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*/);
scalar_t__ chdir(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int if_directory_chdir(const char *dir_name, const char *base_name) {
  char buff[516];
  struct stat a_stat;
  strcpy(buff, dir_name);
  strcat(buff, "/");
  strcat(buff, base_name);
  if (stat(buff, &a_stat) < 0)
    return 0;
  if (S_ISDIR(a_stat.st_mode)) {
    if (chdir(buff) < 0)
      return 0;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<if_directory_chdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <strcat@plt>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcat@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11bf <if_directory_chdir+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    1209 <if_directory_chdir+0xb9>
mov    -0x228(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1202 <if_directory_chdir+0xb2>
lea    -0x220(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    11f6 <if_directory_chdir+0xa6>
movl   $0x0,-0x4(%rbp)
jmp    1209 <if_directory_chdir+0xb9>
movl   $0x1,-0x4(%rbp)
jmp    1209 <if_directory_chdir+0xb9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_directory_chdir>:
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     11bd <if_directory_chdir+0x6d>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     11bd <if_directory_chdir+0x6d>
lea    0x10(%rsp),%rdi
call   1040 <chdir@plt>
mov    %rax,%rbx
shr    $0x3f,%rbx
xor    $0x1,%ebx
mov    %ebx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
define i32 @if_directory_chdir(i8* %0, i8* %1) {
%3 = alloca [516 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [516 x i8], [516 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i32 @strcpy(i8* %5, i8* %0)
%8 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @strcat(i8* %5, i8* %1)
%10 = call i64 @stat(i8* %5, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = call i64 @chdir(i8* %5)
%19 = lshr i64 %18, 63
%20 = trunc i64 %19 to i32
%21 = xor i32 %20, 1
br label %22
22: ; preds = %12, %17, %2
%23 = phi i32 [ 0, %2 ], [ %21, %17 ], [ 0, %12 ]
ret i32 %23
}