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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int uio_getprussfile(char *devname) {
  DIR *d;
  struct dirent *dent;
  char fn[100];
  FILE *fd;
  char s_name[50];
  devname[0] = 0;
  d = opendir("/dev");
  dent = readdir(d);
  while (dent != NULL) {
    if (dent->d_name[0] == 'u' && dent->d_name[1] == 'i' &&
        dent->d_name[2] == 'o' && devname[0] == 0) {
      sprintf(fn, "/sys/class/uio/%s/name", dent->d_name);
      fd = fopen(fn, "r");
      fgets(s_name, 50, fd);
      s_name[strlen(s_name) - 1] = 0;
      if (!strncmp(s_name, "pruss", 5)) {
        sprintf(devname, "/dev/%s", dent->d_name);
      }
      fclose(fd);
    }
    dent = readdir(d);
  }
  closedir(d);
  return 0;
}


x86 assembly (clang, -O0):
<uio_getprussfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
lea    0xe53(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ce <uio_getprussfile+0x13e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movzbl (%rax),%eax
cmp    $0x75,%eax
jne    12bc <uio_getprussfile+0x12c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movzbl 0x1(%rax),%eax
cmp    $0x69,%eax
jne    12bc <uio_getprussfile+0x12c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movzbl 0x2(%rax),%eax
cmp    $0x6f,%eax
jne    12bc <uio_getprussfile+0x12c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12bc <uio_getprussfile+0x12c>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xdda(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x88(%rbp)
lea    -0xc0(%rbp),%rdi
mov    -0x88(%rbp),%rdx
mov    $0x32,%esi
call   1080 <fgets@plt>
lea    -0xc0(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0xc0(%rbp,%rax,1)
lea    -0xc0(%rbp),%rdi
lea    0xd98(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12b0 <uio_getprussfile+0x120>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xd79(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x88(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    11c3 <uio_getprussfile+0x33>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uio_getprussfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rdi,%r14
movb   $0x0,(%rdi)
lea    0xe52(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1283 <uio_getprussfile+0xf3>
mov    %rax,%r13
lea    0x40(%rsp),%r12
mov    %rsp,%rbp
jmp    11f4 <uio_getprussfile+0x64>
mov    %r15,%rdi
call   1040 <fclose@plt>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1283 <uio_getprussfile+0xf3>
mov    0x0(%r13),%rdx
cmpb   $0x75,(%rdx)
jne    11e0 <uio_getprussfile+0x50>
cmpb   $0x69,0x1(%rdx)
jne    11e0 <uio_getprussfile+0x50>
cmpb   $0x6f,0x2(%rdx)
jne    11e0 <uio_getprussfile+0x50>
cmpb   $0x0,(%r14)
jne    11e0 <uio_getprussfile+0x50>
mov    %r12,%rdi
lea    0xdec(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %r12,%rdi
lea    0xdf4(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    $0x32,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbp,1)
mov    %rbp,%rdi
lea    0xdc5(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11d4 <uio_getprussfile+0x44>
mov    0x0(%r13),%rdx
mov    %r14,%rdi
lea    0xdab(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    11d4 <uio_getprussfile+0x44>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [23 x i8] c"/sys/class/uio/%s/name\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"pruss\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i32 @uio_getprussfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca [100 x i8]
%6 = alloca i32*
%7 = alloca [50 x i8]
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %9
%10 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %4
br label %13
13: ; preds = %74, %1
%14 = load %struct.dirent*, %struct.dirent** %4
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %77
16: ; preds = %13
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp eq i32 %22, 117
br i1 %23, label %24, label %74
24: ; preds = %16
%25 = load %struct.dirent*, %struct.dirent** %4
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp eq i32 %30, 105
br i1 %31, label %32, label %74
32: ; preds = %24
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 2
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = icmp eq i32 %38, 111
br i1 %39, label %40, label %74
40: ; preds = %32
%41 = load i8*, i8** %2
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %74
46: ; preds = %40
%47 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%48 = load %struct.dirent*, %struct.dirent** %4
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @sprintf(i8* %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %50)
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %53, i32** %6
%54 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%55 = load i32*, i32** %6
%56 = call i32 @fgets(i8* %54, i32 50, i32* %55)
%57 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 %60
store i8 0, i8* %61
%62 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%63 = call i32 @strncmp(i8* %62, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%64 = icmp ne i32 %63, 0
br i1 %64, label %71, label %65
65: ; preds = %46
%66 = load i8*, i8** %2
%67 = load %struct.dirent*, %struct.dirent** %4
%68 = getelementptr inbounds %struct.dirent, %struct.dirent* %67, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = call i32 @sprintf(i8* %66, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %65, %46
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
br label %74
74: ; preds = %71, %40, %32, %24, %16
%75 = load i32*, i32** %3
%76 = call %struct.dirent* @readdir(i32* %75)
store %struct.dirent* %76, %struct.dirent** %4
br label %13
77: ; preds = %13
%78 = load i32*, i32** %3
%79 = call i32 @closedir(i32* %78)
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [23 x i8] c"/sys/class/uio/%s/name\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"pruss\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i32 @uio_getprussfile(i8* nocapture %0) {
%2 = alloca [100 x i8]
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
store i8 0, i8* %0
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call %struct.dirent* @readdir(i32* %6)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %44, label %9
9: ; preds = %1, %41
%10 = phi %struct.dirent* [ %42, %41 ], [ %7, %1 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 117
br i1 %14, label %15, label %41
15: ; preds = %9
%16 = getelementptr inbounds i8, i8* %12, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 105
br i1 %18, label %19, label %41
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %12, i64 2
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 111
br i1 %22, label %23, label %41
23: ; preds = %19
%24 = load i8, i8* %0
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %23
%27 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%28 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @fgets(i8* %5, i32 50, i32* %28)
%30 = call i32 @strlen(i8* %5)
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 @strncmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = load i8*, i8** %11
%38 = call i32 @sprintf(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %36, %26
%40 = call i32 @fclose(i32* %28)
br label %41
41: ; preds = %39, %23, %19, %15, %9
%42 = call %struct.dirent* @readdir(i32* %6)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %9
44: ; preds = %41, %1
%45 = call i32 @closedir(i32* %6)
ret i32 0
}