#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 ifreq {
  int ifr_flags;
  int /*<<< orphan*/ ifr_name;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ EAFNOSUPPORT;
int IFF_RUNNING;
int IFF_UP;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ die2(char *, char const *);
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
void init_network(const char *iface, int domain) {
  int s = socket(domain, SOCK_DGRAM, IPPROTO_IP);
  if (s < 0) {
    if (errno == EAFNOSUPPORT) {
      return;
    }
    die("socket");
  }
  struct ifreq request = {0};
  strncpy(request.ifr_name, iface, sizeof(request.ifr_name));
  if (ioctl(s, SIOCGIFFLAGS, &request) < 0) {
    die2("ioctl(SIOCGIFFLAGS)", iface);
  }
  request.ifr_flags |= IFF_UP | IFF_RUNNING;
  if (ioctl(s, SIOCSIFFLAGS, &request) < 0) {
    die2("ioctl(SIOCSIFFLAGS)", iface);
  }
  close(s);
}


x86 assembly (clang, -O0):
<init_network>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d1 <init_network+0x61>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <init_network+0x55>
jmp    1270 <init_network+0x100>
lea    0xe34(%rip),%rdi
call   1080 <die@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1221 <init_network+0xb1>
mov    -0x8(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1060 <die2@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    0x2da7(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1268 <init_network+0xf8>
mov    -0x8(%rbp),%rsi
lea    0xdb8(%rip),%rdi
call   1060 <die2@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_network>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%eax
mov    %rdi,%rbx
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e65(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b0 <init_network+0x50>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     123f <init_network+0xdf>
lea    0xe55(%rip),%rdi
call   1070 <die@plt>
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jns    11f9 <init_network+0x99>
lea    0xe16(%rip),%rdi
mov    %rbx,%rsi
call   1050 <die2@plt>
mov    0x2dd0(%rip),%rax
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jns    1238 <init_network+0xd8>
lea    0xdeb(%rip),%rdi
mov    %rbx,%rsi
call   1050 <die2@plt>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32 }
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@errno = global i64 0
@EAFNOSUPPORT = global i64 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [20 x i8] c"ioctl(SIOCGIFFLAGS)\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [20 x i8] c"ioctl(SIOCSIFFLAGS)\00"
define void @init_network(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_IP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAFNOSUPPORT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
br label %50
18: ; preds = %13
%19 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = bitcast %struct.ifreq* %6 to i8*
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i8*, i8** %3
%25 = call i32 @strncpy(i32 %23, i8* %24, i32 4)
%26 = load i32, i32* %5
%27 = load i32, i32* @SIOCGIFFLAGS
%28 = call i64 @ioctl(i32 %26, i32 %27, %struct.ifreq* %6)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = call i32 @die2(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %20
%34 = load i32, i32* @IFF_UP
%35 = load i32, i32* @IFF_RUNNING
%36 = or i32 %34, %35
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = or i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* %5
%41 = load i32, i32* @SIOCSIFFLAGS
%42 = call i64 @ioctl(i32 %40, i32 %41, %struct.ifreq* %6)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %33
%45 = load i8*, i8** %3
%46 = call i32 @die2(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %44, %33
%48 = load i32, i32* %5
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %17
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32 }
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@errno = global i64 0
@EAFNOSUPPORT = global i64 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [20 x i8] c"ioctl(SIOCGIFFLAGS)\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [20 x i8] c"ioctl(SIOCSIFFLAGS)\00"
define void @init_network(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = load i32, i32* @SOCK_DGRAM
%5 = load i32, i32* @IPPROTO_IP
%6 = tail call i32 @socket(i32 %1, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAFNOSUPPORT
%11 = icmp eq i64 %9, %10
br i1 %11, label %36, label %12
12: ; preds = %8
%13 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = bitcast %struct.ifreq* %3 to i8*
%16 = tail call i32 @strncpy(i32 0, i8* %0, i32 4)
%17 = load i32, i32* @SIOCGIFFLAGS
%18 = call i64 @ioctl(i32 %6, i32 %17, %struct.ifreq* %3)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @die2(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %20, %14
%23 = load i32, i32* @IFF_UP
%24 = load i32, i32* @IFF_RUNNING
%25 = or i32 %24, %23
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %25, %27
store i32 %28, i32* %26
%29 = load i32, i32* @SIOCSIFFLAGS
%30 = call i64 @ioctl(i32 %6, i32 %29, %struct.ifreq* %3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 @die2(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %34
34: ; preds = %32, %22
%35 = call i32 @close(i32 %6)
br label %36
36: ; preds = %8, %34
ret void
}