#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;
};

/* Variables and functions */
int IFF_UP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int iface_up(char *iface) {
  int fd;
  int ir;
  struct ifreq ifr;
  fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  strcpy(ifr.ifr_name, iface);
  ir = ioctl(fd, SIOCGIFFLAGS, &ifr);
  close(fd);
  if ((ir != 0) || (!(ifr.ifr_flags & IFF_UP)))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<iface_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11ba <iface_up+0x7a>
mov    -0x20(%rbp),%eax
mov    0x2e39(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11c6 <iface_up+0x86>
movl   $0x0,-0x4(%rbp)
jmp    11cd <iface_up+0x8d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iface_up>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
jne    11a3 <iface_up+0x63>
mov    0x2e4f(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
test   %ecx,(%rsp)
setne  %al
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
define i32 @iface_up(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %3
%14 = call i32 @strcpy(i32 %12, i8* %13)
%15 = load i32, i32* %4
%16 = load i32, i32* @SIOCGIFFLAGS
%17 = call i32 @ioctl(i32 %15, i32 %16, %struct.ifreq* %6)
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %1
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @IFF_UP
%26 = and i32 %24, %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %22, %1
store i32 0, i32* %2
br label %30
29: ; preds = %22
store i32 1, i32* %2
br label %30
30: ; preds = %29, %28
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
define i32 @iface_up(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_UDP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = tail call i32 @strcpy(i32 undef, i8* %0)
%9 = load i32, i32* @SIOCGIFFLAGS
%10 = call i32 @ioctl(i32 %7, i32 %9, %struct.ifreq* %2)
%11 = call i32 @close(i32 %7)
%12 = icmp eq i32 %10, 0
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @IFF_UP
%17 = and i32 %16, %15
%18 = icmp ne i32 %17, 0
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %13, %1
%21 = phi i32 [ 0, %1 ], [ %19, %13 ]
ret i32 %21
}