#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 */
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFNETMASK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
in_addr_t net_get_netmask(char *ifname) {
  struct ifreq ifr;
  int skfd;
  struct sockaddr_in *saddr;
  if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket error");
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(skfd, SIOCGIFNETMASK, &ifr) < 0) {
    perror("net_get_netmask: ioctl SIOCGIFNETMASK");
    close(skfd);
    return -1;
  }
  close(skfd);
  saddr = (struct sockaddr_in *)&ifr.ifr_addr;
  return saddr->sin_addr.s_addr;
}


x86 assembly (clang, -O0):
<net_get_netmask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1199 <net_get_netmask+0x49>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <net_get_netmask+0xb6>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ed <net_get_netmask+0x9d>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <net_get_netmask+0xb6>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_get_netmask>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11b1 <net_get_netmask+0x61>
mov    %eax,%ebx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11c4 <net_get_netmask+0x74>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a9 <net_get_netmask+0x59>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11a9 <net_get_netmask+0x59>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFNETMASK = global i32 0
@.str.1 = [38 x i8] c"net_get_netmask: ioctl SIOCGIFNETMASK\00"
define i32 @net_get_netmask(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i8*, i8** %3
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i32 %15, i8* %16, i32 %17)
%19 = load i32, i32* %5
%20 = load i32, i32* @SIOCGIFNETMASK
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %36
27: ; preds = %13
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%31 = bitcast i32* %30 to %struct.sockaddr_in*
store %struct.sockaddr_in* %31, %struct.sockaddr_in** %6
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i32, i32* %34
store i32 %35, i32* %2
br label %36
36: ; preds = %27, %23, %11
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFNETMASK = global i32 0
@.str.1 = [38 x i8] c"net_get_netmask: ioctl SIOCGIFNETMASK\00"
define i32 @net_get_netmask(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %1
%11 = load i32, i32* @IFNAMSIZ
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %11)
%13 = load i32, i32* @SIOCGIFNETMASK
%14 = call i64 @ioctl(i32 %6, i32 %13, %struct.ifreq* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %6)
br label %23
19: ; preds = %10
%20 = call i32 @close(i32 %6)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%22 = load i32, i32* %21
br label %23
23: ; preds = %19, %16, %8
%24 = phi i32 [ -1, %8 ], [ -1, %16 ], [ %22, %19 ]
ret i32 %24
}