#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 /*<<< orphan*/ AF_INET;
int IFF_UP;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int getIfIsUp(char *ifname) {
  struct ifreq ifr;
  int skfd;
  skfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (skfd == -1) {
    perror("socket");
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
    perror("ioctl");
    close(skfd);
    return -1;
  }
  close(skfd);
  if (ifr.ifr_flags & IFF_UP)
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<getIfIsUp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119a <getIfIsUp+0x4a>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getIfIsUp+0xca>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ea <getIfIsUp+0x9a>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getIfIsUp+0xca>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    0x2de4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1213 <getIfIsUp+0xc3>
movl   $0x1,-0x4(%rbp)
jmp    121a <getIfIsUp+0xca>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getIfIsUp>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11bc <getIfIsUp+0x6c>
mov    %eax,%ebx
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11cf <getIfIsUp+0x7f>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
test   %ecx,(%rsp)
setne  %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11b4 <getIfIsUp+0x64>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11b4 <getIfIsUp+0x64>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@IFF_UP = global i32 0
define i32 @getIfIsUp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i8*, i8** %3
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 4)
%18 = load i32, i32* %5
%19 = load i32, i32* @SIOCGIFFLAGS
%20 = call i64 @ioctl(i32 %18, i32 %19, %struct.ifreq* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %36
26: ; preds = %13
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @IFF_UP
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 1, i32* %2
br label %36
35: ; preds = %26
store i32 0, i32* %2
br label %36
36: ; preds = %35, %34, %22, %11
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@IFF_UP = global i32 0
define i32 @getIfIsUp(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %1
%11 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%12 = load i32, i32* @SIOCGIFFLAGS
%13 = call i64 @ioctl(i32 %6, i32 %12, %struct.ifreq* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @close(i32 %6)
br label %26
18: ; preds = %10
%19 = call i32 @close(i32 %6)
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @IFF_UP
%23 = and i32 %22, %21
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %18, %15, %8
%27 = phi i32 [ -1, %8 ], [ -1, %15 ], [ %25, %18 ]
ret i32 %27
}