#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__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  scalar_t__ sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int ifr_flags;
  int /*<<< orphan*/ ifr_netmask;
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
void *AF_INET;
int IFF_RUNNING;
int IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SIOCSIFNETMASK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
void *htonl(unsigned int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct sockaddr_in *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int set_ip(const char *name, unsigned int ip, unsigned int mask) {
  struct ifreq ifr;
  struct sockaddr_in sin;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  int err;
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  err = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
  if (err < 0)
    perror("ioctl 1");
  ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
  err = ioctl(sockfd, SIOCSIFFLAGS, &ifr);
  if (err < 0)
    perror("ioctl 2");
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  memset(&sin, 0, sizeof(struct sockaddr));
  sin.sin_family = AF_INET;
  sin.sin_port = 0;
  sin.sin_addr.s_addr = htonl(ip);
  memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
  err = ioctl(sockfd, SIOCSIFADDR, &ifr);
  if (err < 0) {
    perror("ioctl 3");
    return -1;
  }
  sin.sin_family = AF_INET;
  sin.sin_port = 0;
  sin.sin_addr.s_addr = htonl(mask);
  memcpy(&ifr.ifr_netmask, &sin, sizeof(struct sockaddr));
  err = ioctl(sockfd, SIOCSIFNETMASK, &ifr);
  if (err < 0) {
    perror("ioctl 4");
    return -1;
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<set_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x44(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    11f2 <set_ip+0x72>
lea    0xe13(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    1238 <set_ip+0xb8>
lea    0xdd5(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htonl@plt>
mov    %rax,-0x40(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x44(%rbp),%edi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    12cf <set_ip+0x14f>
lea    0xd52(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <set_ip+0x1d0>
mov    0x2d0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <htonl@plt>
mov    %rax,-0x40(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x44(%rbp),%edi
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    1341 <set_ip+0x1c1>
lea    0xce8(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <set_ip+0x1d0>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e42(%rip),%r12
mov    (%r12),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    0x2df5(%rip),%r13
mov    0x0(%r13),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     12e4 <set_ip+0x164>
mov    0x2ddf(%rip),%rax
mov    0x2de0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     12f5 <set_ip+0x175>
mov    0xc(%rsp),%edi
mov    0x0(%r13),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    (%r12),%rax
mov    %rax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r15d,%edi
call   1040 <htonl@plt>
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1306 <set_ip+0x186>
mov    (%r12),%rax
mov    %rax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htonl@plt>
mov    %rax,0x10(%rsp)
lea    0x4(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     130f <set_ip+0x18f>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd15(%rip),%rdi
call   1090 <perror@plt>
jmp    11e2 <set_ip+0x62>
lea    0xd0c(%rip),%rdi
call   1090 <perror@plt>
jmp    1212 <set_ip+0x92>
lea    0xd03(%rip),%rdi
jmp    1316 <set_ip+0x196>
lea    0xd02(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    12d5 <set_ip+0x155>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i8* }
%struct.TYPE_2__ = type { i8* }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str = [8 x i8] c"ioctl 1\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [8 x i8] c"ioctl 2\00"
@SIOCSIFADDR = global i32 0
@.str.2 = [8 x i8] c"ioctl 3\00"
@SIOCSIFNETMASK = global i32 0
@.str.3 = [8 x i8] c"ioctl 4\00"
define i32 @set_ip(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ifreq
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i8*, i8** @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i8* %12, i32 %13, i32 0)
store i32 %14, i32* %10
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 3
%16 = load i32, i32* %15
%17 = load i8*, i8** %5
%18 = load i32, i32* @IFNAMSIZ
%19 = call i32 @strncpy(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %10
%21 = load i32, i32* @SIOCGIFFLAGS
%22 = call i32 @ioctl(i32 %20, i32 %21, %struct.ifreq* %8)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %3
%28 = load i32, i32* @IFF_UP
%29 = load i32, i32* @IFF_RUNNING
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%32 = load i32, i32* %31
%33 = or i32 %32, %30
store i32 %33, i32* %31
%34 = load i32, i32* %10
%35 = load i32, i32* @SIOCSIFFLAGS
%36 = call i32 @ioctl(i32 %34, i32 %35, %struct.ifreq* %8)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %27
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %27
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 3
%43 = load i32, i32* %42
%44 = load i8*, i8** %5
%45 = load i32, i32* @IFNAMSIZ
%46 = call i32 @strncpy(i32 %43, i8* %44, i32 %45)
%47 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 4)
%48 = load i8*, i8** @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i8* %48, i8** %49
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i64 0, i64* %50
%51 = load i32, i32* %6
%52 = call i8* @htonl(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
store i8* %52, i8** %54
%55 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 2
%56 = call i32 @memcpy(i32* %55, %struct.sockaddr_in* %9, i32 4)
%57 = load i32, i32* %10
%58 = load i32, i32* @SIOCSIFADDR
%59 = call i32 @ioctl(i32 %57, i32 %58, %struct.ifreq* %8)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %41
%63 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %84
64: ; preds = %41
%65 = load i8*, i8** @AF_INET
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i8* %65, i8** %66
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i64 0, i64* %67
%68 = load i32, i32* %7
%69 = call i8* @htonl(i32 %68)
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%71 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %70, i32 0, i32 0
store i8* %69, i8** %71
%72 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%73 = call i32 @memcpy(i32* %72, %struct.sockaddr_in* %9, i32 4)
%74 = load i32, i32* %10
%75 = load i32, i32* @SIOCSIFNETMASK
%76 = call i32 @ioctl(i32 %74, i32 %75, %struct.ifreq* %8)
store i32 %76, i32* %11
%77 = load i32, i32* %11
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %64
%80 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %84
81: ; preds = %64
%82 = load i32, i32* %10
%83 = call i32 @close(i32 %82)
store i32 0, i32* %4
br label %84
84: ; preds = %81, %79, %62
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i8* }
%struct.TYPE_2__ = type { i8* }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str = [8 x i8] c"ioctl 1\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [8 x i8] c"ioctl 2\00"
@SIOCSIFADDR = global i32 0
@.str.2 = [8 x i8] c"ioctl 3\00"
@SIOCSIFNETMASK = global i32 0
@.str.3 = [8 x i8] c"ioctl 4\00"
define i32 @set_ip(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.ifreq* %4 to i8*
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = load i8*, i8** @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i8* %8, i32 %9, i32 0)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 3
%12 = load i32, i32* @IFNAMSIZ
%13 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %12)
%14 = load i32, i32* @SIOCGIFFLAGS
%15 = call i32 @ioctl(i32 %10, i32 %14, %struct.ifreq* %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %3
%20 = load i32, i32* @IFF_UP
%21 = load i32, i32* @IFF_RUNNING
%22 = or i32 %21, %20
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = or i32 %22, %24
store i32 %25, i32* %23
%26 = load i32, i32* @SIOCSIFFLAGS
%27 = call i32 @ioctl(i32 %10, i32 %26, %struct.ifreq* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %19
%32 = load i32, i32* %11
%33 = load i32, i32* @IFNAMSIZ
%34 = call i32 @strncpy(i32 %32, i8* %0, i32 %33)
%35 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 4)
%36 = load i8*, i8** @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i8* %36, i8** %37
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i64 0, i64* %38
%39 = call i8* @htonl(i32 %1)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i8* %39, i8** %40
%41 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 2
%42 = call i32 @memcpy(i32* %41, %struct.sockaddr_in* %5, i32 4)
%43 = load i32, i32* @SIOCSIFADDR
%44 = call i32 @ioctl(i32 %10, i32 %43, %struct.ifreq* %4)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %31
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %60
48: ; preds = %31
%49 = load i8*, i8** @AF_INET
store i8* %49, i8** %37
store i64 0, i64* %38
%50 = call i8* @htonl(i32 %2)
store i8* %50, i8** %40
%51 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%52 = call i32 @memcpy(i32* %51, %struct.sockaddr_in* %5, i32 4)
%53 = load i32, i32* @SIOCSIFNETMASK
%54 = call i32 @ioctl(i32 %10, i32 %53, %struct.ifreq* %4)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %60
58: ; preds = %48
%59 = call i32 @close(i32 %10)
br label %60
60: ; preds = %58, %56, %46
%61 = phi i32 [ -1, %46 ], [ -1, %56 ], [ 0, %58 ]
ret i32 %61
}