#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;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ path_target;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int PATH_MAX;
char *basename(char *);
scalar_t__ lstat(char *, struct stat *);
size_t readlink(char *, char *, int);
scalar_t__ snprintf(char *, int, char *, char const *);
char *strdup(char *);
char *sysfs_read_device_driver(const char *syspath) {
  char path[PATH_MAX];
  char path_target[PATH_MAX];
  char *driver = NULL;
  char *pbname = NULL;
  ssize_t len = 0;
  struct stat st;
  if (snprintf(path, sizeof(path), "%s/driver", syspath) < 0) {
    return NULL;
  }
  if (lstat(path, &st) < 0) {
    return NULL;
  }
  len = readlink(path, path_target, sizeof(path_target));
  if (len <= 0 || len == (ssize_t)sizeof(path_target)) {
    return NULL;
  }
  path_target[len] = '\0';
  if (*path_target == '\0') {
    return NULL;
  }
  pbname = basename(path_target);
  if (pbname != NULL && *pbname != '\0') {
    driver = strdup(pbname);
    if (driver == NULL) {
      return NULL;
    } else {
      return driver;
    }
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<sysfs_read_device_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe31(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    11f2 <sysfs_read_device_driver+0xa2>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <sysfs_read_device_driver+0x1b4>
mov    -0x68(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    121d <sysfs_read_device_driver+0xcd>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <sysfs_read_device_driver+0x1b4>
mov    -0x58(%rbp),%rsi
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rax
mov    %eax,%edx
call   1030 <readlink@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jbe    124d <sysfs_read_device_driver+0xfd>
mov    -0x60(%rbp),%rax
cmp    %rax,-0x40(%rbp)
jne    1261 <sysfs_read_device_driver+0x111>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <sysfs_read_device_driver+0x1b4>
mov    -0x58(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    128d <sysfs_read_device_driver+0x13d>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <sysfs_read_device_driver+0x1b4>
mov    -0x58(%rbp),%rdi
call   1060 <basename@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12f5 <sysfs_read_device_driver+0x1a5>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12f5 <sysfs_read_device_driver+0x1a5>
mov    -0x38(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12e1 <sysfs_read_device_driver+0x191>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <sysfs_read_device_driver+0x1b4>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <sysfs_read_device_driver+0x1b4>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_device_driver>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rcx
mov    0x2e8c(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe78(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <snprintf@plt>
test   %rax,%rax
js     11ed <sysfs_read_device_driver+0x9d>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <lstat@plt>
test   %rax,%rax
js     11ed <sysfs_read_device_driver+0x9d>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <readlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11ef <sysfs_read_device_driver+0x9f>
cmp    %r14,%rcx
je     11ef <sysfs_read_device_driver+0x9f>
movb   $0x0,(%r15,%rcx,1)
cmpb   $0x0,(%r15)
je     11ed <sysfs_read_device_driver+0x9d>
mov    %r15,%rdi
call   1060 <basename@plt>
test   %rax,%rax
je     11ed <sysfs_read_device_driver+0x9d>
cmpb   $0x0,(%rax)
je     11ed <sysfs_read_device_driver+0x9d>
mov    %rax,%rdi
call   1070 <strdup@plt>
jmp    11ef <sysfs_read_device_driver+0x9f>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [10 x i8] c"%s/driver\00"
define noalias i8* @sysfs_read_device_driver(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = bitcast %struct.stat* %2 to i8*
%8 = call i64 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %1
%11 = call i64 @lstat(i8* %5, %struct.stat* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %10
%14 = call i64 @readlink(i8* %5, i8* %6, i32 %3)
%15 = icmp eq i64 %14, 0
%16 = icmp eq i64 %14, %4
%17 = or i1 %15, %16
br i1 %17, label %30, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 0, i8* %19
%20 = load i8, i8* %6
%21 = icmp eq i8 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %18
%23 = call i8* @basename(i8* %6)
%24 = icmp eq i8* %23, null
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = load i8, i8* %23
%27 = icmp eq i8 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i8* @strdup(i8* %23)
br label %30
30: ; preds = %22, %25, %28, %18, %13, %10, %1
%31 = phi i8* [ null, %1 ], [ null, %10 ], [ null, %13 ], [ null, %18 ], [ %29, %28 ], [ null, %25 ], [ null, %22 ]
ret i8* %31
}