#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 stat {
  int /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ dev;

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ if_indextoname(unsigned int, char *);
unsigned int major(int /*<<< orphan*/);
unsigned int minor(int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int open_by_name_index(char *, unsigned long);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, unsigned int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int open_by_node(const char *node) {
  char dev[64];
  struct stat stbuf;
  ssize_t got;
  unsigned int maj, min;
  unsigned long idx;
  int fd = open(node, O_RDONLY);
  if (fd < 0) {
    perror("open");
    return (EXIT_FAILURE);
  }
  if (fstat(fd, &stbuf) != 0) {
    perror("stat");
    close(fd);
    return (EXIT_FAILURE);
  }
  close(fd);
  if (!S_ISCHR(stbuf.st_mode)) {
    fprintf(stderr, "interface name or device file expected");
    return (EXIT_FAILURE);
  }
  maj = major(stbuf.st_rdev);
  min = minor(stbuf.st_rdev);
  snprintf(dev, sizeof(dev), "/sys/dev/char/%u:%u/device/ifindex", maj, min);
  fd = open(dev, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "failed to get interface index from device: %s\n",
            strerror(errno));
    return (EXIT_FAILURE);
  }
  got = read(fd, dev, sizeof(dev) - 1);
  if (got <= 0) {
    perror("read");
    close(fd);
    return (EXIT_FAILURE);
  }
  close(fd);
  dev[got] = 0;
  idx = strtoul(dev, NULL, 0);
  if (!if_indextoname((unsigned int)idx, dev)) {
    perror("failed to find interface name");
    return (EXIT_FAILURE);
  }
  return open_by_name_index(dev, idx);
}


x86 assembly (clang, -O0):
<open_by_node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    122b <open_by_node+0x4b>
lea    0xdeb(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e5 <open_by_node+0x205>
mov    -0x74(%rbp),%edi
lea    -0x58(%rbp),%rsi
call   1100 <fstat@plt>
cmp    $0x0,%rax
je     1266 <open_by_node+0x86>
lea    0xdbd(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e5 <open_by_node+0x205>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
mov    -0x54(%rbp),%edi
call   10a0 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    12a7 <open_by_node+0xc7>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e5 <open_by_node+0x205>
mov    -0x58(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x64(%rbp)
mov    -0x58(%rbp),%edi
call   1030 <minor@plt>
mov    %eax,-0x68(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x64(%rbp),%ecx
mov    -0x68(%rbp),%r8d
mov    $0x40,%esi
lea    0xd5d(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    1337 <open_by_node+0x157>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xd35(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e5 <open_by_node+0x205>
mov    -0x74(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3f,%edx
call   1080 <read@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
ja     137c <open_by_node+0x19c>
lea    0xd25(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e5 <open_by_node+0x205>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
mov    -0x60(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10e0 <strtoul@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
mov    %eax,%edi
lea    -0x50(%rbp),%rsi
call   10b0 <if_indextoname@plt>
cmp    $0x0,%eax
jne    13d5 <open_by_node+0x1f5>
lea    0xcc9(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2bfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e5 <open_by_node+0x205>
lea    -0x50(%rbp),%rdi
mov    -0x70(%rbp),%rsi
call   1070 <open_by_name_index@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_by_node>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    0x2df9(%rip),%r14
mov    (%r14),%esi
call   10c0 <open@plt>
test   %eax,%eax
js     1314 <open_by_node+0x134>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1100 <fstat@plt>
test   %rax,%rax
jne    131d <open_by_node+0x13d>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   10a0 <S_ISCHR@plt>
test   %eax,%eax
je     12cf <open_by_node+0xef>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1050 <major@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <minor@plt>
lea    0xde8(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %ebp,%ecx
mov    %eax,%r8d
call   1040 <snprintf@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     12e8 <open_by_node+0x108>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x3f,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1326 <open_by_node+0x146>
mov    %rax,%r14
mov    %ebx,%edi
call   1060 <close@plt>
movb   $0x0,0x10(%rsp,%r14,1)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <strtoul@plt>
mov    %rax,%rbx
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <if_indextoname@plt>
test   %eax,%eax
je     133b <open_by_node+0x15b>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <open_by_name_index@plt>
jmp    1350 <open_by_node+0x170>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1347 <open_by_node+0x167>
mov    0x2ce9(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd4e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1347 <open_by_node+0x167>
lea    0xce5(%rip),%rdi
jmp    1342 <open_by_node+0x162>
lea    0xce1(%rip),%rdi
jmp    132d <open_by_node+0x14d>
lea    0xd56(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1347 <open_by_node+0x167>
lea    0xd46(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"interface name or device file expected\00"
@.str.3 = [35 x i8] c"/sys/dev/char/%u:%u/device/ifindex\00"
@.str.4 = [47 x i8] c"failed to get interface index from device: %s\0A\00"
@errno = global i32 0
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [30 x i8] c"failed to find interface name\00"
define i32 @open_by_node(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [64 x i8]
%5 = alloca %struct.stat
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
store i32 %18, i32* %2
br label %91
19: ; preds = %1
%20 = load i32, i32* %10
%21 = call i64 @fstat(i32 %20, %struct.stat* %5)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %10
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %2
br label %91
28: ; preds = %19
%29 = load i32, i32* %10
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @S_ISCHR(i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
store i32 %38, i32* %2
br label %91
39: ; preds = %28
%40 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @major(i32 %41)
store i32 %42, i32* %7
%43 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @minor(i32 %44)
store i32 %45, i32* %8
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = call i32 @snprintf(i8* %46, i32 64, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 %48)
%50 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* %50, i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %39
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %58)
%60 = load i32, i32* @EXIT_FAILURE
store i32 %60, i32* %2
br label %91
61: ; preds = %39
%62 = load i32, i32* %10
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%64 = call i64 @read(i32 %62, i8* %63, i32 63)
store i64 %64, i64* %6
%65 = load i64, i64* %6
%66 = icmp ule i64 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %61
%68 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%69 = load i32, i32* %10
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* @EXIT_FAILURE
store i32 %71, i32* %2
br label %91
72: ; preds = %61
%73 = load i32, i32* %10
%74 = call i32 @close(i32 %73)
%75 = load i64, i64* %6
%76 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %75
store i8 0, i8* %76
%77 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%78 = call i64 @strtoul(i8* %77, i32* null, i32 0)
store i64 %78, i64* %9
%79 = load i64, i64* %9
%80 = trunc i64 %79 to i32
%81 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%82 = call i32 @if_indextoname(i32 %80, i8* %81)
%83 = icmp ne i32 %82, 0
br i1 %83, label %87, label %84
84: ; preds = %72
%85 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%86 = load i32, i32* @EXIT_FAILURE
store i32 %86, i32* %2
br label %91
87: ; preds = %72
%88 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%89 = load i64, i64* %9
%90 = call i32 @open_by_name_index(i8* %88, i64 %89)
store i32 %90, i32* %2
br label %91
91: ; preds = %87, %84, %67, %55, %35, %23, %16
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"interface name or device file expected\00"
@.str.3 = [35 x i8] c"/sys/dev/char/%u:%u/device/ifindex\00"
@.str.4 = [47 x i8] c"failed to get interface index from device: %s\0A\00"
@errno = global i32 0
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [30 x i8] c"failed to find interface name\00"
define i32 @open_by_node(i8* nocapture %0) {
%2 = alloca [64 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
br label %63
12: ; preds = %1
%13 = call i64 @fstat(i32 %7, %struct.stat* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @close(i32 %7)
%18 = load i32, i32* @EXIT_FAILURE
br label %63
19: ; preds = %12
%20 = tail call i32 @close(i32 %7)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = tail call i32 @S_ISCHR(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
br label %63
29: ; preds = %19
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i32 @major(i32 %31)
%33 = tail call i32 @minor(i32 %31)
%34 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %4, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %29
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
br label %63
44: ; preds = %29
%45 = call i64 @read(i32 %36, i8* %4, i32 63)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%49 = call i32 @close(i32 %36)
%50 = load i32, i32* @EXIT_FAILURE
br label %63
51: ; preds = %44
%52 = call i32 @close(i32 %36)
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %45
store i8 0, i8* %53
%54 = call i64 @strtoul(i8* nocapture %4, i32* null, i32 0)
%55 = trunc i64 %54 to i32
%56 = call i32 @if_indextoname(i32 %55, i8* %4)
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
br label %63
61: ; preds = %51
%62 = call i32 @open_by_name_index(i8* %4, i64 %54)
br label %63
63: ; preds = %61, %58, %47, %38, %25, %15, %9
%64 = phi i32 [ %11, %9 ], [ %18, %15 ], [ %43, %38 ], [ %50, %47 ], [ %62, %61 ], [ %60, %58 ], [ %28, %25 ]
ret i32 %64
}