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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int vdev_read_file(char *, char **, size_t *);
int vdev_sysfs_uevent_get_key(char *, size_t, char *, char **, size_t *);
int get_device_type(char const *sysfs_path, char **device_type,
                    size_t *device_type_len) {
  char uevent_path[4097];
  memset(uevent_path, 0, 4097);
  char *uevent_buf = NULL;
  size_t uevent_len = 0;
  char *device_type_buf = NULL;
  size_t device_type_buf_len = 0;
  int rc = 0;
  snprintf(uevent_path, 4096, "%s/uevent", sysfs_path);
  rc = vdev_read_file(uevent_path, &uevent_buf, &uevent_len);
  if (rc != 0) {
    return rc;
  }
  rc = vdev_sysfs_uevent_get_key(uevent_buf, uevent_len, "DEVTYPE",
                                 &device_type_buf, &device_type_buf_len);
  if (rc != 0) {
    free(uevent_buf);
    return rc;
  }
  *device_type = device_type_buf;
  *device_type_len = device_type_buf_len;
  free(uevent_buf);
  return 0;
}


x86 assembly (clang, -O0):
<get_device_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1060 <memset@plt>
movq   $0x0,-0x1038(%rbp)
movq   $0x0,-0x1040(%rbp)
movq   $0x0,-0x1048(%rbp)
movq   $0x0,-0x1050(%rbp)
movl   $0x0,-0x1054(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe39(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x1030(%rbp),%rdi
lea    -0x1038(%rbp),%rsi
lea    -0x1040(%rbp),%rdx
call   1030 <vdev_read_file@plt>
mov    %eax,-0x1054(%rbp)
cmpl   $0x0,-0x1054(%rbp)
je     1207 <get_device_type+0xb7>
mov    -0x1054(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <get_device_type+0x13b>
mov    -0x1038(%rbp),%rdi
mov    -0x1040(%rbp),%rsi
lea    0xdee(%rip),%rdx
lea    -0x1048(%rbp),%rcx
lea    -0x1050(%rbp),%r8
call   1070 <vdev_sysfs_uevent_get_key@plt>
mov    %eax,-0x1054(%rbp)
cmpl   $0x0,-0x1054(%rbp)
je     125c <get_device_type+0x10c>
mov    -0x1038(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1054(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <get_device_type+0x13b>
mov    -0x1048(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1050(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1038(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_device_type>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1028,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1060 <memset@plt>
movq   $0x0,(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe5c(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %rbp,%rcx
call   1050 <snprintf@plt>
mov    %rsp,%rsi
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
call   1030 <vdev_read_file@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    120a <get_device_type+0xba>
mov    (%rsp),%rdi
mov    0x18(%rsp),%rsi
lea    0xe30(%rip),%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
call   1070 <vdev_sysfs_uevent_get_key@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1201 <get_device_type+0xb1>
mov    0x10(%rsp),%rax
mov    %rax,(%r15)
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
xor    %ebp,%ebp
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"%s/uevent\00"
@.str.1 = [8 x i8] c"DEVTYPE\00"
define i32 @get_device_type(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca [4097 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 4097)
%11 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%12 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%13 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%14 = bitcast i64* %8 to i8*
store i64 0, i64* %8
%15 = call i32 @snprintf(i8* %9, i32 4096, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = call i32 @vdev_read_file(i8* %9, i8** %5, i64* %6)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = call i32 @vdev_sysfs_uevent_get_key(i8* %19, i64 %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %7, i64* %8)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %7
store i8* %24, i8** %1
%25 = load i64, i64* %8
store i64 %25, i64* %2
br label %26
26: ; preds = %18, %23
%27 = phi i32 [ 0, %23 ], [ %21, %18 ]
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %26, %3
%31 = phi i32 [ %16, %3 ], [ %27, %26 ]
ret i32 %31
}