#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*/ sysname;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ENOENT;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ slprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char *, char const *);
int /*<<< orphan*/ whine(char *);
int device_irq_number(const char *ifname) {
  int irq = 0;
  char *buffp;
  char buff[512];
  char sysname[512];
  if (!strncmp("lo", ifname, strlen("lo")))
    return 0;
  FILE *fp = fopen("/proc/interrupts", "r");
  if (!fp) {
    whine("Cannot open /proc/interrupts!\n");
    return -ENOENT;
  }
  memset(buff, 0, sizeof(buff));
  while (fgets(buff, sizeof(buff), fp) != NULL) {
    buff[sizeof(buff) - 1] = 0;
    if (strstr(buff, ifname) == NULL)
      continue;
    buffp = buff;
    while (*buffp != ':')
      buffp++;
    *buffp = 0;
    irq = atoi(buff);
    memset(buff, 0, sizeof(buff));
  }
  fclose(fp);
  if (irq != 0)
    return irq;
  slprintf(sysname, sizeof(sysname), "/sys/class/net/%s/device/irq", ifname);
  fp = fopen(sysname, "r");
  if (!fp)
    return -ENOENT;
  memset(buff, 0, sizeof(buff));
  if (fgets(buff, sizeof(buff), fp) != NULL) {
    buff[sizeof(buff) - 1] = 0;
    irq = atoi(buff);
  }
  fclose(fp);
  return irq;
}


x86 assembly (clang, -O0):
<device_irq_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    0xe38(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
lea    0xe23(%rip),%rdi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f7 <device_irq_number+0x57>
movl   $0x0,-0x4(%rbp)
jmp    13c2 <device_irq_number+0x222>
lea    0xe05(%rip),%rdi
lea    0xe0f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    123e <device_irq_number+0x9e>
lea    0xdf0(%rip),%rdi
call   1090 <whine@plt>
xor    %eax,%eax
mov    0x2da4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c2 <device_irq_number+0x222>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    -0x220(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12f0 <device_irq_number+0x150>
movb   $0x0,-0x21(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
jne    1296 <device_irq_number+0xf6>
jmp    1251 <device_irq_number+0xb1>
lea    -0x220(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
je     12c2 <device_irq_number+0x122>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    12a1 <device_irq_number+0x101>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x220(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
jmp    1251 <device_irq_number+0xb1>
mov    -0x428(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x14(%rbp)
je     1311 <device_irq_number+0x171>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c2 <device_irq_number+0x222>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xd0d(%rip),%rdx
call   1050 <slprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1368 <device_irq_number+0x1c8>
xor    %eax,%eax
mov    0x2c7a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c2 <device_irq_number+0x222>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    -0x220(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     13b0 <device_irq_number+0x210>
movb   $0x0,-0x21(%rbp)
lea    -0x220(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_irq_number>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r15
lea    0xe49(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     12e8 <device_irq_number+0x148>
lea    0xe28(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ed <device_irq_number+0x14d>
mov    %rax,%rbp
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %rbp,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     125a <device_irq_number+0xba>
movb   $0x0,0x1ff(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1210 <device_irq_number+0x70>
mov    %rbx,%rax
cmpb   $0x3a,(%rax)
lea    0x1(%rax),%rax
jne    1240 <device_irq_number+0xa0>
movb   $0x0,-0x1(%rax)
mov    %rbx,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
jmp    11f9 <device_irq_number+0x59>
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %r14d,%r14d
jne    1306 <device_irq_number+0x166>
lea    0xdc3(%rip),%rdx
lea    0x200(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rcx
call   1050 <slprintf@plt>
lea    0xd83(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12f9 <device_irq_number+0x159>
mov    %rax,%rbx
mov    %rsp,%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12de <device_irq_number+0x13e>
movb   $0x0,0x1ff(%rsp)
mov    %rsp,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1306 <device_irq_number+0x166>
xor    %r14d,%r14d
jmp    1306 <device_irq_number+0x166>
lea    0xd22(%rip),%rdi
call   1090 <whine@plt>
mov    0x2cd8(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"lo\00"
@.str.1 = [17 x i8] c"/proc/interrupts\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [31 x i8] c"Cannot open /proc/interrupts!\0A\00"
@ENOENT = global i32 0
@.str.4 = [29 x i8] c"/sys/class/net/%s/device/irq\00"
define i32 @device_irq_number(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [512 x i8]
%7 = alloca [512 x i8]
%8 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %4
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %1
store i32 0, i32* %2
br label %86
14: ; preds = %1
%15 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = call i32 @whine(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* @ENOENT
%21 = sub nsw i32 0, %20
store i32 %21, i32* %2
br label %86
22: ; preds = %14
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 512)
br label %25
25: ; preds = %47, %36, %22
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%27 = load i32*, i32** %8
%28 = call i32* @fgets(i8* %26, i32 512, i32* %27)
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %53
30: ; preds = %25
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 511
store i8 0, i8* %31
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%33 = load i8*, i8** %3
%34 = call i32* @strstr(i8* %32, i8* %33)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %30
br label %25
37: ; preds = %30
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
store i8* %38, i8** %5
br label %39
39: ; preds = %44, %37
%40 = load i8*, i8** %5
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 58
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %5
br label %39
47: ; preds = %39
%48 = load i8*, i8** %5
store i8 0, i8* %48
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%50 = call i32 @atoi(i8* %49)
store i32 %50, i32* %4
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%52 = call i32 @memset(i8* %51, i32 0, i32 512)
br label %25
53: ; preds = %25
%54 = load i32*, i32** %8
%55 = call i32 @fclose(i32* %54)
%56 = load i32, i32* %4
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = load i32, i32* %4
store i32 %59, i32* %2
br label %86
60: ; preds = %53
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%62 = load i8*, i8** %3
%63 = call i32 @slprintf(i8* %61, i32 512, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %62)
%64 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%65 = call i32* @fopen(i8* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %65, i32** %8
%66 = load i32*, i32** %8
%67 = icmp ne i32* %66, null
br i1 %67, label %71, label %68
68: ; preds = %60
%69 = load i32, i32* @ENOENT
%70 = sub nsw i32 0, %69
store i32 %70, i32* %2
br label %86
71: ; preds = %60
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%73 = call i32 @memset(i8* %72, i32 0, i32 512)
%74 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%75 = load i32*, i32** %8
%76 = call i32* @fgets(i8* %74, i32 512, i32* %75)
%77 = icmp ne i32* %76, null
br i1 %77, label %78, label %82
78: ; preds = %71
%79 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 511
store i8 0, i8* %79
%80 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%81 = call i32 @atoi(i8* %80)
store i32 %81, i32* %4
br label %82
82: ; preds = %78, %71
%83 = load i32*, i32** %8
%84 = call i32 @fclose(i32* %83)
%85 = load i32, i32* %4
store i32 %85, i32* %2
br label %86
86: ; preds = %82, %68, %58, %18, %13
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
@.str = [3 x i8] c"lo\00"
@.str.1 = [17 x i8] c"/proc/interrupts\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [31 x i8] c"Cannot open /proc/interrupts!\0A\00"
@ENOENT = global i32 0
@.str.4 = [29 x i8] c"/sys/class/net/%s/device/irq\00"
define i32 @device_irq_number(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %54, label %9
9: ; preds = %1
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = tail call i32 @whine(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32, i32* @ENOENT
%15 = sub nsw i32 0, %14
br label %54
16: ; preds = %9
%17 = call i32 @memset(i8* %4, i32 0, i32 512)
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 511
br label %19
19: ; preds = %32, %16
%20 = phi i32 [ %33, %32 ], [ 0, %16 ]
br label %21
21: ; preds = %19, %24
%22 = call i32* @fgets(i8* %4, i32 512, i32* %10)
%23 = icmp eq i32* %22, null
br i1 %23, label %35, label %24
24: ; preds = %21
store i8 0, i8* %18
%25 = call i32* @strstr(i8* %4, i8* dereferenceable(1) %0)
%26 = icmp eq i32* %25, null
br i1 %26, label %21, label %27
27: ; preds = %24, %27
%28 = phi i8* [ %31, %27 ], [ %4, %24 ]
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 58
%31 = getelementptr inbounds i8, i8* %28, i64 1
br i1 %30, label %32, label %27
32: ; preds = %27
store i8 0, i8* %28
%33 = call i32 @atoi(i8* %4)
%34 = call i32 @memset(i8* %4, i32 0, i32 512)
br label %19
35: ; preds = %21
%36 = call i32 @fclose(i32* %10)
%37 = icmp eq i32 %20, 0
br i1 %37, label %38, label %54
38: ; preds = %35
%39 = call i32 @slprintf(i8* %5, i32 512, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%40 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @ENOENT
%44 = sub nsw i32 0, %43
br label %54
45: ; preds = %38
%46 = call i32 @memset(i8* %4, i32 0, i32 512)
%47 = call i32* @fgets(i8* %4, i32 512, i32* %40)
%48 = icmp eq i32* %47, null
br i1 %48, label %51, label %49
49: ; preds = %45
store i8 0, i8* %18
%50 = call i32 @atoi(i8* %4)
br label %51
51: ; preds = %49, %45
%52 = phi i32 [ %50, %49 ], [ 0, %45 ]
%53 = call i32 @fclose(i32* %40)
br label %54
54: ; preds = %12, %42, %51, %35, %1
%55 = phi i32 [ 0, %1 ], [ %52, %51 ], [ %44, %42 ], [ %15, %12 ], [ %20, %35 ]
ret i32 %55
}