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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ asprintf(char **, char *, char *, char const *);
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char *get_nvme_ctrl_attr(char *path, const char *attr) {
  char *attrpath;
  char *value;
  int fd;
  ssize_t ret;
  int i;
  ret = asprintf(&attrpath, "%s/%s", path, attr);
  if (ret < 0)
    return NULL;
  value = calloc(1, 1024);
  if (!value)
    goto err_free_path;
  fd = open(attrpath, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s: %s\n", attrpath, strerror(errno));
    goto err_free_value;
  }
  ret = read(fd, value, 1024);
  if (ret < 0) {
    fprintf(stderr, "read :%s :%s\n", attrpath, strerror(errno));
    goto err_close_fd;
  }
  if (value[strlen(value) - 1] == '\n')
    value[strlen(value) - 1] = '\0';
  for (i = 0; i < strlen(value); i++) {
    if (value[i] == ',')
      value[i] = ' ';
  }
  close(fd);
  free(attrpath);
  return value;
err_close_fd:
  close(fd);
err_free_value:
  free(value);
err_free_path:
  free(attrpath);
  return NULL;
}


x86 assembly (clang, -O0):
<get_nvme_ctrl_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1090 <asprintf@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11d7 <get_nvme_ctrl_attr+0x47>
movq   $0x0,-0x8(%rbp)
jmp    1395 <get_nvme_ctrl_attr+0x205>
mov    $0x1,%edi
mov    $0x400,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fa <get_nvme_ctrl_attr+0x6a>
jmp    1384 <get_nvme_ctrl_attr+0x1f4>
mov    -0x20(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1256 <get_nvme_ctrl_attr+0xc6>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdba(%rip),%rsi
call   1080 <fprintf@plt>
jmp    137b <get_nvme_ctrl_attr+0x1eb>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    12b3 <get_nvme_ctrl_attr+0x123>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd74(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1373 <get_nvme_ctrl_attr+0x1e3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12fe <get_nvme_ctrl_attr+0x16e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x74(%rbp),%eax
cmp    %ecx,%eax
jge    1355 <get_nvme_ctrl_attr+0x1c5>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2c,%eax
jne    1342 <get_nvme_ctrl_attr+0x1b2>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    1347 <get_nvme_ctrl_attr+0x1b7>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1305 <get_nvme_ctrl_attr+0x175>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1395 <get_nvme_ctrl_attr+0x205>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_nvme_ctrl_attr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe57(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <asprintf@plt>
test   %rax,%rax
js     125f <get_nvme_ctrl_attr+0xcf>
mov    $0x1,%edi
mov    $0x400,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     12e4 <get_nvme_ctrl_attr+0x154>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     1270 <get_nvme_ctrl_attr+0xe0>
mov    %eax,%r12d
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
js     12a2 <get_nvme_ctrl_attr+0x112>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    122d <get_nvme_ctrl_attr+0x9d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1266 <get_nvme_ctrl_attr+0xd6>
xor    %ebp,%ebp
jmp    1253 <get_nvme_ctrl_attr+0xc3>
nopl   (%rax)
add    $0x1,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbp
jge    1266 <get_nvme_ctrl_attr+0xd6>
cmpb   $0x2c,(%rbx,%rbp,1)
jne    1240 <get_nvme_ctrl_attr+0xb0>
movb   $0x20,(%rbx,%rbp,1)
jmp    1240 <get_nvme_ctrl_attr+0xb0>
xor    %ebx,%ebx
jmp    12f0 <get_nvme_ctrl_attr+0x160>
mov    %r12d,%edi
call   1050 <close@plt>
jmp    12e6 <get_nvme_ctrl_attr+0x156>
mov    0x2d61(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rsp),%r14
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd73(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
jmp    12dc <get_nvme_ctrl_attr+0x14c>
mov    0x2d2f(%rip),%rax
mov    (%rax),%r14d
mov    0x8(%rsp),%r15
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd57(%rip),%rsi
mov    %r14d,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [14 x i8] c"read :%s :%s\0A\00"
define i8* @get_nvme_ctrl_attr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i64 @asprintf(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
store i64 %13, i64* %9
%14 = load i64, i64* %9
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %101
17: ; preds = %2
%18 = call i8* @calloc(i32 1, i32 1024)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %17
br label %98
22: ; preds = %17
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %6
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %32)
br label %95
34: ; preds = %22
%35 = load i32, i32* %8
%36 = load i8*, i8** %7
%37 = call i64 @read(i32 %35, i8* %36, i32 1024)
store i64 %37, i64* %9
%38 = load i64, i64* %9
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %6
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %44)
br label %92
46: ; preds = %34
%47 = load i8*, i8** %7
%48 = load i8*, i8** %7
%49 = call i32 @strlen(i8* %48)
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %63
56: ; preds = %46
%57 = load i8*, i8** %7
%58 = load i8*, i8** %7
%59 = call i32 @strlen(i8* %58)
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %57, i64 %61
store i8 0, i8* %62
br label %63
63: ; preds = %56, %46
store i32 0, i32* %10
br label %64
64: ; preds = %83, %63
%65 = load i32, i32* %10
%66 = load i8*, i8** %7
%67 = call i32 @strlen(i8* %66)
%68 = icmp slt i32 %65, %67
br i1 %68, label %69, label %86
69: ; preds = %64
%70 = load i8*, i8** %7
%71 = load i32, i32* %10
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %75, 44
br i1 %76, label %77, label %82
77: ; preds = %69
%78 = load i8*, i8** %7
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %78, i64 %80
store i8 32, i8* %81
br label %82
82: ; preds = %77, %69
br label %83
83: ; preds = %82
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 1
store i32 %85, i32* %10
br label %64
86: ; preds = %64
%87 = load i32, i32* %8
%88 = call i32 @close(i32 %87)
%89 = load i8*, i8** %6
%90 = call i32 @free(i8* %89)
%91 = load i8*, i8** %7
store i8* %91, i8** %3
br label %101
92: ; preds = %40
%93 = load i32, i32* %8
%94 = call i32 @close(i32 %93)
br label %95
95: ; preds = %92, %28
%96 = load i8*, i8** %7
%97 = call i32 @free(i8* %96)
br label %98
98: ; preds = %95, %21
%99 = load i8*, i8** %6
%100 = call i32 @free(i8* %99)
store i8* null, i8** %3
br label %101
101: ; preds = %98, %86, %16
%102 = load i8*, i8** %3
ret i8* %102
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [14 x i8] c"read :%s :%s\0A\00"
define i8* @get_nvme_ctrl_attr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %65, label %7
7: ; preds = %2
%8 = call dereferenceable_or_null(1024) i8* @calloc(i32 1, i32 1024)
%9 = icmp eq i8* %8, null
br i1 %9, label %61, label %10
10: ; preds = %7
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %19)
br label %59
21: ; preds = %10
%22 = call i64 @read(i32 %13, i8* %8, i32 1024)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %28)
%30 = call i32 @close(i32 %13)
br label %59
31: ; preds = %21
%32 = call i32 @strlen(i8* %8)
%33 = add nsw i32 %32, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %8, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 10
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = call i32 @strlen(i8* %8)
%40 = add nsw i32 %39, -1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %8, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %38, %31
%44 = call i32 @strlen(i8* %8)
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %43, %52
%47 = phi i64 [ %53, %52 ], [ 0, %43 ]
%48 = getelementptr inbounds i8, i8* %8, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 44
br i1 %50, label %51, label %52
51: ; preds = %46
store i8 32, i8* %48
br label %52
52: ; preds = %46, %51
%53 = add nuw nsw i64 %47, 1
%54 = call i32 @strlen(i8* %8)
%55 = sext i32 %54 to i64
%56 = icmp slt i64 %53, %55
br i1 %56, label %46, label %57
57: ; preds = %52, %43
%58 = call i32 @close(i32 %13)
br label %61
59: ; preds = %24, %15
%60 = call i32 @free(i8* %8)
br label %61
61: ; preds = %59, %7, %57
%62 = phi i8* [ %8, %57 ], [ null, %7 ], [ null, %59 ]
%63 = load i8*, i8** %3
%64 = call i32 @free(i8* %63)
br label %65
65: ; preds = %61, %2
%66 = phi i8* [ null, %2 ], [ %62, %61 ]
ret i8* %66
}