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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
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*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char *get_nvme_subsnqn(char *path) {
  char sspath[320];
  char *subsysnqn;
  int fd;
  int ret;
  snprintf(sspath, sizeof(sspath), "%s/subsysnqn", path);
  fd = open(sspath, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s: %s\n", sspath, strerror(errno));
    return NULL;
  }
  subsysnqn = calloc(1, 256);
  if (!subsysnqn)
    goto close_fd;
  ret = read(fd, subsysnqn, 256);
  if (ret < 0) {
    fprintf(stderr, "Failed to read %s: %s\n", sspath, strerror(errno));
    free(subsysnqn);
    subsysnqn = NULL;
  } else if (subsysnqn[strlen(subsysnqn) - 1] == '\n') {
    subsysnqn[strlen(subsysnqn) - 1] = '\0';
  }
close_fd:
  close(fd);
  return subsysnqn;
}


x86 assembly (clang, -O0):
<get_nvme_subsnqn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x150(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x140,%esi
lea    0xe4a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x150(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x15c(%rbp)
cmpl   $0x0,-0x15c(%rbp)
jge    1237 <get_nvme_subsnqn+0xa7>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x16c(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x168(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x16c(%rbp),%edi
mov    -0x168(%rbp),%rdx
mov    %rax,%rcx
lea    0xde8(%rip),%rsi
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1370 <get_nvme_subsnqn+0x1e0>
mov    $0x1,%edi
mov    $0x100,%esi
call   1080 <calloc@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
jne    1260 <get_nvme_subsnqn+0xd0>
jmp    135a <get_nvme_subsnqn+0x1ca>
mov    -0x15c(%rbp),%edi
mov    -0x158(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,-0x160(%rbp)
cmpl   $0x0,-0x160(%rbp)
jge    12ed <get_nvme_subsnqn+0x15d>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x17c(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x178(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x17c(%rbp),%edi
mov    -0x178(%rbp),%rdx
mov    %rax,%rcx
lea    0xd58(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x158(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x158(%rbp)
jmp    1355 <get_nvme_subsnqn+0x1c5>
mov    -0x158(%rbp),%rax
mov    %rax,-0x188(%rbp)
mov    -0x158(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x188(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1350 <get_nvme_subsnqn+0x1c0>
mov    -0x158(%rbp),%rax
mov    %rax,-0x190(%rbp)
mov    -0x158(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x190(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1355 <get_nvme_subsnqn+0x1c5>
jmp    135a <get_nvme_subsnqn+0x1ca>
mov    -0x15c(%rbp),%edi
call   1060 <close@plt>
mov    -0x158(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_nvme_subsnqn>:
push   %rbp
push   %r14
push   %rbx
sub    $0x140,%rsp
mov    %rdi,%rcx
lea    0xe5b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x140,%esi
call   1050 <snprintf@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     121c <get_nvme_subsnqn+0x8c>
mov    %eax,%ebp
mov    $0x1,%edi
mov    $0x100,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1280 <get_nvme_subsnqn+0xf0>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
test   %eax,%eax
js     124b <get_nvme_subsnqn+0xbb>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1282 <get_nvme_subsnqn+0xf2>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    1282 <get_nvme_subsnqn+0xf2>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebx
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd3(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1090 <fprintf@plt>
xor    %ebx,%ebx
jmp    1289 <get_nvme_subsnqn+0xf9>
mov    0x2d86(%rip),%rax
mov    (%rax),%r14d
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdba(%rip),%rsi
mov    %rsp,%rdx
mov    %r14d,%edi
mov    %rax,%rcx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rax
add    $0x140,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"%s/subsysnqn\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 = [23 x i8] c"Failed to read %s: %s\0A\00"
define i8* @get_nvme_subsnqn(i8* %0) {
%2 = alloca [320 x i8]
%3 = getelementptr inbounds [320 x i8], [320 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 320, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %11)
br label %40
13: ; preds = %1
%14 = call dereferenceable_or_null(256) i8* @calloc(i32 1, i32 256)
%15 = icmp eq i8* %14, null
br i1 %15, label %37, label %16
16: ; preds = %13
%17 = call i32 @read(i32 %6, i8* %14, i32 256)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %22)
%24 = call i32 @free(i8* %14)
br label %37
25: ; preds = %16
%26 = call i32 @strlen(i8* %14)
%27 = add nsw i32 %26, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %14, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 10
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = call i32 @strlen(i8* %14)
%34 = add nsw i32 %33, -1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %14, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %19, %32, %25, %13
%38 = phi i8* [ null, %19 ], [ %14, %32 ], [ %14, %25 ], [ null, %13 ]
%39 = call i32 @close(i32 %6)
br label %40
40: ; preds = %37, %8
%41 = phi i8* [ null, %8 ], [ %38, %37 ]
ret i8* %41
}