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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
scalar_t__ strstr(int /*<<< orphan*/, char *);
int get_frontend_info(int adapter, char *frontend) {
  DIR *dev_dir;
  struct dirent *adap_dir;
  char tmp[64];
  sprintf(tmp, "%s/adapter%d", "/dev/dvb", adapter);
  dev_dir = opendir(tmp);
  if (!dev_dir) {
    fprintf(stderr, "ERROR: Opening %s directory\n", tmp);
    return -1;
  }
  while ((adap_dir = readdir(dev_dir)) != NULL) {
    if (!strcmp(adap_dir->d_name, "."))
      continue;
    if (!strcmp(adap_dir->d_name, ".."))
      continue;
    if (strstr(adap_dir->d_name, "frontend")) {
      strcpy(frontend, adap_dir->d_name);
    }
  }
  closedir(dev_dir);
  return 0;
}


x86 assembly (clang, -O0):
<get_frontend_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
lea    0xe63(%rip),%rsi
lea    0xe69(%rip),%rdx
call   1090 <sprintf@plt>
lea    -0x60(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e6 <get_frontend_info+0x66>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <get_frontend_info+0x102>
jmp    11eb <get_frontend_info+0x6b>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1272 <get_frontend_info+0xf2>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1222 <get_frontend_info+0xa2>
jmp    11eb <get_frontend_info+0x6b>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1242 <get_frontend_info+0xc2>
jmp    11eb <get_frontend_info+0x6b>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     126d <get_frontend_info+0xed>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
jmp    11eb <get_frontend_info+0x6b>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_frontend_info>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xe66(%rip),%rsi
lea    0xe6c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1247 <get_frontend_info+0xc7>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     123b <get_frontend_info+0xbb>
mov    %rax,%rbp
lea    0xe5d(%rip),%r15
lea    0xe55(%rip),%r12
lea    0xe51(%rip),%r13
jmp    1200 <get_frontend_info+0x80>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     123b <get_frontend_info+0xbb>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f0 <get_frontend_info+0x70>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f0 <get_frontend_info+0x70>
mov    0x0(%rbp),%edi
mov    %r13,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11f0 <get_frontend_info+0x70>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1030 <strcpy@plt>
jmp    11f0 <get_frontend_info+0x70>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    1264 <get_frontend_info+0xe4>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    %rsp,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [13 x i8] c"%s/adapter%d\00"
@.str.1 = [9 x i8] c"/dev/dvb\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"ERROR: Opening %s directory\0A\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [9 x i8] c"frontend\00"
define i32 @get_frontend_info(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca [64 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %55
20: ; preds = %2
br label %21
21: ; preds = %51, %38, %31, %20
%22 = load i32*, i32** %6
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %7
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %52
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %7
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @strcmp(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %25
br label %21
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %7
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @strcmp(i32 %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %32
br label %21
39: ; preds = %32
%40 = load %struct.dirent*, %struct.dirent** %7
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @strstr(i32 %42, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i8*, i8** %5
%47 = load %struct.dirent*, %struct.dirent** %7
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @strcpy(i8* %46, i32 %49)
br label %51
51: ; preds = %45, %39
br label %21
52: ; preds = %21
%53 = load i32*, i32** %6
%54 = call i32 @closedir(i32* %53)
store i32 0, i32* %3
br label %55
55: ; preds = %52, %16
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [13 x i8] c"%s/adapter%d\00"
@.str.1 = [9 x i8] c"/dev/dvb\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"ERROR: Opening %s directory\0A\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [9 x i8] c"frontend\00"
define i32 @get_frontend_info(i32 %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %34, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %36
14: ; preds = %8, %31
%15 = phi %struct.dirent* [ %32, %31 ], [ %9, %8 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @strcmp(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
br i1 %19, label %31, label %20
20: ; preds = %14
%21 = load i32, i32* %16
%22 = tail call i32 @strcmp(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = load i32, i32* %16
%26 = tail call i64 @strstr(i32 %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* %16
%30 = tail call i32 @strcpy(i8* %1, i32 %29)
br label %31
31: ; preds = %24, %28, %14, %20
%32 = tail call %struct.dirent* @readdir(i32* %6)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %14
34: ; preds = %31, %8
%35 = tail call i32 @closedir(i32* %6)
br label %36
36: ; preds = %34, %11
%37 = phi i32 [ 0, %34 ], [ -1, %11 ]
ret i32 %37
}