#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 */
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strstr(char *, char *);
int get_frontend_entry(DIR *dvb_dir, char *frontend_dev) {
  struct dirent *adap_dir;
  char tmp[64];
  adap_dir = readdir(dvb_dir);
  if (adap_dir != NULL) {
    sprintf(tmp, "%s/%s", "/sys/class/dvb", adap_dir->d_name);
    if (strstr(tmp, "frontend")) {
      strcpy(frontend_dev, tmp);
      return 0;
    } else {
      return -2;
    }
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<get_frontend_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c5 <get_frontend_entry+0x85>
lea    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xe86(%rip),%rsi
lea    0xe85(%rip),%rdx
call   1050 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11b9 <get_frontend_entry+0x79>
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cc <get_frontend_entry+0x8c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11cc <get_frontend_entry+0x8c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_frontend_entry>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
call   1040 <readdir@plt>
test   %rax,%rax
je     1193 <get_frontend_entry+0x53>
mov    (%rax),%rcx
lea    0xea2(%rip),%rsi
lea    0xea1(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     119a <get_frontend_entry+0x5a>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    119f <get_frontend_entry+0x5f>
mov    $0xffffffff,%eax
jmp    119f <get_frontend_entry+0x5f>
mov    $0xfffffffe,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [15 x i8] c"/sys/class/dvb\00"
@.str.2 = [9 x i8] c"frontend\00"
define i32 @get_frontend_entry(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca [64 x i8]
store i32* %0, i32** %4
store i8* %1, i8** %5
%8 = load i32*, i32** %4
%9 = call %struct.dirent* @readdir(i32* %8)
store %struct.dirent* %9, %struct.dirent** %6
%10 = load %struct.dirent*, %struct.dirent** %6
%11 = icmp ne %struct.dirent* %10, null
br i1 %11, label %12, label %26
12: ; preds = %2
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%14 = load %struct.dirent*, %struct.dirent** %6
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%19 = call i64 @strstr(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i8*, i8** %5
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%24 = call i32 @strcpy(i8* %22, i8* %23)
store i32 0, i32* %3
br label %27
25: ; preds = %12
store i32 -2, i32* %3
br label %27
26: ; preds = %2
store i32 -1, i32* %3
br label %27
27: ; preds = %26, %25, %21
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [15 x i8] c"/sys/class/dvb\00"
@.str.2 = [9 x i8] c"frontend\00"
define i32 @get_frontend_entry(i32* %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = tail call %struct.dirent* @readdir(i32* %0)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %15, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %5, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = call i32 @strcpy(i8* %1, i8* %4)
br label %15
15: ; preds = %2, %7, %13
%16 = phi i32 [ 0, %13 ], [ -2, %7 ], [ -1, %2 ]
ret i32 %16
}