#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
scalar_t__ strstr(char *, char const *);
int pwm_get_key(const char *pin, char **pwm_key) {
  DIR *dd;
  struct dirent *df;
  dd = opendir("/sys/devices/ocp.2");
  if (dd != NULL) {
    while ((df = readdir(dd))) {
      if (strstr(df->d_name, pin)) {
        int pwm_key_len = strlen(df->d_name) + 1;
        *pwm_key = (char *)calloc(pwm_key_len, sizeof(char));
        snprintf(*pwm_key, pwm_key_len, "%s", df->d_name);
        closedir(dd);
        return 0;
      }
    }
    closedir(dd);
  }
  return -1;
}


x86 assembly (clang, -O0):
<pwm_get_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe79(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123a <pwm_get_key+0xca>
jmp    11a0 <pwm_get_key+0x30>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1231 <pwm_get_key+0xc1>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     122c <pwm_get_key+0xbc>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xe01(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    1241 <pwm_get_key+0xd1>
jmp    11a0 <pwm_get_key+0x30>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwm_get_key>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe7b(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11f2 <pwm_get_key+0x82>
mov    %rax,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11f9 <pwm_get_key+0x89>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11a0 <pwm_get_key+0x30>
mov    (%rbx),%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,(%r14)
mov    (%rbx),%rcx
lea    0xe2f(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
call   1050 <snprintf@plt>
xor    %ebx,%ebx
jmp    11fe <pwm_get_key+0x8e>
mov    $0xffffffff,%ebx
jmp    1206 <pwm_get_key+0x96>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/devices/ocp.2\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @pwm_get_key(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %47
12: ; preds = %2
br label %13
13: ; preds = %43, %12
%14 = load i32*, i32** %6
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %7
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %44
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %7
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8*, i8** %4
%22 = call i64 @strstr(i8* %20, i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %43
24: ; preds = %17
%25 = load %struct.dirent*, %struct.dirent** %7
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = call i64 @calloc(i32 %30, i32 1)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %5
store i8* %32, i8** %33
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = load i32, i32* %8
%37 = load %struct.dirent*, %struct.dirent** %7
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @snprintf(i8* %35, i32 %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32*, i32** %6
%42 = call i32 @closedir(i32* %41)
store i32 0, i32* %3
br label %48
43: ; preds = %17
br label %13
44: ; preds = %13
%45 = load i32*, i32** %6
%46 = call i32 @closedir(i32* %45)
br label %47
47: ; preds = %44, %2
store i32 -1, i32* %3
br label %48
48: ; preds = %47, %24
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/devices/ocp.2\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @pwm_get_key(i8* %0, i8** nocapture writeonly %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %25, label %5
5: ; preds = %2, %8
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i64 @strstr(i8* %10, i8* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %5, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 1
%18 = tail call i64 @calloc(i32 %17, i32 1)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %1
%20 = load i8*, i8** %14
%21 = tail call i32 @snprintf(i8* %19, i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %5, %13
%23 = phi i32 [ 0, %13 ], [ -1, %5 ]
%24 = tail call i32 @closedir(i32* %3)
br label %25
25: ; preds = %22, %2
%26 = phi i32 [ -1, %2 ], [ %23, %22 ]
ret i32 %26
}