#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ t_drv_buf;
struct TYPE_2__ {
  unsigned short base_addr;
  unsigned char irq;
};
struct ifreq {
  char *ifr_name;
  short ifr_flags;
  TYPE_1__ ifr_map;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr_buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFMAP;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned short);
int sscanf(char *, char *, char *, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int check_driver(int skfd, char *ifname, char *drvname, char *version,
                 unsigned short int *base_addr, unsigned char *irq,
                 short int *flags) {
  char buf[1024];
  char addr_buf[4 + 1];
  char t_addr_buf[4 + 1];
  char t_drv_buf[24 + 1];
  struct ifreq ifr;
  FILE *fh;
  strncpy(ifr.ifr_name, ifname, 16);
  if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
    return (-1);
  else {
    *base_addr = ifr.ifr_map.base_addr;
    *irq = ifr.ifr_map.irq;
  }
  if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
    return (-1);
  else {
    *flags = ifr.ifr_flags;
  }
  memset(addr_buf, 0, sizeof(addr_buf));
  snprintf(addr_buf, sizeof(addr_buf), "%04x", *base_addr);
  fh = fopen("/proc/ioports", "r");
  if (fh != NULL) {
    int result;
    do {
      memset(buf, 0, sizeof(buf));
      if (fgets(buf, sizeof(buf), fh) != NULL) {
        result = sscanf(buf, " %04s%*s : %s", t_addr_buf, t_drv_buf);
        if (!strncmp(addr_buf, t_addr_buf, sizeof(addr_buf)))
          strncpy(drvname, t_drv_buf, sizeof(t_drv_buf));
      } else
        break;
    } while (result > 0);
  }
  return (1);
}


x86 assembly (clang, -O0):
<check_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x470(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    -0x470(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    11e9 <check_driver+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1358 <check_driver+0x1d8>
mov    -0x466(%rbp),%cx
mov    -0x28(%rbp),%rax
mov    %cx,(%rax)
mov    -0x464(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x470(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    1231 <check_driver+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1358 <check_driver+0x1d8>
mov    -0x468(%rbp),%cx
mov    0x10(%rbp),%rax
mov    %cx,(%rax)
lea    -0x435(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1060 <memset@plt>
lea    -0x435(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    $0x5,%esi
lea    0xd97(%rip),%rdx
movzwl (%rax),%ecx
call   1050 <snprintf@plt>
lea    0xd8d(%rip),%rdi
lea    0xd94(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x478(%rbp)
cmpq   $0x0,-0x478(%rbp)
je     1351 <check_driver+0x1d1>
jmp    129e <check_driver+0x11e>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x478(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1335 <check_driver+0x1b5>
lea    -0x430(%rbp),%rdi
lea    -0x43a(%rbp),%rdx
lea    -0x460(%rbp),%rcx
lea    0xd26(%rip),%rsi
call   1090 <sscanf@plt>
mov    %eax,-0x47c(%rbp)
lea    -0x435(%rbp),%rdi
lea    -0x43a(%rbp),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    1330 <check_driver+0x1b0>
mov    -0x18(%rbp),%rdi
lea    -0x460(%rbp),%rsi
mov    $0x19,%edx
call   1030 <strncpy@plt>
jmp    133a <check_driver+0x1ba>
jmp    134c <check_driver+0x1cc>
jmp    133f <check_driver+0x1bf>
cmpl   $0x0,-0x47c(%rbp)
jg     129e <check_driver+0x11e>
jmp    1351 <check_driver+0x1d1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_driver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    %r9,%rbx
mov    %r8,%r14
mov    %rdx,0x20(%rsp)
mov    %edi,%ebp
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
js     12df <check_driver+0x15f>
movzwl 0x1a(%rsp),%eax
mov    %ax,(%r14)
mov    0x1c(%rsp),%al
mov    %al,(%rbx)
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
js     12df <check_driver+0x15f>
mov    0x490(%rsp),%rax
movzwl 0x18(%rsp),%ecx
mov    %cx,(%rax)
lea    0xb(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1060 <memset@plt>
movzwl (%r14),%ecx
lea    0xdd8(%rip),%rdx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1050 <snprintf@plt>
lea    0xdc9(%rip),%rdi
lea    0xdd0(%rip),%rsi
call   10a0 <fopen@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     12df <check_driver+0x15f>
mov    %rax,%rbx
lea    0x50(%rsp),%rbp
lea    0x2b(%rsp),%r13
lea    0x30(%rsp),%r14
lea    0xb(%rsp),%r15
jmp    1275 <check_driver+0xf5>
test   %r12d,%r12d
jle    12d9 <check_driver+0x159>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12d9 <check_driver+0x159>
mov    %rbp,%rdi
lea    0xd72(%rip),%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1090 <sscanf@plt>
mov    %eax,%r12d
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1270 <check_driver+0xf0>
mov    0x20(%rsp),%rdi
mov    %r14,%rsi
mov    $0x19,%edx
call   1030 <strncpy@plt>
jmp    1270 <check_driver+0xf0>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x458,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, i16, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i16, i8 }
@SIOCGIFMAP = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str = [5 x i8] c"%04x\00"
@.str.1 = [14 x i8] c"/proc/ioports\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [14 x i8] c" %04s%*s : %s\00"
define i32 @check_driver(i32 %0, i8* %1, i8* %2, i8* %3, i16* %4, i8* %5, i16* %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i16*
%14 = alloca i8*
%15 = alloca i16*
%16 = alloca [1024 x i8]
%17 = alloca [5 x i8]
%18 = alloca [5 x i8]
%19 = alloca [25 x i8]
%20 = alloca %struct.ifreq
%21 = alloca i32*
%22 = alloca i32
store i32 %0, i32* %9
store i8* %1, i8** %10
store i8* %2, i8** %11
store i8* %3, i8** %12
store i16* %4, i16** %13
store i8* %5, i8** %14
store i16* %6, i16** %15
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %20, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = load i8*, i8** %10
%26 = call i32 @strncpy(i8* %24, i8* %25, i32 16)
%27 = load i32, i32* %9
%28 = load i32, i32* @SIOCGIFMAP
%29 = call i64 @ioctl(i32 %27, i32 %28, %struct.ifreq* %20)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %7
store i32 -1, i32* %8
br label %90
32: ; preds = %7
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %20, i32 0, i32 2
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i16, i16* %34
%36 = load i16*, i16** %13
store i16 %35, i16* %36
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %20, i32 0, i32 2
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 1
%39 = load i8, i8* %38
%40 = load i8*, i8** %14
store i8 %39, i8* %40
br label %41
41: ; preds = %32
%42 = load i32, i32* %9
%43 = load i32, i32* @SIOCGIFFLAGS
%44 = call i64 @ioctl(i32 %42, i32 %43, %struct.ifreq* %20)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %8
br label %90
47: ; preds = %41
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %20, i32 0, i32 1
%49 = load i16, i16* %48
%50 = load i16*, i16** %15
store i16 %49, i16* %50
br label %51
51: ; preds = %47
%52 = getelementptr inbounds [5 x i8], [5 x i8]* %17, i64 0, i64 0
%53 = call i32 @memset(i8* %52, i32 0, i32 5)
%54 = getelementptr inbounds [5 x i8], [5 x i8]* %17, i64 0, i64 0
%55 = load i16*, i16** %13
%56 = load i16, i16* %55
%57 = call i32 @snprintf(i8* %54, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i16 zeroext %56)
%58 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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* %58, i32** %21
%59 = load i32*, i32** %21
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %89
61: ; preds = %51
br label %62
62: ; preds = %85, %61
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%64 = call i32 @memset(i8* %63, i32 0, i32 1024)
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%66 = load i32*, i32** %21
%67 = call i32* @fgets(i8* %65, i32 1024, i32* %66)
%68 = icmp ne i32* %67, null
br i1 %68, label %69, label %83
69: ; preds = %62
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%71 = getelementptr inbounds [5 x i8], [5 x i8]* %18, i64 0, i64 0
%72 = getelementptr inbounds [25 x i8], [25 x i8]* %19, i64 0, i64 0
%73 = call i32 @sscanf(i8* %70, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %71, i8* %72)
store i32 %73, i32* %22
%74 = getelementptr inbounds [5 x i8], [5 x i8]* %17, i64 0, i64 0
%75 = getelementptr inbounds [5 x i8], [5 x i8]* %18, i64 0, i64 0
%76 = call i32 @strncmp(i8* %74, i8* %75, i32 5)
%77 = icmp ne i32 %76, 0
br i1 %77, label %82, label %78
78: ; preds = %69
%79 = load i8*, i8** %11
%80 = getelementptr inbounds [25 x i8], [25 x i8]* %19, i64 0, i64 0
%81 = call i32 @strncpy(i8* %79, i8* %80, i32 25)
br label %82
82: ; preds = %78, %69
br label %84
83: ; preds = %62
br label %88
84: ; preds = %82
br label %85
85: ; preds = %84
%86 = load i32, i32* %22
%87 = icmp sgt i32 %86, 0
br i1 %87, label %62, label %88
88: ; preds = %85, %83
br label %89
89: ; preds = %88, %51
store i32 1, i32* %8
br label %90
90: ; preds = %89, %46, %31
%91 = load i32, i32* %8
ret i32 %91
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i16, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i16, i8 }
@SIOCGIFMAP = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str = [5 x i8] c"%04x\00"
@.str.1 = [14 x i8] c"/proc/ioports\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [14 x i8] c" %04s%*s : %s\00"
define i32 @check_driver(i32 %0, i8* %1, i8* %2, i8* nocapture readnone %3, i16* nocapture %4, i8* nocapture writeonly %5, i16* nocapture writeonly %6) {
%8 = alloca [1024 x i8]
%9 = alloca [5 x i8]
%10 = alloca [5 x i8]
%11 = alloca [25 x i8]
%12 = alloca %struct.ifreq
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%16 = getelementptr inbounds [25 x i8], [25 x i8]* %11, i64 0, i64 0
%17 = bitcast %struct.ifreq* %12 to i8*
%18 = tail call i32 @strncpy(i8* undef, i8* %1, i32 16)
%19 = load i32, i32* @SIOCGIFMAP
%20 = call i64 @ioctl(i32 %0, i32 %19, %struct.ifreq* %12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %50, label %22
22: ; preds = %7
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i64 0, i32 2, i32 0
%24 = load i16, i16* %23
store i16 %24, i16* %4
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i64 0, i32 2, i32 1
%26 = load i8, i8* %25
store i8 %26, i8* %5
%27 = load i32, i32* @SIOCGIFFLAGS
%28 = call i64 @ioctl(i32 %0, i32 %27, %struct.ifreq* %12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %50, label %30
30: ; preds = %22
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i64 0, i32 1
%32 = load i16, i16* %31
store i16 %32, i16* %6
%33 = call i32 @memset(i8* %14, i32 0, i32 5)
%34 = load i16, i16* %4
%35 = call i32 @snprintf(i8* %14, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i16 zeroext %34)
%36 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %50, label %38
38: ; preds = %30, %48
%39 = call i32 @memset(i8* %13, i32 0, i32 1024)
%40 = call i32* @fgets(i8* %13, i32 1024, i32* %36)
%41 = icmp eq i32* %40, null
br i1 %41, label %50, label %42
42: ; preds = %38
%43 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %15, i8* %16)
%44 = call i32 @strncmp(i8* %14, i8* %15, i32 5)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @strncpy(i8* %2, i8* %16, i32 25)
br label %48
48: ; preds = %46, %42
%49 = icmp sgt i32 %43, 0
br i1 %49, label %38, label %50
50: ; preds = %38, %48, %30, %22, %7
%51 = phi i32 [ -1, %7 ], [ -1, %22 ], [ 1, %30 ], [ 1, %48 ], [ 1, %38 ]
ret i32 %51
}