#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_8__ TYPE_4__;
typedef struct TYPE_7__ TYPE_3__;
typedef struct TYPE_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_3__ sin_addr;
};
struct in_addr {
  int s_addr;
};
struct TYPE_8__ {
  void *sa_family;
};
struct TYPE_6__ {
  void *sa_family;
};
struct TYPE_5__ {
  void *sa_family;
};
struct ifreq {
  int ifr_flags;
  TYPE_4__ ifr_broadaddr;
  TYPE_2__ ifr_netmask;
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SIOCSIFBRDADDR;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SIOCSIFNETMASK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int ifconfig(char *name, int flags, char *addr, char *netmask) {
  int s;
  struct ifreq ifr;
  struct in_addr in_addr, in_netmask, in_broadaddr;
  if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
    goto err;
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  ifr.ifr_flags = flags;
  if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
    goto err;
  if (addr) {
    inet_aton(addr, &in_addr);
    (((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr).s_addr = in_addr.s_addr;
    ifr.ifr_addr.sa_family = AF_INET;
    if (ioctl(s, SIOCSIFADDR, &ifr) < 0)
      goto err;
  }
  if (addr && netmask) {
    inet_aton(netmask, &in_netmask);
    (((struct sockaddr_in *)(&ifr.ifr_netmask))->sin_addr).s_addr =
        in_netmask.s_addr;
    ifr.ifr_netmask.sa_family = AF_INET;
    if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0)
      goto err;
    in_broadaddr.s_addr =
        (in_addr.s_addr & in_netmask.s_addr) | ~in_netmask.s_addr;
    (((struct sockaddr_in *)(&ifr.ifr_broadaddr))->sin_addr).s_addr =
        in_broadaddr.s_addr;
    ifr.ifr_broadaddr.sa_family = AF_INET;
    if (ioctl(s, SIOCSIFBRDADDR, &ifr) < 0)
      goto err;
  }
  close(s);
  return 0;
err:
  close(s);
  perror(name);
  return errno;
}


x86 assembly (clang, -O0):
<ifconfig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11a9 <ifconfig+0x49>
jmp    12fa <ifconfig+0x19a>
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11e8 <ifconfig+0x88>
jmp    12fa <ifconfig+0x19a>
cmpq   $0x0,-0x20(%rbp)
je     123d <ifconfig+0xdd>
mov    -0x20(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1060 <inet_aton@plt>
mov    -0x60(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1238 <ifconfig+0xd8>
jmp    12fa <ifconfig+0x19a>
jmp    123d <ifconfig+0xdd>
cmpq   $0x0,-0x20(%rbp)
je     12e6 <ifconfig+0x186>
cmpq   $0x0,-0x28(%rbp)
je     12e6 <ifconfig+0x186>
mov    -0x28(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   1060 <inet_aton@plt>
mov    -0x68(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1298 <ifconfig+0x138>
jmp    12fa <ifconfig+0x19a>
mov    -0x60(%rbp),%eax
and    -0x68(%rbp),%eax
mov    -0x68(%rbp),%ecx
xor    $0xffffffff,%ecx
or     %ecx,%eax
mov    %eax,-0x70(%rbp)
mov    -0x70(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    12e1 <ifconfig+0x181>
jmp    12fa <ifconfig+0x19a>
jmp    12e6 <ifconfig+0x186>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1317 <ifconfig+0x1b7>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2ca6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifconfig>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e60(%rip),%r13
mov    0x0(%r13),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     127f <ifconfig+0x11f>
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    %ebx,0x10(%rsp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     127f <ifconfig+0x11f>
test   %r12,%r12
je     1267 <ifconfig+0x107>
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1060 <inet_aton@plt>
mov    0x0(%r13),%rax
mov    %rax,0x28(%rsp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     127f <ifconfig+0x11f>
test   %r15,%r15
je     1267 <ifconfig+0x107>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1060 <inet_aton@plt>
mov    0x0(%r13),%rax
mov    %rax,0x20(%rsp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     127f <ifconfig+0x11f>
mov    0x0(%r13),%rax
mov    %rax,0x18(%rsp)
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     127f <ifconfig+0x11f>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
jmp    1270 <ifconfig+0x110>

LLVM IR (-O0):
%struct.ifreq = type { i32, %struct.TYPE_8__, %struct.TYPE_6__, %struct.TYPE_5__, i32 }
%struct.TYPE_8__ = type { i8* }
%struct.TYPE_6__ = type { i8* }
%struct.TYPE_5__ = type { i8* }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_7__ }
%struct.TYPE_7__ = type { i32 }
@AF_INET = global i8* null
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@IFNAMSIZ = global i32 0
@SIOCSIFFLAGS = global i32 0
@SIOCSIFADDR = global i32 0
@SIOCSIFNETMASK = global i32 0
@SIOCSIFBRDADDR = global i32 0
@errno = global i32 0
define i32 @ifconfig(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.ifreq
%12 = alloca %struct.in_addr
%13 = alloca %struct.in_addr
%14 = alloca %struct.in_addr
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%15 = load i8*, i8** @AF_INET
%16 = load i32, i32* @SOCK_RAW
%17 = load i32, i32* @IPPROTO_RAW
%18 = call i32 @socket(i8* %15, i32 %16, i32 %17)
store i32 %18, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
br label %107
21: ; preds = %4
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 4
%23 = load i32, i32* %22
%24 = load i8*, i8** %6
%25 = load i32, i32* @IFNAMSIZ
%26 = call i32 @strncpy(i32 %23, i8* %24, i32 %25)
%27 = load i32, i32* %7
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %10
%30 = load i32, i32* @SIOCSIFFLAGS
%31 = call i64 @ioctl(i32 %29, i32 %30, %struct.ifreq* %11)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %21
br label %107
34: ; preds = %21
%35 = load i8*, i8** %8
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %55
37: ; preds = %34
%38 = load i8*, i8** %8
%39 = call i32 @inet_aton(i8* %38, %struct.in_addr* %12)
%40 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 3
%43 = bitcast %struct.TYPE_5__* %42 to %struct.sockaddr_in*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i32 0, i32 0
%45 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %44, i32 0, i32 0
store i32 %41, i32* %45
%46 = load i8*, i8** @AF_INET
%47 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 3
%48 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %47, i32 0, i32 0
store i8* %46, i8** %48
%49 = load i32, i32* %10
%50 = load i32, i32* @SIOCSIFADDR
%51 = call i64 @ioctl(i32 %49, i32 %50, %struct.ifreq* %11)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %37
br label %107
54: ; preds = %37
br label %55
55: ; preds = %54, %34
%56 = load i8*, i8** %8
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %104
58: ; preds = %55
%59 = load i8*, i8** %9
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %104
61: ; preds = %58
%62 = load i8*, i8** %9
%63 = call i32 @inet_aton(i8* %62, %struct.in_addr* %13)
%64 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i32 0, i32 0
%65 = load i32, i32* %64
%66 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 2
%67 = bitcast %struct.TYPE_6__* %66 to %struct.sockaddr_in*
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %67, i32 0, i32 0
%69 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %68, i32 0, i32 0
store i32 %65, i32* %69
%70 = load i8*, i8** @AF_INET
%71 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 2
%72 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %71, i32 0, i32 0
store i8* %70, i8** %72
%73 = load i32, i32* %10
%74 = load i32, i32* @SIOCSIFNETMASK
%75 = call i64 @ioctl(i32 %73, i32 %74, %struct.ifreq* %11)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %61
br label %107
78: ; preds = %61
%79 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
%80 = load i32, i32* %79
%81 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i32 0, i32 0
%82 = load i32, i32* %81
%83 = and i32 %80, %82
%84 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i32 0, i32 0
%85 = load i32, i32* %84
%86 = xor i32 %85, -1
%87 = or i32 %83, %86
%88 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %14, i32 0, i32 0
store i32 %87, i32* %88
%89 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %14, i32 0, i32 0
%90 = load i32, i32* %89
%91 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 1
%92 = bitcast %struct.TYPE_8__* %91 to %struct.sockaddr_in*
%93 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %92, i32 0, i32 0
%94 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %93, i32 0, i32 0
store i32 %90, i32* %94
%95 = load i8*, i8** @AF_INET
%96 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 1
%97 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %96, i32 0, i32 0
store i8* %95, i8** %97
%98 = load i32, i32* %10
%99 = load i32, i32* @SIOCSIFBRDADDR
%100 = call i64 @ioctl(i32 %98, i32 %99, %struct.ifreq* %11)
%101 = icmp slt i64 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %78
br label %107
103: ; preds = %78
br label %104
104: ; preds = %103, %58, %55
%105 = load i32, i32* %10
%106 = call i32 @close(i32 %105)
store i32 0, i32* %5
br label %113
107: ; preds = %102, %77, %53, %33, %20
%108 = load i32, i32* %10
%109 = call i32 @close(i32 %108)
%110 = load i8*, i8** %6
%111 = call i32 @perror(i8* %110)
%112 = load i32, i32* @errno
store i32 %112, i32* %5
br label %113
113: ; preds = %107, %104
%114 = load i32, i32* %5
ret i32 %114
}

LLVM IR (-O2):
%struct.ifreq = type { i32, %struct.TYPE_8__, %struct.TYPE_6__, %struct.TYPE_5__, i32 }
%struct.TYPE_8__ = type { i8* }
%struct.TYPE_6__ = type { i8* }
%struct.TYPE_5__ = type { i8* }
%struct.in_addr = type { i32 }
@AF_INET = global i8* null
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@IFNAMSIZ = global i32 0
@SIOCSIFFLAGS = global i32 0
@SIOCSIFADDR = global i32 0
@SIOCSIFNETMASK = global i32 0
@SIOCSIFBRDADDR = global i32 0
@errno = global i32 0
define i32 @ifconfig(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca %struct.ifreq
%6 = alloca %struct.in_addr
%7 = alloca %struct.in_addr
%8 = bitcast %struct.ifreq* %5 to i8*
%9 = bitcast %struct.in_addr* %6 to i8*
%10 = bitcast %struct.in_addr* %7 to i8*
%11 = load i8*, i8** @AF_INET
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* @IPPROTO_RAW
%14 = tail call i32 @socket(i8* %11, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %49, label %16
16: ; preds = %4
%17 = load i32, i32* @IFNAMSIZ
%18 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %17)
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
store i32 %1, i32* %19
%20 = load i32, i32* @SIOCSIFFLAGS
%21 = call i64 @ioctl(i32 %14, i32 %20, %struct.ifreq* %5)
%22 = icmp slt i64 %21, 0
br i1 %22, label %49, label %23
23: ; preds = %16
%24 = icmp eq i8* %2, null
br i1 %24, label %47, label %25
25: ; preds = %23
%26 = call i32 @inet_aton(i8* %2, %struct.in_addr* %6)
%27 = load i8*, i8** @AF_INET
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 3, i32 0
store i8* %27, i8** %28
%29 = load i32, i32* @SIOCSIFADDR
%30 = call i64 @ioctl(i32 %14, i32 %29, %struct.ifreq* %5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %49, label %32
32: ; preds = %25
%33 = icmp eq i8* %3, null
br i1 %33, label %47, label %34
34: ; preds = %32
%35 = call i32 @inet_aton(i8* %3, %struct.in_addr* %7)
%36 = load i8*, i8** @AF_INET
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 2, i32 0
store i8* %36, i8** %37
%38 = load i32, i32* @SIOCSIFNETMASK
%39 = call i64 @ioctl(i32 %14, i32 %38, %struct.ifreq* %5)
%40 = icmp slt i64 %39, 0
br i1 %40, label %49, label %41
41: ; preds = %34
%42 = load i8*, i8** @AF_INET
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 1, i32 0
store i8* %42, i8** %43
%44 = load i32, i32* @SIOCSIFBRDADDR
%45 = call i64 @ioctl(i32 %14, i32 %44, %struct.ifreq* %5)
%46 = icmp slt i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %23, %41, %32
%48 = call i32 @close(i32 %14)
br label %53
49: ; preds = %41, %34, %25, %16, %4
%50 = call i32 @close(i32 %14)
%51 = call i32 @perror(i8* %0)
%52 = load i32, i32* @errno
br label %53
53: ; preds = %49, %47
%54 = phi i32 [ %52, %49 ], [ 0, %47 ]
ret i32 %54
}