#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 /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};
struct cxl_afu_id {
  int afu_mode;
  int flags;
  int /*<<< orphan*/ afu_offset;
  int /*<<< orphan*/ card_id;
};
struct cxl_afu_h {
  int dummy;
};

/* Variables and functions */
int CXL_AFUID_FLAG_SLAVE;
int /*<<< orphan*/ CXL_IOCTL_GET_AFU_ID;
scalar_t__ EINVAL;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int asprintf(char **, char *, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int cxl_afu_fd(struct cxl_afu_h *);
scalar_t__ errno;
scalar_t__ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct cxl_afu_id *);
int /*<<< orphan*/ major(int /*<<< orphan*/);
int /*<<< orphan*/ minor(int /*<<< orphan*/);
int cxl_sysfs_fd(char **bufp, struct cxl_afu_h *afu) {
  struct cxl_afu_id afuid;
  char suffix = '\0';
  int fd = cxl_afu_fd(afu);
  if (ioctl(fd, CXL_IOCTL_GET_AFU_ID, &afuid) < 0) {
    struct stat sb;
    if ((errno != EINVAL) || (fstat(fd, &sb) < 0) || !S_ISCHR(sb.st_mode))
      return -1;
    return asprintf(bufp, "/sys/dev/char/%i:%i", major(sb.st_rdev),
                    minor(sb.st_rdev));
  }
  switch (afuid.afu_mode) {
  case 0x1:
    suffix = 'd';
    break;
  case 0x2:
    suffix = (afuid.flags & CXL_AFUID_FLAG_SLAVE) ? 's' : 'm';
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  return asprintf(bufp, "/sys/class/cxl/afu%i.%i%c", afuid.card_id,
                  afuid.afu_offset, suffix);
}


x86 assembly (clang, -O0):
<cxl_sysfs_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x29(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <cxl_afu_fd@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1236 <cxl_sysfs_fd+0xc6>
mov    0x2e22(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    11f0 <cxl_sysfs_fd+0x80>
mov    -0x30(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jl     11f0 <cxl_sysfs_fd+0x80>
mov    -0x34(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    11fc <cxl_sysfs_fd+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <cxl_sysfs_fd+0x158>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
call   1040 <minor@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1080 <asprintf@plt>
mov    %eax,-0x4(%rbp)
jmp    12c8 <cxl_sysfs_fd+0x158>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x1,%eax
je     125b <cxl_sysfs_fd+0xeb>
jmp    124a <cxl_sysfs_fd+0xda>
mov    -0x4c(%rbp),%eax
sub    $0x2,%eax
je     1264 <cxl_sysfs_fd+0xf4>
jmp    1288 <cxl_sysfs_fd+0x118>
movb   $0x64,-0x29(%rbp)
jmp    12a8 <cxl_sysfs_fd+0x138>
mov    -0x24(%rbp),%edx
mov    0x2d72(%rip),%rax
and    (%rax),%edx
mov    $0x6d,%eax
mov    $0x73,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,-0x29(%rbp)
jmp    12a8 <cxl_sysfs_fd+0x138>
mov    0x2d39(%rip),%rax
mov    (%rax),%rcx
mov    0x2d3f(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <cxl_sysfs_fd+0x158>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
movsbl -0x29(%rbp),%r8d
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1080 <asprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cxl_sysfs_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <cxl_afu_fd@plt>
mov    %eax,%ebx
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     11d3 <cxl_sysfs_fd+0x63>
mov    0x10(%rsp),%eax
cmp    $0x1,%eax
je     123b <cxl_sysfs_fd+0xcb>
cmp    $0x2,%eax
jne    125e <cxl_sysfs_fd+0xee>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
test   %eax,0x14(%rsp)
mov    $0x6d,%eax
mov    $0x73,%r8d
cmove  %eax,%r8d
jmp    1241 <cxl_sysfs_fd+0xd1>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2de4(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    1277 <cxl_sysfs_fd+0x107>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     1277 <cxl_sysfs_fd+0x107>
mov    0xc(%rsp),%edi
call   1070 <S_ISCHR@plt>
test   %eax,%eax
je     1277 <cxl_sysfs_fd+0x107>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1050 <major@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <minor@plt>
lea    0xdd5(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <asprintf@plt>
jmp    125a <cxl_sysfs_fd+0xea>
mov    $0x64,%r8d
mov    0x18(%rsp),%ecx
mov    0x1c(%rsp),%edx
lea    0xdc4(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <asprintf@plt>
mov    %eax,%ebp
jmp    1277 <cxl_sysfs_fd+0x107>
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d69(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.cxl_afu_h = type { i32 }
%struct.cxl_afu_id = type { i32, i32, i32, i32 }
%struct.stat = type { i32, i32 }
@CXL_IOCTL_GET_AFU_ID = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str = [20 x i8] c"/sys/dev/char/%i:%i\00"
@CXL_AFUID_FLAG_SLAVE = global i32 0
@.str.1 = [26 x i8] c"/sys/class/cxl/afu%i.%i%c\00"
define i32 @cxl_sysfs_fd(i8** %0, %struct.cxl_afu_h* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.cxl_afu_h*
%6 = alloca %struct.cxl_afu_id
%7 = alloca i8
%8 = alloca i32
%9 = alloca %struct.stat
store i8** %0, i8*** %4
store %struct.cxl_afu_h* %1, %struct.cxl_afu_h** %5
store i8 0, i8* %7
%10 = load %struct.cxl_afu_h*, %struct.cxl_afu_h** %5
%11 = call i32 @cxl_afu_fd(%struct.cxl_afu_h* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = load i32, i32* @CXL_IOCTL_GET_AFU_ID
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.cxl_afu_id* %6)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINVAL
%19 = icmp ne i64 %17, %18
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = load i32, i32* %8
%22 = call i64 @fstat(i32 %21, %struct.stat* %9)
%23 = icmp slt i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @S_ISCHR(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %24, %20, %16
store i32 -1, i32* %3
br label %63
30: ; preds = %24
%31 = load i8**, i8*** %4
%32 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @major(i32 %33)
%35 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @minor(i32 %36)
%38 = call i32 (i8**, i8*, i32, i32, ...) @asprintf(i8** %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %34, i32 %37)
store i32 %38, i32* %3
br label %63
39: ; preds = %2
%40 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %6, i32 0, i32 0
%41 = load i32, i32* %40
switch i32 %41, label %52 [
i32 1, label %42
i32 2, label %43
]
42: ; preds = %39
store i8 100, i8* %7
br label %54
43: ; preds = %39
%44 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %6, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32, i32* @CXL_AFUID_FLAG_SLAVE
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 115, i32 109
%51 = trunc i32 %50 to i8
store i8 %51, i8* %7
br label %54
52: ; preds = %39
%53 = load i64, i64* @EINVAL
store i64 %53, i64* @errno
store i32 -1, i32* %3
br label %63
54: ; preds = %43, %42
%55 = load i8**, i8*** %4
%56 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %6, i32 0, i32 3
%57 = load i32, i32* %56
%58 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %6, i32 0, i32 2
%59 = load i32, i32* %58
%60 = load i8, i8* %7
%61 = sext i8 %60 to i32
%62 = call i32 (i8**, i8*, i32, i32, ...) @asprintf(i8** %55, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %57, i32 %59, i32 %61)
store i32 %62, i32* %3
br label %63
63: ; preds = %54, %52, %30, %29
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.cxl_afu_h = type { i32 }
%struct.cxl_afu_id = type { i32, i32, i32, i32 }
%struct.stat = type { i32, i32 }
@CXL_IOCTL_GET_AFU_ID = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str = [20 x i8] c"/sys/dev/char/%i:%i\00"
@CXL_AFUID_FLAG_SLAVE = global i32 0
@.str.1 = [26 x i8] c"/sys/class/cxl/afu%i.%i%c\00"
define i32 @cxl_sysfs_fd(i8** %0, %struct.cxl_afu_h* %1) {
%3 = alloca %struct.cxl_afu_id
%4 = alloca %struct.stat
%5 = bitcast %struct.cxl_afu_id* %3 to i8*
%6 = tail call i32 @cxl_afu_fd(%struct.cxl_afu_h* %1)
%7 = load i32, i32* @CXL_IOCTL_GET_AFU_ID
%8 = call i64 @ioctl(i32 %6, i32 %7, %struct.cxl_afu_id* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %2
%11 = bitcast %struct.stat* %4 to i8*
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINVAL
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %29
15: ; preds = %10
%16 = call i64 @fstat(i32 %6, %struct.stat* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @S_ISCHR(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @major(i32 %25)
%27 = call i32 @minor(i32 %25)
%28 = call i32 (i8**, i8*, i32, i32, ...) @asprintf(i8** %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %10, %15, %18, %23
%30 = phi i32 [ %28, %23 ], [ -1, %18 ], [ -1, %15 ], [ -1, %10 ]
br label %50
31: ; preds = %2
%32 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %3, i64 0, i32 0
%33 = load i32, i32* %32
switch i32 %33, label %41 [
i32 1, label %43
i32 2, label %34
]
34: ; preds = %31
%35 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %3, i64 0, i32 1
%36 = load i32, i32* %35
%37 = load i32, i32* @CXL_AFUID_FLAG_SLAVE
%38 = and i32 %37, %36
%39 = icmp eq i32 %38, 0
%40 = select i1 %39, i32 109, i32 115
br label %43
41: ; preds = %31
%42 = load i64, i64* @EINVAL
store i64 %42, i64* @errno
br label %50
43: ; preds = %31, %34
%44 = phi i32 [ %40, %34 ], [ 100, %31 ]
%45 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %3, i64 0, i32 3
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.cxl_afu_id, %struct.cxl_afu_id* %3, i64 0, i32 2
%48 = load i32, i32* %47
%49 = call i32 (i8**, i8*, i32, i32, ...) @asprintf(i8** %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 %48, i32 %44)
br label %50
50: ; preds = %43, %41, %29
%51 = phi i32 [ %30, %29 ], [ -1, %41 ], [ %49, %43 ]
ret i32 %51
}