#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_addr;
  int /*<<< orphan*/ sin_family;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SIOCSIFNETMASK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bzero(struct ifreq *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int set_if_addr(char *ifname, char *addr, char *mask) {
  if (!ifname || !addr || !mask) {
    fprintf(stderr, "ifname, addr and mask must not be null!\n");
    return -1;
  }
  int fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  if (fd < 0) {
    fprintf(stderr, "socket error!\n");
    return -1;
  }
  struct ifreq ifr;
  bzero(&ifr, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
  sin->sin_family = AF_INET;
  inet_aton(addr, &sin->sin_addr);
  ioctl(fd, SIOCSIFADDR, &ifr);
  inet_aton(mask, &sin->sin_addr);
  ioctl(fd, SIOCSIFNETMASK, &ifr);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<set_if_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a5 <set_if_addr+0x35>
cmpq   $0x0,-0x18(%rbp)
je     11a5 <set_if_addr+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    11c6 <set_if_addr+0x56>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <set_if_addr+0x132>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1214 <set_if_addr+0xa4>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <set_if_addr+0x132>
lea    -0x30(%rbp),%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <inet_aton@plt>
mov    -0x24(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <inet_aton@plt>
mov    -0x24(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_if_addr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1243 <set_if_addr+0xd3>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1243 <set_if_addr+0xd3>
mov    %rdx,%r14
test   %rdx,%rdx
je     1243 <set_if_addr+0xd3>
mov    %rdi,%rbp
mov    0x2e42(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1255 <set_if_addr+0xe5>
mov    %eax,%r15d
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    0x4(%rsp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <inet_aton@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r12,%rdx
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <inet_aton@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r12,%rdx
call   1040 <ioctl@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    126f <set_if_addr+0xff>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
jmp    1265 <set_if_addr+0xf5>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@stderr = global i32 0
@.str = [41 x i8] c"ifname, addr and mask must not be null!\0A\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str.1 = [15 x i8] c"socket error!\0A\00"
@IFNAMSIZ = global i32 0
@SIOCSIFADDR = global i32 0
@SIOCSIFNETMASK = global i32 0
define i32 @set_if_addr(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.ifreq
%10 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16, %13, %3
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %60
22: ; preds = %16
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_RAW
%25 = load i32, i32* @IPPROTO_RAW
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %60
32: ; preds = %22
%33 = call i32 @bzero(%struct.ifreq* %9, i32 8)
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load i8*, i8** %5
%37 = load i32, i32* @IFNAMSIZ
%38 = call i32 @strncpy(i32 %35, i8* %36, i32 %37)
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%40 = bitcast i32* %39 to %struct.sockaddr_in*
store %struct.sockaddr_in* %40, %struct.sockaddr_in** %10
%41 = load i32, i32* @AF_INET
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 1
store i32 %41, i32* %43
%44 = load i8*, i8** %6
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 0
%47 = call i32 @inet_aton(i8* %44, i32* %46)
%48 = load i32, i32* %8
%49 = load i32, i32* @SIOCSIFADDR
%50 = call i32 @ioctl(i32 %48, i32 %49, %struct.ifreq* %9)
%51 = load i8*, i8** %7
%52 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %52, i32 0, i32 0
%54 = call i32 @inet_aton(i8* %51, i32* %53)
%55 = load i32, i32* %8
%56 = load i32, i32* @SIOCSIFNETMASK
%57 = call i32 @ioctl(i32 %55, i32 %56, %struct.ifreq* %9)
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
store i32 0, i32* %4
br label %60
60: ; preds = %32, %29, %19
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@stderr = global i32 0
@.str = [41 x i8] c"ifname, addr and mask must not be null!\0A\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str.1 = [15 x i8] c"socket error!\0A\00"
@IFNAMSIZ = global i32 0
@SIOCSIFADDR = global i32 0
@SIOCSIFNETMASK = global i32 0
define i32 @set_if_addr(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
%8 = icmp ne i8* %2, null
%9 = and i1 %7, %8
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %38
13: ; preds = %3
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_RAW
%16 = load i32, i32* @IPPROTO_RAW
%17 = tail call i32 @socket(i32 %14, i32 %15, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %38
22: ; preds = %13
%23 = bitcast %struct.ifreq* %4 to i8*
%24 = call i32 @bzero(%struct.ifreq* %4, i32 8)
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%26 = load i32, i32* %25
%27 = load i32, i32* @IFNAMSIZ
%28 = tail call i32 @strncpy(i32 %26, i8* %0, i32 %27)
%29 = load i32, i32* @AF_INET
store i32 %29, i32* %25
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%31 = call i32 @inet_aton(i8* %1, i32* %30)
%32 = load i32, i32* @SIOCSIFADDR
%33 = call i32 @ioctl(i32 %17, i32 %32, %struct.ifreq* %4)
%34 = call i32 @inet_aton(i8* %2, i32* %30)
%35 = load i32, i32* @SIOCSIFNETMASK
%36 = call i32 @ioctl(i32 %17, i32 %35, %struct.ifreq* %4)
%37 = call i32 @close(i32 %17)
br label %38
38: ; preds = %19, %22, %10
%39 = phi i32 [ -1, %10 ], [ -1, %19 ], [ 0, %22 ]
ret i32 %39
}