#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*/ path;

/* Variables and functions */
int PATH_MAX;
char *readlink_internal(char *);
scalar_t__ snprintf(char *, int, char *, char const *, ...);
scalar_t__ strlen(char const *);
char *sysfs_read_link(const char *syspath, const char *attr) {
  char path[PATH_MAX];
  if (!syspath) {
    return NULL;
  }
  if (!attr) {
    attr = "";
  }
  if (strlen(attr) == 0) {
    if (snprintf(path, sizeof(path), "%s", syspath) < 0) {
      return NULL;
    }
  } else {
    if (snprintf(path, sizeof(path), "%s/%s", syspath, attr) < 0) {
      return NULL;
    }
  }
  return readlink_internal(path);
}


x86 assembly (clang, -O0):
<sysfs_read_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118f <sysfs_read_link+0x5f>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    124e <sysfs_read_link+0x11e>
cmpq   $0x0,-0x18(%rbp)
jne    11a5 <sysfs_read_link+0x75>
lea    0xe64(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    11f7 <sysfs_read_link+0xc7>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe36(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    11f2 <sysfs_read_link+0xc2>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    124e <sysfs_read_link+0x11e>
jmp    123a <sysfs_read_link+0x10a>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xdf0(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    1235 <sysfs_read_link+0x105>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    124e <sysfs_read_link+0x11e>
jmp    123a <sysfs_read_link+0x10a>
mov    -0x38(%rbp),%rdi
call   1050 <readlink_internal@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_link>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%rax
mov    0x2ebf(%rip),%rdx
mov    (%rdx),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     117b <sysfs_read_link+0x5b>
mov    %rdi,%rcx
test   %rax,%rax
lea    0xeaf(%rip),%r8
cmovne %rax,%r8
cmpb   $0x0,(%r8)
je     1180 <sysfs_read_link+0x60>
lea    0xe99(%rip),%rdx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <snprintf@plt>
test   %rax,%rax
jns    1199 <sysfs_read_link+0x79>
jmp    11a4 <sysfs_read_link+0x84>
xor    %r14d,%r14d
jmp    11a4 <sysfs_read_link+0x84>
lea    0xe7c(%rip),%rdx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <snprintf@plt>
test   %rax,%rax
js     11a4 <sysfs_read_link+0x84>
mov    %rbx,%rdi
call   1040 <readlink_internal@plt>
mov    %rax,%r14
mov    %r14,%rax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @sysfs_read_link(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %8
br label %41
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %5
br label %20
20: ; preds = %19, %16
%21 = load i8*, i8** %5
%22 = call i64 @strlen(i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = trunc i64 %10 to i32
%26 = load i8*, i8** %4
%27 = call i64 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %12, i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i8* null, i8** %3
store i32 1, i32* %8
br label %41
30: ; preds = %24
br label %39
31: ; preds = %20
%32 = trunc i64 %10 to i32
%33 = load i8*, i8** %4
%34 = load i8*, i8** %5
%35 = call i64 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %12, i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
store i8* null, i8** %3
store i32 1, i32* %8
br label %41
38: ; preds = %31
br label %39
39: ; preds = %38, %30
%40 = call i8* @readlink_internal(i8* %12)
store i8* %40, i8** %3
store i32 1, i32* %8
br label %41
41: ; preds = %39, %37, %29, %15
%42 = load i8*, i8** %6
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @sysfs_read_link(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i8* %0, null
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = icmp eq i8* %1, null
%9 = select i1 %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = call i64 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %20, label %18
15: ; preds = %7
%16 = call i64 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %9)
%17 = icmp slt i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15, %12
%19 = call i8* @readlink_internal(i8* %5)
br label %20
20: ; preds = %15, %12, %2, %18
%21 = phi i8* [ %19, %18 ], [ null, %2 ], [ null, %12 ], [ null, %15 ]
ret i8* %21
}