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

/* Variables and functions */
int /*<<< orphan*/ IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int network_iface_up(const char *iface, int domain, int type) {
  struct ifreq ifr;
  int fd = -1;
  int rc;
  if (iface == NULL)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, iface, IFNAMSIZ);
  fd = socket(domain, type, 0);
  if (fd < 0)
    goto error;
  rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
  if (rc < 0)
    goto error;
  ifr.ifr_flags |= IFF_UP;
  rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
  if (rc < 0)
    goto error;
  rc = 0;
  goto complete;
error:
  rc = -1;
complete:
  if (fd >= 0)
    close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<network_iface_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1180 <network_iface_up+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <network_iface_up+0xfc>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c4 <network_iface_up+0x74>
jmp    122d <network_iface_up+0xdd>
mov    -0x24(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11eb <network_iface_up+0x9b>
jmp    122d <network_iface_up+0xdd>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1221 <network_iface_up+0xd1>
jmp    122d <network_iface_up+0xdd>
movl   $0x0,-0x28(%rbp)
jmp    1234 <network_iface_up+0xe4>
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1246 <network_iface_up+0xf6>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<network_iface_up>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     11ee <network_iface_up+0x9e>
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    %ebp,%edi
mov    %r14d,%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11ee <network_iface_up+0x9e>
mov    %eax,%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     11e1 <network_iface_up+0x91>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
or     %eax,(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
xor    %r15d,%r15d
test   %eax,%eax
jns    11e7 <network_iface_up+0x97>
mov    $0xffffffff,%r15d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @network_iface_up(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ifreq
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 -1, i32* %9
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %55
14: ; preds = %3
%15 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %5
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %14
br label %46
27: ; preds = %14
%28 = load i32, i32* %9
%29 = load i32, i32* @SIOCGIFFLAGS
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.ifreq* %8)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %46
34: ; preds = %27
%35 = load i32, i32* @IFF_UP
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%37 = load i32, i32* %36
%38 = or i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* %9
%40 = load i32, i32* @SIOCSIFFLAGS
%41 = call i32 @ioctl(i32 %39, i32 %40, %struct.ifreq* %8)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %34
br label %46
45: ; preds = %34
store i32 0, i32* %10
br label %47
46: ; preds = %44, %33, %26
store i32 -1, i32* %10
br label %47
47: ; preds = %46, %45
%48 = load i32, i32* %9
%49 = icmp sge i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %9
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %10
store i32 %54, i32* %4
br label %55
55: ; preds = %53, %13
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @network_iface_up(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = icmp eq i8* %0, null
br i1 %6, label %31, label %7
7: ; preds = %3
%8 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%10 = load i32, i32* %9
%11 = load i32, i32* @IFNAMSIZ
%12 = call i32 @strncpy(i32 %10, i8* %0, i32 %11)
%13 = call i32 @socket(i32 %1, i32 %2, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %7
%16 = load i32, i32* @SIOCGIFFLAGS
%17 = call i32 @ioctl(i32 %13, i32 %16, %struct.ifreq* %4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = load i32, i32* @IFF_UP
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = or i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @SIOCSIFFLAGS
%25 = call i32 @ioctl(i32 %13, i32 %24, %struct.ifreq* %4)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %15, %19
br label %28
28: ; preds = %19, %27
%29 = phi i32 [ -1, %27 ], [ 0, %19 ]
%30 = call i32 @close(i32 %13)
br label %31
31: ; preds = %7, %28, %3
%32 = phi i32 [ -1, %3 ], [ %29, %28 ], [ -1, %7 ]
ret i32 %32
}