#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 sockaddr_in {
  int /*<<< orphan*/ sin_family;
};
struct ifreq {
  int ifr_ifindex;
  int ifr_flags;
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFF_MULTICAST;
int IFF_RUNNING;
int IFF_UP;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
void if_prepare(const char *nic) {
  int sockd;
  if ((sockd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
    perror("Failed to create socket");
    exit(1);
  }
  struct ifreq interface;
  strncpy(interface.ifr_name, nic, sizeof(interface.ifr_name));
  int nic_index;
  if (ioctl(sockd, SIOCGIFINDEX, &interface) == -1) {
    perror("Failed to obtain Index of interface");
    exit(1);
  }
  nic_index = interface.ifr_ifindex;
  int multicasting = 0;
  if (ioctl(sockd, SIOCGIFFLAGS, &interface) == -1) {
    perror("Failed to read interface flags");
    exit(1);
  }
  multicasting = interface.ifr_flags;
  multicasting &= IFF_MULTICAST;
  struct sockaddr_in *addr = (struct sockaddr_in *)&interface.ifr_addr;
  memset(addr, 0, sizeof(interface.ifr_addr));
  addr->sin_family = AF_INET;
  if (ioctl(sockd, SIOCSIFADDR, &interface) == -1) {
    perror("Failed to change NIC's IP Address to 0");
    exit(1);
  }
  int flags = 0;
  flags |= IFF_UP;
  flags |= IFF_RUNNING;
  interface.ifr_flags |= flags;
  if (ioctl(sockd, SIOCSIFFLAGS, &interface) == -1) {
    perror("Could not set the new flags");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<if_prepare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11ae <if_prepare+0x4e>
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11f3 <if_prepare+0x93>
lea    0xe34(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1234 <if_prepare+0xd4>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12a8 <if_prepare+0x148>
lea    0xdc2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x34(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    130a <if_prepare+0x1aa>
lea    0xd87(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_prepare>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e6d(%rip),%r15
mov    (%r15),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1241 <if_prepare+0xe1>
mov    %eax,%ebx
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     124a <if_prepare+0xea>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1253 <if_prepare+0xf3>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     125c <if_prepare+0xfc>
mov    0x2db8(%rip),%rax
mov    0x2db9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x4(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1265 <if_prepare+0x105>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdb8(%rip),%rdi
jmp    126c <if_prepare+0x10c>
lea    0xdc7(%rip),%rdi
jmp    126c <if_prepare+0x10c>
lea    0xde2(%rip),%rdi
jmp    126c <if_prepare+0x10c>
lea    0xdf8(%rip),%rdi
jmp    126c <if_prepare+0x10c>
lea    0xe16(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [36 x i8] c"Failed to obtain Index of interface\00"
@SIOCGIFFLAGS = global i32 0
@.str.2 = [31 x i8] c"Failed to read interface flags\00"
@IFF_MULTICAST = global i32 0
@SIOCSIFADDR = global i32 0
@.str.3 = [39 x i8] c"Failed to change NIC's IP Address to 0\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.4 = [28 x i8] c"Could not set the new flags\00"
define void @if_prepare(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_RAW
%11 = load i32, i32* @IPPROTO_RAW
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 3
%19 = load i32, i32* %18
%20 = load i8*, i8** %2
%21 = call i32 @strncpy(i32 %19, i8* %20, i32 4)
%22 = load i32, i32* %3
%23 = load i32, i32* @SIOCGIFINDEX
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%31 = load i32, i32* %30
store i32 %31, i32* %5
store i32 0, i32* %6
%32 = load i32, i32* %3
%33 = load i32, i32* @SIOCGIFFLAGS
%34 = call i32 @ioctl(i32 %32, i32 %33, %struct.ifreq* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %29
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%41 = load i32, i32* %40
store i32 %41, i32* %6
%42 = load i32, i32* @IFF_MULTICAST
%43 = load i32, i32* %6
%44 = and i32 %43, %42
store i32 %44, i32* %6
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 2
%46 = bitcast i32* %45 to %struct.sockaddr_in*
store %struct.sockaddr_in* %46, %struct.sockaddr_in** %7
%47 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%48 = call i32 @memset(%struct.sockaddr_in* %47, i32 0, i32 4)
%49 = load i32, i32* @AF_INET
%50 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i32, i32* %3
%53 = load i32, i32* @SIOCSIFADDR
%54 = call i32 @ioctl(i32 %52, i32 %53, %struct.ifreq* %4)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %39
%57 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %39
store i32 0, i32* %8
%60 = load i32, i32* @IFF_UP
%61 = load i32, i32* %8
%62 = or i32 %61, %60
store i32 %62, i32* %8
%63 = load i32, i32* @IFF_RUNNING
%64 = load i32, i32* %8
%65 = or i32 %64, %63
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%68 = load i32, i32* %67
%69 = or i32 %68, %66
store i32 %69, i32* %67
%70 = load i32, i32* %3
%71 = load i32, i32* @SIOCSIFFLAGS
%72 = call i32 @ioctl(i32 %70, i32 %71, %struct.ifreq* %4)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %59
%75 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %59
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [36 x i8] c"Failed to obtain Index of interface\00"
@SIOCGIFFLAGS = global i32 0
@.str.2 = [31 x i8] c"Failed to read interface flags\00"
@IFF_MULTICAST = global i32 0
@SIOCSIFADDR = global i32 0
@.str.3 = [39 x i8] c"Failed to change NIC's IP Address to 0\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.4 = [28 x i8] c"Could not set the new flags\00"
define void @if_prepare(i8* %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @IPPROTO_RAW
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = bitcast %struct.ifreq* %2 to i8*
%13 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%14 = load i32, i32* @SIOCGIFINDEX
%15 = call i32 @ioctl(i32 %6, i32 %14, %struct.ifreq* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = load i32, i32* @SIOCGIFFLAGS
%22 = call i32 @ioctl(i32 %6, i32 %21, %struct.ifreq* %2)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 2
%30 = bitcast i32* %29 to %struct.sockaddr_in*
%31 = call i32 @memset(%struct.sockaddr_in* %30, i32 0, i32 4)
%32 = load i32, i32* @AF_INET
store i32 %32, i32* %29
%33 = load i32, i32* @SIOCSIFADDR
%34 = call i32 @ioctl(i32 %6, i32 %33, %struct.ifreq* %2)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %27
%40 = load i32, i32* @IFF_UP
%41 = load i32, i32* @IFF_RUNNING
%42 = or i32 %41, %40
%43 = load i32, i32* %28
%44 = or i32 %42, %43
store i32 %44, i32* %28
%45 = load i32, i32* @SIOCSIFFLAGS
%46 = call i32 @ioctl(i32 %6, i32 %45, %struct.ifreq* %2)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %39
ret void
}