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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFNETMASK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int getNetMask(const char *interface, char *nm) {
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
  if (ioctl(fd, SIOCGIFNETMASK, &ifr) != 0) {
    return -1;
  }
  close(fd);
  strcpy(nm, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
  return 0;
}


x86 assembly (clang, -O0):
<getNetMask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e22(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     11dd <getNetMask+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <getNetMask+0xa7>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNetMask>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e74(%rip),%r15
mov    (%r15),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11e0 <getNetMask+0x80>
mov    %ebp,%edi
call   1070 <close@plt>
mov    (%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFNETMASK = global i32 0
define i32 @getNetMask(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i64, i64* @IFNAMSIZ
%11 = add nsw i64 %10, -1
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i64 %11)
%13 = load i32, i32* @SIOCGIFNETMASK
%14 = call i64 @ioctl(i32 %7, i32 %13, %struct.ifreq* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = call i32 @close(i32 %7)
%18 = load i32, i32* %9
%19 = call i32 @inet_ntoa(i32 %18)
%20 = call i32 @strcpy(i8* %1, i32 %19)
br label %21
21: ; preds = %2, %16
%22 = phi i32 [ 0, %16 ], [ -1, %2 ]
ret i32 %22
}