#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 */
typedef int /*<<< orphan*/ subsystem_link;

/* Variables and functions */
int asprintf(char **, char *, char const *);
char *basename(char *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int readlink(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int sysfs_subsystem(char **bufp, const char *path) {
  char *subsystem_path, *name, *buf;
  char subsystem_link[256];
  int len;
  int rc = -1;
  if ((asprintf(&subsystem_path, "%s/subsystem", path)) == -1)
    return -1;
  if ((len = readlink(subsystem_path, subsystem_link,
                      sizeof(subsystem_link) - 1)) == -1)
    goto out;
  if (len >= sizeof(subsystem_link) - 1)
    goto out;
  subsystem_link[len] = '\0';
  name = basename(subsystem_link);
  if (!(buf = malloc(strlen(name) + 1)))
    goto out;
  strcpy(buf, name);
  *bufp = buf;
  rc = 0;
out:
  free(subsystem_path);
  return rc;
}


x86 assembly (clang, -O0):
<sysfs_subsystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x138(%rbp)
ff ff ff
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1090 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11b6 <sysfs_subsystem+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <sysfs_subsystem+0x102>
mov    -0x20(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x134(%rbp)
cmp    $0xffffffff,%eax
jne    11df <sysfs_subsystem+0x6f>
jmp    1260 <sysfs_subsystem+0xf0>
movslq -0x134(%rbp),%rax
cmp    $0xff,%rax
jb     11f7 <sysfs_subsystem+0x87>
jmp    1260 <sysfs_subsystem+0xf0>
movslq -0x134(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x130(%rbp),%rdi
call   1070 <basename@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    123e <sysfs_subsystem+0xce>
jmp    1260 <sysfs_subsystem+0xf0>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x138(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x138(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_subsystem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rdx
mov    %rdi,%r14
lea    0xe76(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1202 <sysfs_subsystem+0x92>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    $0xffffffff,%ebp
cmp    $0xfe,%eax
ja     11f6 <sysfs_subsystem+0x86>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1070 <basename@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11f6 <sysfs_subsystem+0x86>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbx,(%r14)
xor    %ebp,%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1207 <sysfs_subsystem+0x97>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%s/subsystem\00"
define i32 @sysfs_subsystem(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 -1, i32* %11
%12 = load i8*, i8** %5
%13 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %49
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%19 = call i32 @readlink(i8* %17, i8* %18, i32 255)
store i32 %19, i32* %10
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %16
br label %45
22: ; preds = %16
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = icmp uge i64 %24, 255
br i1 %25, label %26, label %27
26: ; preds = %22
br label %45
27: ; preds = %22
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%32 = call i8* @basename(i8* %31)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = call i8* @malloc(i64 %35)
store i8* %36, i8** %8
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %27
br label %45
39: ; preds = %27
%40 = load i8*, i8** %8
%41 = load i8*, i8** %7
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = load i8*, i8** %8
%44 = load i8**, i8*** %4
store i8* %43, i8** %44
store i32 0, i32* %11
br label %45
45: ; preds = %39, %38, %26, %21
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* %11
store i32 %48, i32* %3
br label %49
49: ; preds = %45, %15
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s/subsystem\00"
define i32 @sysfs_subsystem(i8** nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = bitcast i8** %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %27, label %9
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = call i32 @readlink(i8* %10, i8* %6, i32 255)
%12 = icmp ugt i32 %11, 254
br i1 %12, label %23, label %13
13: ; preds = %9
%14 = zext i32 %11 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i8* @basename(i8* %6)
%17 = call i64 @strlen(i8* dereferenceable(1) %16)
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %13
%22 = call i32 @strcpy(i8* %19, i8* %16)
store i8* %19, i8** %0
br label %23
23: ; preds = %13, %9, %21
%24 = phi i32 [ -1, %9 ], [ 0, %21 ], [ -1, %13 ]
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %2, %23
%28 = phi i32 [ %24, %23 ], [ -1, %2 ]
ret i32 %28
}