#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*/ unit_buf;
struct output {
  int dummy;
};
struct ifreq {
  char *ifr_name;
};
typedef int /*<<< orphan*/ ethertap_buf;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ ENODEV;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ add_output(struct output *, char *, int);
int do_exec(char **, int /*<<< orphan*/, struct output *);
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ output_errno(struct output *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int maybe_insmod(char *dev, struct output *output) {
  struct ifreq ifr;
  int fd, unit;
  char unit_buf[sizeof("unit=nnn\0")];
  char ethertap_buf[sizeof("ethertapnnn\0")];
  char *ethertap_argv[] = {"modprobe", "ethertap",   unit_buf,
                           "-o",       ethertap_buf, NULL};
  char *netlink_argv[] = {"modprobe", "netlink_dev", NULL};
  char buf[256];
  if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    output_errno(output, "socket");
    return (-1);
  }
  strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name) - 1);
  ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0)
    return (0);
  if (errno != ENODEV) {
    output_errno(output, "SIOCGIFFLAGS on tap device");
    return (-1);
  }
  if (sscanf(dev, "tap%d", &unit) != 1) {
    snprintf(buf, sizeof(buf), "failed to get unit number from '%s'\n", dev);
    add_output(output, buf, -1);
    return (-1);
  }
  snprintf(unit_buf, sizeof(unit_buf) - 1, "unit=%d", unit);
  snprintf(ethertap_buf, sizeof(ethertap_buf) - 1, "ethertap%d", unit);
  do_exec(netlink_argv, 0, output);
  return (do_exec(ethertap_argv, 0, output));
}


x86 assembly (clang, -O0):
<maybe_insmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe66(%rip),%rax
mov    %rax,-0x70(%rbp)
lea    0xe64(%rip),%rax
mov    %rax,-0x68(%rbp)
lea    -0x32(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    -0x3f(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0x2c0d(%rip),%rax
mov    %rax,-0x90(%rbp)
mov    0x2c07(%rip),%rax
mov    %rax,-0x88(%rbp)
mov    0x2c01(%rip),%rax
mov    %rax,-0x80(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1234 <maybe_insmod+0xb4>
mov    -0x18(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1030 <output_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <maybe_insmod+0x1e3>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,0x7(%rax)
mov    -0x24(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jne    1279 <maybe_insmod+0xf9>
movl   $0x0,-0x4(%rbp)
jmp    1363 <maybe_insmod+0x1e3>
mov    0x2d50(%rip),%rax
mov    (%rax),%rax
mov    0x2d36(%rip),%rcx
cmp    (%rcx),%rax
je     12af <maybe_insmod+0x12f>
mov    -0x18(%rbp),%rdi
lea    0xd8a(%rip),%rsi
call   1030 <output_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <maybe_insmod+0x1e3>
mov    -0x10(%rbp),%rdi
lea    0xdbb(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     130b <maybe_insmod+0x18b>
lea    -0x190(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xd60(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x190(%rbp),%rsi
mov    $0xffffffff,%edx
call   1090 <add_output@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <maybe_insmod+0x1e3>
lea    -0x32(%rbp),%rdi
mov    -0x28(%rbp),%ecx
mov    $0x9,%esi
lea    0xd4a(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x3f(%rbp),%rdi
mov    -0x28(%rbp),%ecx
mov    $0xc,%esi
lea    0xd38(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <do_exec@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <do_exec@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maybe_insmod>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
test   %eax,%eax
jns    1173 <maybe_insmod+0x43>
lea    0xea2(%rip),%rsi
mov    %rbx,%rdi
call   1030 <output_errno@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>

LLVM IR (-O0):
%struct.output = type { i32 }
%struct.ifreq = type { i8* }
@.str = [9 x i8] c"modprobe\00"
@.str.1 = [9 x i8] c"ethertap\00"
@.str.2 = [3 x i8] c"-o\00"
@.str.3 = [12 x i8] c"netlink_dev\00"
@__const.maybe_insmod.netlink_argv = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i32 0, i32 0), i8* null]
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@SIOCGIFFLAGS = global i32 0
@errno = global i64 0
@ENODEV = global i64 0
@.str.5 = [27 x i8] c"SIOCGIFFLAGS on tap device\00"
@.str.6 = [6 x i8] c"tap%d\00"
@.str.7 = [37 x i8] c"failed to get unit number from '%s'\0A\00"
@.str.8 = [8 x i8] c"unit=%d\00"
@.str.9 = [11 x i8] c"ethertap%d\00"
define i32 @maybe_insmod(i8* %0, %struct.output* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.output*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i8]
%10 = alloca [13 x i8]
%11 = alloca [6 x i8*]
%12 = alloca [3 x i8*]
%13 = alloca [256 x i8]
store i8* %0, i8** %4
store %struct.output* %1, %struct.output** %5
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %11, i64 0, i64 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds i8*, i8** %14, i64 1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
store i8* %17, i8** %16
%18 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %18
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %10, i64 0, i64 0
store i8* %20, i8** %19
%21 = getelementptr inbounds i8*, i8** %19, i64 1
store i8* null, i8** %21
%22 = bitcast [3 x i8*]* %12 to i8*
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_DGRAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = load %struct.output*, %struct.output** %5
%29 = call i32 @output_errno(%struct.output* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %74
30: ; preds = %2
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = load i8*, i8** %4
%34 = call i32 @strncpy(i8* %32, i8* %33, i32 7)
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 7
store i8 0, i8* %37
%38 = load i32, i32* %7
%39 = load i32, i32* @SIOCGIFFLAGS
%40 = call i64 @ioctl(i32 %38, i32 %39, %struct.ifreq* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %30
store i32 0, i32* %3
br label %74
43: ; preds = %30
%44 = load i64, i64* @errno
%45 = load i64, i64* @ENODEV
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load %struct.output*, %struct.output** %5
%49 = call i32 @output_errno(%struct.output* %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %74
50: ; preds = %43
%51 = load i8*, i8** %4
%52 = call i32 @sscanf(i8* %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32* %8)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%56 = load i8*, i8** %4
%57 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %55, i32 256, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %56)
%58 = load %struct.output*, %struct.output** %5
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%60 = call i32 @add_output(%struct.output* %58, i8* %59, i32 -1)
store i32 -1, i32* %3
br label %74
61: ; preds = %50
%62 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%63 = load i32, i32* %8
%64 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %62, i32 9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i32 %63)
%65 = getelementptr inbounds [13 x i8], [13 x i8]* %10, i64 0, i64 0
%66 = load i32, i32* %8
%67 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %65, i32 12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %66)
%68 = getelementptr inbounds [3 x i8*], [3 x i8*]* %12, i64 0, i64 0
%69 = load %struct.output*, %struct.output** %5
%70 = call i32 @do_exec(i8** %68, i32 0, %struct.output* %69)
%71 = getelementptr inbounds [6 x i8*], [6 x i8*]* %11, i64 0, i64 0
%72 = load %struct.output*, %struct.output** %5
%73 = call i32 @do_exec(i8** %71, i32 0, %struct.output* %72)
store i32 %73, i32* %3
br label %74
74: ; preds = %61, %54, %47, %42, %27
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.output = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@SIOCGIFFLAGS = global i32 0
@errno = global i64 0
@ENODEV = global i64 0
define i32 @maybe_insmod(i8* %0, %struct.output* %1) {
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @output_errno(%struct.output* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
ret i32 -1
9: ; preds = %2
%10 = call i32 @strncpy(i8* undef, i8* %0, i32 7)
unreachable
}