#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 /*<<< orphan*/ free(char *);
scalar_t__ stat(char *, struct stat *);
char *util_path_sysfs(char *, int, int, int);
char *get_chp_dir(int css, int id) {
  struct stat sb;
  char *path;
  path = util_path_sysfs("devices/css%x/chp%x.%x", css, css, id);
  if ((stat(path, &sb) == 0) && S_ISDIR(sb.st_mode))
    return path;
  free(path);
  return util_path_sysfs("devices/css%x/chp%x.%02x", css, css, id);
}


x86 assembly (clang, -O0):
<get_chp_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xea2(%rip),%rdi
call   1060 <util_path_sysfs@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    119d <get_chp_dir+0x5d>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     119d <get_chp_dir+0x5d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bf <get_chp_dir+0x7f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe61(%rip),%rdi
call   1060 <util_path_sysfs@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_chp_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xeac(%rip),%rdi
mov    %ebp,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1060 <util_path_sysfs@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    1183 <get_chp_dir+0x43>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
jne    11a1 <get_chp_dir+0x61>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe85(%rip),%rdi
mov    %ebp,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1060 <util_path_sysfs@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [23 x i8] c"devices/css%x/chp%x.%x\00"
@.str.1 = [25 x i8] c"devices/css%x/chp%x.%02x\00"
define i8* @get_chp_dir(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i8* @util_path_sysfs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %0, i32 %1)
%6 = call i64 @stat(i8* %5, %struct.stat* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8, %2
%14 = call i32 @free(i8* %5)
%15 = call i8* @util_path_sysfs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %0, i32 %1)
br label %16
16: ; preds = %8, %13
%17 = phi i8* [ %15, %13 ], [ %5, %8 ]
ret i8* %17
}