#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct ifreq {
  int /*<<< orphan*/ ifr_flags;
  int /*<<< orphan*/ ifr_name;
  int /*<<< orphan*/ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFF_UP;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dotted_to_addr(char const *);
int /*<<< orphan*/ err(int, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
struct in_addr setup_network(const char *devname, const char *addrstr) {
  struct ifreq ifr;
  int fd;
  struct in_addr addr;
  struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
  strcpy(ifr.ifr_name, devname);
  addr.s_addr = dotted_to_addr(addrstr);
  sin->sin_family = AF_INET;
  sin->sin_addr = addr;
  fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (fd < 0)
    err(1, "opening IP socket");
  if (ioctl(fd, SIOCSIFADDR, &ifr) != 0)
    err(1, "Setting interface address for %s", devname);
  ifr.ifr_flags = IFF_UP;
  if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0)
    err(1, "Bringing interface %s up", devname);
  close(fd);
  return addr;
}


x86 assembly (clang, -O0):
<setup_network>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dotted_to_addr@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,(%rax)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ed <setup_network+0x8d>
mov    $0x1,%edi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x2c(%rbp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1223 <setup_network+0xc3>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1265 <setup_network+0x105>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_network>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1070 <dotted_to_addr@plt>
mov    %eax,%r14d
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,0x8(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11cc <setup_network+0x6c>
lea    0xe40(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <err@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     11fa <setup_network+0x9a>
lea    0xe27(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     1234 <setup_network+0xd4>
lea    0xe0e(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.ifreq = type { i32, i32, i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [18 x i8] c"opening IP socket\00"
@SIOCSIFADDR = global i32 0
@.str.1 = [33 x i8] c"Setting interface address for %s\00"
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [25 x i8] c"Bringing interface %s up\00"
define i32 @setup_network(i8* %0, i8* %1) {
%3 = alloca %struct.in_addr
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca %struct.sockaddr_in*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%10 = bitcast i32* %9 to %struct.sockaddr_in*
store %struct.sockaddr_in* %10, %struct.sockaddr_in** %8
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %4
%14 = call i32 @strcpy(i32 %12, i8* %13)
%15 = load i8*, i8** %5
%16 = call i32 @dotted_to_addr(i8* %15)
%17 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
store i32 %18, i32* %20
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 0
%23 = bitcast %struct.in_addr* %22 to i8*
%24 = bitcast %struct.in_addr* %3 to i8*
%25 = load i32, i32* @PF_INET
%26 = load i32, i32* @SOCK_DGRAM
%27 = load i32, i32* @IPPROTO_IP
%28 = call i32 @socket(i32 %25, i32 %26, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %2
%32 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %2
%34 = load i32, i32* %7
%35 = load i32, i32* @SIOCSIFADDR
%36 = call i64 @ioctl(i32 %34, i32 %35, %struct.ifreq* %6)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i8*, i8** %4
%40 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %33
%42 = load i32, i32* @IFF_UP
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %7
%45 = load i32, i32* @SIOCSIFFLAGS
%46 = call i64 @ioctl(i32 %44, i32 %45, %struct.ifreq* %6)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %41
%49 = load i8*, i8** %4
%50 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %48, %41
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
%55 = load i32, i32* %54
ret i32 %55
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [18 x i8] c"opening IP socket\00"
@SIOCSIFADDR = global i32 0
@.str.1 = [33 x i8] c"Setting interface address for %s\00"
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [25 x i8] c"Bringing interface %s up\00"
define i32 @setup_network(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 2
%6 = tail call i32 @strcpy(i32 undef, i8* %0)
%7 = tail call i32 @dotted_to_addr(i8* %1)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds i32, i32* %5, i64 1
store i32 %8, i32* %9
store i32 %7, i32* %5
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_IP
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* @SIOCSIFADDR
%19 = call i64 @ioctl(i32 %13, i32 %18, %struct.ifreq* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* @IFF_UP
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @SIOCSIFFLAGS
%27 = call i64 @ioctl(i32 %13, i32 %26, %struct.ifreq* %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %29, %23
%32 = call i32 @close(i32 %13)
ret i32 %7
}