#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 *);
char *strdup(char *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ strncmp(char *, char const *, int /*<<< orphan*/);
int sysfs_read_uevents(char ***, int *, int *, char const *);
char *sysfs_read_uevent_key_val(const char *syspath, const char *key) {
  char *uevent = NULL;
  char *value = NULL;
  int pos = 0;
  int ret = 0;
  char **uevents = NULL;
  int uevent_count = 0;
  int uevent_max = 0;
  ret = sysfs_read_uevents(&uevents, &uevent_count, &uevent_max, syspath);
  if (ret < 0) {
    return NULL;
  }
  for (pos = 0; (uevents[pos] != NULL); ++pos) {
    uevent = uevents[pos];
    if ((strncmp(uevent, key, strlen(key))) == 0) {
      value = strdup(uevent);
      goto exit_free;
    }
  }
exit_free:
  if (uevents) {
    for (; (*uevents != NULL); uevents++) {
      if (*uevents) {
        free(*uevents);
      }
    }
  }
  if (ret < 0) {
    if (value) {
      free(value);
    }
    return NULL;
  }
  return value;
}


x86 assembly (clang, -O0):
<sysfs_read_uevent_key_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x38(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1070 <sysfs_read_uevents@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11c3 <sysfs_read_uevent_key_val+0x73>
movq   $0x0,-0x8(%rbp)
jmp    12cf <sysfs_read_uevent_key_val+0x17f>
movl   $0x0,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1244 <sysfs_read_uevent_key_val+0xf4>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1231 <sysfs_read_uevent_key_val+0xe1>
mov    -0x20(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
jmp    1249 <sysfs_read_uevent_key_val+0xf9>
jmp    1236 <sysfs_read_uevent_key_val+0xe6>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ca <sysfs_read_uevent_key_val+0x7a>
jmp    1249 <sysfs_read_uevent_key_val+0xf9>
cmpq   $0x0,-0x38(%rbp)
je     129c <sysfs_read_uevent_key_val+0x14c>
jmp    1259 <sysfs_read_uevent_key_val+0x109>
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
je     1297 <sysfs_read_uevent_key_val+0x147>
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
je     1281 <sysfs_read_uevent_key_val+0x131>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
jmp    1286 <sysfs_read_uevent_key_val+0x136>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    1259 <sysfs_read_uevent_key_val+0x109>
jmp    129c <sysfs_read_uevent_key_val+0x14c>
cmpl   $0x0,-0x30(%rbp)
jge    12c7 <sysfs_read_uevent_key_val+0x177>
cmpq   $0x0,-0x28(%rbp)
je     12ba <sysfs_read_uevent_key_val+0x16a>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cf <sysfs_read_uevent_key_val+0x17f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_uevent_key_val>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rcx
movq   $0x0,0x8(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1070 <sysfs_read_uevents@plt>
test   %eax,%eax
js     11de <sysfs_read_uevent_key_val+0x8e>
mov    0x8(%rsp),%rax
mov    (%rax),%r12
test   %r12,%r12
je     11de <sysfs_read_uevent_key_val+0x8e>
mov    $0x8,%ebx
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11e3 <sysfs_read_uevent_key_val+0x93>
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,1),%r12
add    $0x8,%rbx
test   %r12,%r12
jne    11b0 <sysfs_read_uevent_key_val+0x60>
jmp    11f8 <sysfs_read_uevent_key_val+0xa8>
xor    %r14d,%r14d
jmp    121c <sysfs_read_uevent_key_val+0xcc>
mov    %r12,%rdi
call   1060 <strdup@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
test   %rax,%rax
je     121c <sysfs_read_uevent_key_val+0xcc>
mov    (%rax),%rdi
test   %rdi,%rdi
je     121c <sysfs_read_uevent_key_val+0xcc>
call   1030 <free@plt>
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    0x8(%rax),%rdi
test   %rdi,%rdi
jne    1200 <sysfs_read_uevent_key_val+0xb0>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @sysfs_read_uevent_key_val(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i8** null, i8*** %10
store i32 0, i32* %11
store i32 0, i32* %12
%13 = load i8*, i8** %4
%14 = call i32 @sysfs_read_uevents(i8*** %10, i32* %11, i32* %12, i8* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i8* null, i8** %3
br label %79
18: ; preds = %2
store i32 0, i32* %8
br label %19
19: ; preds = %42, %18
%20 = load i8**, i8*** %10
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %45
26: ; preds = %19
%27 = load i8**, i8*** %10
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = load i8*, i8** %5
%34 = load i8*, i8** %5
%35 = call i32 @strlen(i8* %34)
%36 = call i64 @strncmp(i8* %32, i8* %33, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = load i8*, i8** %6
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %7
br label %46
41: ; preds = %26
br label %42
42: ; preds = %41
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %19
45: ; preds = %19
br label %46
46: ; preds = %45, %38
%47 = load i8**, i8*** %10
%48 = icmp ne i8** %47, null
br i1 %48, label %49, label %67
49: ; preds = %46
br label %50
50: ; preds = %63, %49
%51 = load i8**, i8*** %10
%52 = load i8*, i8** %51
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %66
54: ; preds = %50
%55 = load i8**, i8*** %10
%56 = load i8*, i8** %55
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = load i8**, i8*** %10
%60 = load i8*, i8** %59
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %58, %54
br label %63
63: ; preds = %62
%64 = load i8**, i8*** %10
%65 = getelementptr inbounds i8*, i8** %64, i32 1
store i8** %65, i8*** %10
br label %50
66: ; preds = %50
br label %67
67: ; preds = %66, %46
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %77
70: ; preds = %67
%71 = load i8*, i8** %7
%72 = icmp ne i8* %71, null
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i8*, i8** %7
%75 = call i32 @free(i8* %74)
br label %76
76: ; preds = %73, %70
store i8* null, i8** %3
br label %79
77: ; preds = %67
%78 = load i8*, i8** %7
store i8* %78, i8** %3
br label %79
79: ; preds = %77, %76, %17
%80 = load i8*, i8** %3
ret i8* %80
}

LLVM IR (-O2):
define noalias i8* @sysfs_read_uevent_key_val(i8* %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i8*** %3 to i8*
store i8** null, i8*** %3
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = call i32 @sysfs_read_uevents(i8*** %3, i32* %4, i32* %5, i8* %0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %43, label %11
11: ; preds = %2
%12 = load i8**, i8*** %3
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %43, label %20
15: ; preds = %20
%16 = load i8**, i8*** %3
%17 = getelementptr inbounds i8*, i8** %16, i64 %26
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %31, label %20
20: ; preds = %11, %15
%21 = phi i64 [ %26, %15 ], [ 0, %11 ]
%22 = phi i8* [ %18, %15 ], [ %13, %11 ]
%23 = call i32 @strlen(i8* %1)
%24 = call i64 @strncmp(i8* %22, i8* %1, i32 %23)
%25 = icmp eq i64 %24, 0
%26 = add nuw i64 %21, 1
br i1 %25, label %27, label %15
27: ; preds = %20
%28 = call i8* @strdup(i8* %22)
%29 = load i8**, i8*** %3
%30 = icmp eq i8** %29, null
br i1 %30, label %43, label %31
31: ; preds = %15, %27
%32 = phi i8* [ %28, %27 ], [ null, %15 ]
%33 = phi i8** [ %29, %27 ], [ %16, %15 ]
%34 = load i8*, i8** %33
%35 = icmp eq i8* %34, null
br i1 %35, label %43, label %36
36: ; preds = %31, %36
%37 = phi i8* [ %41, %36 ], [ %34, %31 ]
%38 = call i32 @free(i8* %37)
%39 = load i8**, i8*** %3
%40 = getelementptr inbounds i8*, i8** %39, i64 1
store i8** %40, i8*** %3
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %36
43: ; preds = %36, %11, %31, %27, %2
%44 = phi i8* [ null, %2 ], [ %28, %27 ], [ %32, %31 ], [ null, %11 ], [ %32, %36 ]
ret i8* %44
}