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

/* Variables and functions */
int 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_down(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 = (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_down>:
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_down+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <network_iface_down+0x101>
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_down+0x74>
jmp    1232 <network_iface_down+0xe2>
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_down+0x9b>
jmp    1232 <network_iface_down+0xe2>
mov    -0x20(%rbp),%eax
mov    0x2deb(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1226 <network_iface_down+0xd6>
jmp    1232 <network_iface_down+0xe2>
movl   $0x0,-0x28(%rbp)
jmp    1239 <network_iface_down+0xe9>
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     124b <network_iface_down+0xfb>
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_down>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     11f0 <network_iface_down+0xa0>
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     11f0 <network_iface_down+0xa0>
mov    %eax,%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     11e3 <network_iface_down+0x93>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
xor    %r15d,%r15d
test   %eax,%eax
jns    11e9 <network_iface_down+0x99>
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_down(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 %57
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 %48
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 %48
34: ; preds = %27
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @IFF_UP
%38 = xor i32 %37, -1
%39 = and i32 %36, %38
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %9
%42 = load i32, i32* @SIOCSIFFLAGS
%43 = call i32 @ioctl(i32 %41, i32 %42, %struct.ifreq* %8)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %34
br label %48
47: ; preds = %34
store i32 0, i32* %10
br label %49
48: ; preds = %46, %33, %26
store i32 -1, i32* %10
br label %49
49: ; preds = %48, %47
%50 = load i32, i32* %9
%51 = icmp sge i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32, i32* %9
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %52, %49
%56 = load i32, i32* %10
store i32 %56, i32* %4
br label %57
57: ; preds = %55, %13
%58 = load i32, i32* %4
ret i32 %58
}

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_down(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 %32, 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 %32, 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 %28, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @IFF_UP
%23 = xor i32 %22, -1
%24 = and i32 %21, %23
store i32 %24, i32* %20
%25 = load i32, i32* @SIOCSIFFLAGS
%26 = call i32 @ioctl(i32 %13, i32 %25, %struct.ifreq* %4)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %15, %19
br label %29
29: ; preds = %19, %28
%30 = phi i32 [ -1, %28 ], [ 0, %19 ]
%31 = call i32 @close(i32 %13)
br label %32
32: ; preds = %7, %29, %3
%33 = phi i32 [ -1, %3 ], [ %30, %29 ], [ -1, %7 ]
ret i32 %33
}