#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 */
typedef int /*<<< orphan*/ tap;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int open_macvtap_device(char *);
int open_tap_device(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int open_by_name_index(const char *name, unsigned int idx) {
  int fd;
  char tap[64];
  snprintf(tap, sizeof(tap), "/sys/class/net/%s/tap%u", name, idx);
  fd = open(tap, O_RDONLY);
  if (fd < 0)
    return open_tap_device(name);
  close(fd);
  snprintf(tap, sizeof(tap), "/dev/tap%u", idx);
  return open_macvtap_device(tap);
}


x86 assembly (clang, -O0):
<open_by_name_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    $0x40,%esi
lea    0xe89(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ae <open_by_name_index+0x5e>
mov    -0x10(%rbp),%rdi
call   1040 <open_tap_device@plt>
mov    %eax,-0x4(%rbp)
jmp    11dc <open_by_name_index+0x8c>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0x60(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x40,%esi
lea    0xe4f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
call   1030 <open_macvtap_device@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_by_name_index>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xe9b(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %rbx,%rcx
mov    %r14d,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11bf <open_by_name_index+0x6f>
mov    %eax,%edi
call   1060 <close@plt>
lea    0xe78(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <open_macvtap_device@plt>
jmp    11c7 <open_by_name_index+0x77>
mov    %rbx,%rdi
call   1040 <open_tap_device@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/sys/class/net/%s/tap%u\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"/dev/tap%u\00"
define i32 @open_by_name_index(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [64 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @open_tap_device(i8* %18)
store i32 %19, i32* %3
br label %28
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%24 = load i32, i32* %5
%25 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %23, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%27 = call i32 @open_macvtap_device(i8* %26)
store i32 %27, i32* %3
br label %28
28: ; preds = %20, %17
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [24 x i8] c"/sys/class/net/%s/tap%u\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"/dev/tap%u\00"
define i32 @open_by_name_index(i8* %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @open_tap_device(i8* %0)
br label %15
11: ; preds = %2
%12 = call i32 @close(i32 %7)
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%14 = call i32 @open_macvtap_device(i8* %4)
br label %15
15: ; preds = %11, %9
%16 = phi i32 [ %10, %9 ], [ %14, %11 ]
ret i32 %16
}