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

/* Variables and functions */
int PATH_MAX;
struct stat *malloc(int);
int /*<<< orphan*/ memcpy(struct stat *, struct stat *, int);
size_t snprintf(char *, int, char *, char const *, char const *);
scalar_t__ stat(char *, struct stat *);
struct stat *sysfs_read_stats(const char *syspath, const char *attr) {
  char path[PATH_MAX];
  struct stat statbuf;
  struct stat *sp = NULL;
  ssize_t len = 0;
  len = snprintf(path, sizeof(path), "%s/%s", syspath, attr);
  if (len <= 0 || len == (ssize_t)sizeof(path)) {
    return NULL;
  }
  path[len] = '\0';
  if (stat(path, &statbuf) < 0) {
    return NULL;
  } else {
    sp = malloc(sizeof(struct stat));
    if (sp == NULL) {
      return NULL;
    }
    memcpy(sp, &statbuf, sizeof(struct stat));
  }
  return (sp);
}


x86 assembly (clang, -O0):
<sysfs_read_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe5f(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jbe    11c3 <sysfs_read_stats+0x83>
mov    -0x58(%rbp),%rax
cmp    %rax,-0x40(%rbp)
jne    11d7 <sysfs_read_stats+0x97>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1258 <sysfs_read_stats+0x118>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    120a <sysfs_read_stats+0xca>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1258 <sysfs_read_stats+0x118>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1237 <sysfs_read_stats+0xf7>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1258 <sysfs_read_stats+0x118>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_stats>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2e99(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe8e(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <snprintf@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11c7 <sysfs_read_stats+0x87>
cmp    %r15,%rax
je     11c7 <sysfs_read_stats+0x87>
movb   $0x0,(%rbx,%rax,1)
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     11c4 <sysfs_read_stats+0x84>
mov    $0x4,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c4 <sysfs_read_stats+0x84>
mov    %rax,%r14
lea    -0x20(%rbp),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
jmp    11c7 <sysfs_read_stats+0x87>
xor    %r14d,%r14d
mov    %r14,%rax
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 = [6 x i8] c"%s/%s\00"
define %struct.stat* @sysfs_read_stats(i8* %0, i8* %1) {
%3 = alloca %struct.stat*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca %struct.stat*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
store %struct.stat* null, %struct.stat** %9
store i64 0, i64* %10
%16 = trunc i64 %13 to i32
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i64 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
store i64 %19, i64* %10
%20 = load i64, i64* %10
%21 = icmp ule i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %2
%23 = load i64, i64* %10
%24 = icmp eq i64 %23, %13
br i1 %24, label %25, label %26
25: ; preds = %22, %2
store %struct.stat* null, %struct.stat** %3
store i32 1, i32* %11
br label %42
26: ; preds = %22
%27 = load i64, i64* %10
%28 = getelementptr inbounds i8, i8* %15, i64 %27
store i8 0, i8* %28
%29 = call i64 @stat(i8* %15, %struct.stat* %8)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store %struct.stat* null, %struct.stat** %3
store i32 1, i32* %11
br label %42
32: ; preds = %26
%33 = call %struct.stat* @malloc(i32 4)
store %struct.stat* %33, %struct.stat** %9
%34 = load %struct.stat*, %struct.stat** %9
%35 = icmp eq %struct.stat* %34, null
br i1 %35, label %36, label %37
36: ; preds = %32
store %struct.stat* null, %struct.stat** %3
store i32 1, i32* %11
br label %42
37: ; preds = %32
%38 = load %struct.stat*, %struct.stat** %9
%39 = call i32 @memcpy(%struct.stat* %38, %struct.stat* %8, i32 4)
br label %40
40: ; preds = %37
%41 = load %struct.stat*, %struct.stat** %9
store %struct.stat* %41, %struct.stat** %3
store i32 1, i32* %11
br label %42
42: ; preds = %40, %36, %31, %25
%43 = load i8*, i8** %6
%44 = load %struct.stat*, %struct.stat** %3
ret %struct.stat* %44
}

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