#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*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int sscanf(int /*<<< orphan*/, char *, char *);
scalar_t__ strcmp(char *, char const *);
int find_interface_in_dir(const char *dirname, const char *ifname) {
  DIR *device_dir;
  struct dirent *device_dp;
  device_dir = opendir(dirname);
  if (!device_dir)
    return 0;
  while ((device_dp = readdir(device_dir)) != NULL) {
    char netname[8];
    if ((sscanf(device_dp->d_name, "net:%s", netname) == 1) &&
        (strcmp(netname, ifname) == 0)) {
      closedir(device_dir);
      return 1;
    }
  }
  closedir(device_dir);
  return 0;
}


x86 assembly (clang, -O0):
<find_interface_in_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1184 <find_interface_in_dir+0x34>
movl   $0x0,-0x4(%rbp)
jmp    1200 <find_interface_in_dir+0xb0>
jmp    1189 <find_interface_in_dir+0x39>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f0 <find_interface_in_dir+0xa0>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11eb <find_interface_in_dir+0x9b>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11eb <find_interface_in_dir+0x9b>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    1200 <find_interface_in_dir+0xb0>
jmp    1189 <find_interface_in_dir+0x39>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_interface_in_dir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
call   1030 <opendir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11d7 <find_interface_in_dir+0x87>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11cf <find_interface_in_dir+0x7f>
lea    0xe7d(%rip),%r12
lea    0x8(%rsp),%rbx
jmp    119d <find_interface_in_dir+0x4d>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11cf <find_interface_in_dir+0x7f>
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1190 <find_interface_in_dir+0x40>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1190 <find_interface_in_dir+0x40>
mov    %rbp,%rdi
call   1040 <closedir@plt>
mov    $0x1,%r14d
jmp    11d7 <find_interface_in_dir+0x87>
mov    %rbp,%rdi
call   1040 <closedir@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [7 x i8] c"net:%s\00"
define i32 @find_interface_in_dir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca [8 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 0, i32* %3
br label %38
14: ; preds = %2
br label %15
15: ; preds = %34, %14
%16 = load i32*, i32** %6
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %7
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %35
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%24 = call i32 @sscanf(i32 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %34
26: ; preds = %19
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%28 = load i8*, i8** %5
%29 = call i64 @strcmp(i8* %27, i8* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32*, i32** %6
%33 = call i32 @closedir(i32* %32)
store i32 1, i32* %3
br label %38
34: ; preds = %26, %19
br label %15
35: ; preds = %15
%36 = load i32*, i32** %6
%37 = call i32 @closedir(i32* %36)
store i32 0, i32* %3
br label %38
38: ; preds = %35, %31, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [7 x i8] c"net:%s\00"
define i32 @find_interface_in_dir(i8* nocapture %0, i8* %1) {
%3 = alloca [8 x i8]
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %27, label %6
6: ; preds = %2
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
br label %11
11: ; preds = %9, %22
%12 = phi %struct.dirent* [ %7, %9 ], [ %23, %22 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @sscanf(i32 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %10)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = call i64 @strcmp(i8* %10, i8* %1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @closedir(i32* %4)
br label %27
22: ; preds = %11, %17
%23 = call %struct.dirent* @readdir(i32* %4)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %11
25: ; preds = %22, %6
%26 = call i32 @closedir(i32* %4)
br label %27
27: ; preds = %20, %2, %25
%28 = phi i32 [ 0, %25 ], [ 0, %2 ], [ 1, %20 ]
ret i32 %28
}