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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
struct ifreq *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct sockaddr_in *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int set_IFACE_IPv4(char *IP, char *name) {
  int sd;
  struct ifreq *ifreq;
  struct sockaddr_in sa;
  ifreq = malloc(sizeof(struct sockaddr_in));
  sd = socket(AF_INET, SOCK_STREAM, 0);
  strncpy(ifreq->ifr_name, name, IFNAMSIZ);
  memset(&sa, 0, sizeof(struct sockaddr));
  sa.sin_family = AF_INET;
  sa.sin_port = 0;
  sa.sin_addr.s_addr = inet_addr(IP);
  memcpy(&ifreq->ifr_addr, &sa, sizeof(struct sockaddr));
  ioctl(sd, SIOCSIFADDR, ifreq);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<set_IFACE_IPv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x18,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_IFACE_IPv4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x18,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    0x4(%rbx),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r14,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCSIFADDR = global i32 0
define i32 @set_IFACE_IPv4(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq*
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call %struct.ifreq* @malloc(i32 24)
store %struct.ifreq* %8, %struct.ifreq** %6
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load %struct.ifreq*, %struct.ifreq** %6
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load i8*, i8** %4
%16 = load i32, i32* @IFNAMSIZ
%17 = call i32 @strncpy(i32 %14, i8* %15, i32 %16)
%18 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 4)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i64 0, i64* %21
%22 = load i8*, i8** %3
%23 = call i32 @inet_addr(i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load %struct.ifreq*, %struct.ifreq** %6
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %26, i32 0, i32 0
%28 = call i32 @memcpy(i32* %27, %struct.sockaddr_in* %7, i32 4)
%29 = load i32, i32* %5
%30 = load i32, i32* @SIOCSIFADDR
%31 = load %struct.ifreq*, %struct.ifreq** %6
%32 = call i32 @ioctl(i32 %29, i32 %30, %struct.ifreq* %31)
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCSIFADDR = global i32 0
define i32 @set_IFACE_IPv4(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.ifreq* @malloc(i32 24)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 1
%10 = load i32, i32* %9
%11 = load i32, i32* @IFNAMSIZ
%12 = tail call i32 @strncpy(i32 %10, i8* %1, i32 %11)
%13 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 4)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i64 0, i64* %16
%17 = call i32 @inet_addr(i8* %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%20 = call i32 @memcpy(i32* %19, %struct.sockaddr_in* %3, i32 4)
%21 = load i32, i32* @SIOCSIFADDR
%22 = call i32 @ioctl(i32 %8, i32 %21, %struct.ifreq* %5)
%23 = call i32 @close(i32 %8)
ret i32 0
}