#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__ {
  scalar_t__ sa_family;
};
struct ifreq {
  int ifr_flags;
  TYPE_1__ ifr_addr;
};
struct ifconf {
  int ifc_len;
  char *ifc_buf;
  struct ifreq *ifc_req;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ AF_INET;
int IFF_UP;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ SIOCGIFFLAGS;
int UDPMSGSIZE;
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int is_network_up(int sock) {
  struct ifconf ifc;
  char buf[UDPMSGSIZE];
  struct ifreq ifreq, *ifr;
  int n;
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) == 0) {
    ifr = ifc.ifc_req;
    for (n = ifc.ifc_len / sizeof(struct ifreq); n > 0; n--, ifr++) {
      ifreq = *ifr;
      if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0)
        break;
      if ((ifreq.ifr_flags & IFF_UP) && ifr->ifr_addr.sa_family == AF_INET)
        return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_network_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    1223 <is_network_up+0x113>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
movslq -0x20(%rbp),%rax
shr    $0x4,%rax
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jle    121e <is_network_up+0x10e>
mov    -0x48(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x40(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11bf <is_network_up+0xaf>
jmp    121e <is_network_up+0x10e>
mov    -0x40(%rbp),%eax
mov    0x2e17(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ff <is_network_up+0xef>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    11ff <is_network_up+0xef>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1231 <is_network_up+0x121>
jmp    1204 <is_network_up+0xf4>
mov    -0x4c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x48(%rbp)
jmp    117e <is_network_up+0x6e>
jmp    1223 <is_network_up+0x113>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_network_up>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r13d
mov    %rsp,%r15
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %eax,-0x48(%rbp)
mov    %rcx,-0x40(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <ioctl@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    11e7 <is_network_up+0xd7>
mov    -0x48(%rbp),%ebx
cmp    $0x10,%ebx
jl     11e7 <is_network_up+0xd7>
mov    %r15,-0x30(%rbp)
shr    $0x4,%ebx
mov    -0x38(%rbp),%r12
add    $0x1,%ebx
add    $0x8,%r12
lea    -0x60(%rbp),%r15
jmp    119c <is_network_up+0x8c>
nopl   0x0(%rax)
add    $0xffffffff,%ebx
add    $0x10,%r12
cmp    $0x1,%ebx
jle    11e3 <is_network_up+0xd3>
movups -0x8(%r12),%xmm0
movaps %xmm0,-0x60(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    %r15,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11e3 <is_network_up+0xd3>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x60(%rbp)
je     1190 <is_network_up+0x80>
mov    (%r12),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <is_network_up+0x80>
mov    $0x1,%r14d
mov    -0x30(%rbp),%r15
mov    %r15,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifconf = type { i32, i8*, %struct.ifreq* }
%struct.ifreq = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@UDPMSGSIZE = global i32 0
@SIOCGIFCONF = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@AF_INET = global i64 0
define i32 @is_network_up(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.ifconf
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.ifreq
%8 = alloca %struct.ifreq*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
%11 = load i32, i32* @UDPMSGSIZE
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = trunc i64 %12 to i32
%16 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %4, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %4, i32 0, i32 1
store i8* %14, i8** %17
%18 = load i32, i32* %3
%19 = load i32, i32* @SIOCGIFCONF
%20 = bitcast %struct.ifconf* %4 to i8*
%21 = call i64 @ioctl(i32 %18, i32 %19, i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %65
23: ; preds = %1
%24 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %4, i32 0, i32 2
%25 = load %struct.ifreq*, %struct.ifreq** %24
store %struct.ifreq* %25, %struct.ifreq** %8
%26 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = udiv i64 %28, 16
%30 = trunc i64 %29 to i32
store i32 %30, i32* %9
br label %31
31: ; preds = %59, %23
%32 = load i32, i32* %9
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %64
34: ; preds = %31
%35 = load %struct.ifreq*, %struct.ifreq** %8
%36 = bitcast %struct.ifreq* %7 to i8*
%37 = bitcast %struct.ifreq* %35 to i8*
%38 = load i32, i32* %3
%39 = load i32, i32* @SIOCGIFFLAGS
%40 = bitcast %struct.ifreq* %7 to i8*
%41 = call i64 @ioctl(i32 %38, i32 %39, i8* %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %34
br label %64
44: ; preds = %34
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* @IFF_UP
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %44
%51 = load %struct.ifreq*, %struct.ifreq** %8
%52 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %51, i32 0, i32 1
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 0
%54 = load i64, i64* %53
%55 = load i64, i64* @AF_INET
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %50
store i32 1, i32* %2
store i32 1, i32* %10
br label %66
58: ; preds = %50, %44
br label %59
59: ; preds = %58
%60 = load i32, i32* %9
%61 = add nsw i32 %60, -1
store i32 %61, i32* %9
%62 = load %struct.ifreq*, %struct.ifreq** %8
%63 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %62, i32 1
store %struct.ifreq* %63, %struct.ifreq** %8
br label %31
64: ; preds = %43, %31
br label %65
65: ; preds = %64, %1
store i32 0, i32* %2
store i32 1, i32* %10
br label %66
66: ; preds = %65, %57
%67 = load i8*, i8** %5
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i8*, %struct.ifreq* }
%struct.ifreq = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@UDPMSGSIZE = global i32 0
@SIOCGIFCONF = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@AF_INET = global i64 0
define i32 @is_network_up(i32 %0) {
%2 = alloca %struct.ifconf
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifconf* %2 to i8*
%5 = load i32, i32* @UDPMSGSIZE
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = bitcast %struct.ifreq* %3 to i8*
%10 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %2, i64 0, i32 0
store i32 %5, i32* %10
%11 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %2, i64 0, i32 1
store i8* %8, i8** %11
%12 = load i32, i32* @SIOCGIFCONF
%13 = call i64 @ioctl(i32 %0, i32 %12, i8* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %1
%16 = load i32, i32* %10
%17 = icmp sgt i32 %16, 15
br i1 %17, label %18, label %44
18: ; preds = %15
%19 = lshr i32 %16, 4
%20 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %2, i64 0, i32 2
%21 = load %struct.ifreq*, %struct.ifreq** %20
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
br label %23
23: ; preds = %18, %40
%24 = phi i32 [ %19, %18 ], [ %41, %40 ]
%25 = phi %struct.ifreq* [ %21, %18 ], [ %42, %40 ]
%26 = bitcast %struct.ifreq* %25 to i8*
%27 = load i32, i32* @SIOCGIFFLAGS
%28 = call i64 @ioctl(i32 %0, i32 %27, i8* %9)
%29 = icmp slt i64 %28, 0
br i1 %29, label %44, label %30
30: ; preds = %23
%31 = load i32, i32* %22
%32 = load i32, i32* @IFF_UP
%33 = and i32 %32, %31
%34 = icmp eq i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %25, i64 0, i32 1, i32 0
%37 = load i64, i64* %36
%38 = load i64, i64* @AF_INET
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %30, %35
%41 = add nsw i32 %24, -1
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %25, i64 1
%43 = icmp sgt i32 %24, 1
br i1 %43, label %23, label %44
44: ; preds = %35, %40, %23, %15, %1
%45 = phi i32 [ 0, %1 ], [ 0, %15 ], [ 1, %35 ], [ 0, %40 ], [ 0, %23 ]
ret i32 %45
}