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

/* Variables and functions */
scalar_t__ ENODEV;
scalar_t__ ENOENT;
int asprintf(char **, char *, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
char *sysfs_get_path(char *path, char *attr_name) {
  char *attr_path = NULL;
  char *new_path;
  struct stat sb;
  path = strdup(path);
  if (path == NULL)
    return NULL;
  while (stat(path, &sb) != -1) {
    if (asprintf(&attr_path, "%s/%s", path, attr_name) == -1)
      goto out;
    if (stat(attr_path, &sb) == 0) {
      free(path);
      return attr_path;
    }
    if (errno != ENOENT)
      goto enodev;
    if (asprintf(&new_path, "%s/device", path) == -1)
      goto out;
    free(path);
    path = new_path;
    free(attr_path);
  }
enodev:
  errno = ENODEV;
out:
  if (attr_path)
    free(attr_path);
  free(path);
  return NULL;
}


x86 assembly (clang, -O0):
<sysfs_get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117d <sysfs_get_path+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    128e <sysfs_get_path+0x14e>
jmp    1182 <sysfs_get_path+0x42>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffffffffffff,%rax
je     1250 <sysfs_get_path+0x110>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11c1 <sysfs_get_path+0x81>
jmp    1269 <sysfs_get_path+0x129>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    11ee <sysfs_get_path+0xae>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    128e <sysfs_get_path+0x14e>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
je     120d <sysfs_get_path+0xcd>
jmp    1255 <sysfs_get_path+0x115>
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1231 <sysfs_get_path+0xf1>
jmp    1269 <sysfs_get_path+0x129>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1182 <sysfs_get_path+0x42>
jmp    1255 <sysfs_get_path+0x115>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rcx
mov    0x2d7a(%rip),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     127d <sysfs_get_path+0x13d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_get_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
movq   $0x0,(%rsp)
call   1060 <strdup@plt>
test   %rax,%rax
je     1255 <sysfs_get_path+0x115>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1040 <stat@plt>
cmp    $0xffffffffffffffff,%rax
je     122b <sysfs_get_path+0xeb>
lea    0xe78(%rip),%r15
mov    %rsp,%r12
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
je     123f <sysfs_get_path+0xff>
mov    (%rsp),%rdi
mov    %r13,%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     1266 <sysfs_get_path+0x126>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    122b <sysfs_get_path+0xeb>
mov    %rbp,%rdi
lea    0xe15(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
je     123f <sysfs_get_path+0xff>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <stat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11a0 <sysfs_get_path+0x60>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    0x2da4(%rip),%rcx
mov    %rax,(%rcx)
mov    (%rsp),%rdi
test   %rdi,%rdi
je     124d <sysfs_get_path+0x10d>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
jmp    1257 <sysfs_get_path+0x117>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [10 x i8] c"%s/device\00"
@ENODEV = global i64 0
define i8* @sysfs_get_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%9 = load i8*, i8** %4
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %61
14: ; preds = %2
br label %15
15: ; preds = %43, %14
%16 = load i8*, i8** %4
%17 = call i64 @stat(i8* %16, %struct.stat* %8)
%18 = icmp ne i64 %17, -1
br i1 %18, label %19, label %49
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i8*, i8** %5
%22 = call i32 (i8**, i8*, i8*, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %19
br label %52
25: ; preds = %19
%26 = load i8*, i8** %6
%27 = call i64 @stat(i8* %26, %struct.stat* %8)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = call i32 @free(i8* %30)
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %61
33: ; preds = %25
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %50
38: ; preds = %33
%39 = load i8*, i8** %4
%40 = call i32 (i8**, i8*, i8*, ...) @asprintf(i8** %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %38
br label %52
43: ; preds = %38
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %7
store i8* %46, i8** %4
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
br label %15
49: ; preds = %15
br label %50
50: ; preds = %49, %37
%51 = load i64, i64* @ENODEV
store i64 %51, i64* @errno
br label %52
52: ; preds = %50, %42, %24
%53 = load i8*, i8** %6
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
br label %58
58: ; preds = %55, %52
%59 = load i8*, i8** %4
%60 = call i32 @free(i8* %59)
store i8* null, i8** %3
br label %61
61: ; preds = %58, %29, %13
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [10 x i8] c"%s/device\00"
@ENODEV = global i64 0
define i8* @sysfs_get_path(i8* nocapture %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = bitcast i8** %4 to i8*
%8 = bitcast %struct.stat* %5 to i8*
%9 = tail call i8* @strdup(i8* %0)
%10 = icmp eq i8* %9, null
br i1 %10, label %50, label %11
11: ; preds = %2
%12 = call i64 @stat(i8* %9, %struct.stat* %5)
%13 = icmp eq i64 %12, -1
br i1 %13, label %39, label %14
14: ; preds = %11, %32
%15 = phi i8* [ %34, %32 ], [ %9, %11 ]
%16 = call i32 (i8**, i8*, i8*, ...) @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %42, label %18
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = call i64 @stat(i8* %19, %struct.stat* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @free(i8* %15)
%24 = load i8*, i8** %3
br label %50
25: ; preds = %18
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENOENT
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %25
%30 = call i32 (i8**, i8*, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%31 = icmp eq i32 %30, -1
br i1 %31, label %42, label %32
32: ; preds = %29
%33 = call i32 @free(i8* %15)
%34 = load i8*, i8** %4
%35 = load i8*, i8** %3
%36 = call i32 @free(i8* %35)
%37 = call i64 @stat(i8* %34, %struct.stat* %5)
%38 = icmp eq i64 %37, -1
br i1 %38, label %39, label %14
39: ; preds = %25, %32, %11
%40 = phi i8* [ %9, %11 ], [ %34, %32 ], [ %15, %25 ]
%41 = load i64, i64* @ENODEV
store i64 %41, i64* @errno
br label %42
42: ; preds = %29, %14, %39
%43 = phi i8* [ %40, %39 ], [ %15, %14 ], [ %15, %29 ]
%44 = load i8*, i8** %3
%45 = icmp eq i8* %44, null
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = call i32 @free(i8* %44)
br label %48
48: ; preds = %46, %42
%49 = call i32 @free(i8* %43)
br label %50
50: ; preds = %2, %48, %22
%51 = phi i8* [ null, %48 ], [ %24, %22 ], [ null, %2 ]
ret i8* %51
}