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


x86 assembly (clang, -O0):
<net_get_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1199 <net_get_flag+0x49>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <net_get_flag+0xab>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ed <net_get_flag+0x9d>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <net_get_flag+0xab>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_get_flag>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11b1 <net_get_flag+0x61>
mov    %eax,%ebx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11c4 <net_get_flag+0x74>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a9 <net_get_flag+0x59>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11a9 <net_get_flag+0x59>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [33 x i8] c"net_get_flag: ioctl SIOCGIFFLAGS\00"
define i32 @net_get_flag(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 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %31
12: ; preds = %1
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load i8*, i8** %3
%16 = load i32, i32* @IFNAMSIZ
%17 = call i32 @strncpy(i32 %14, i8* %15, i32 %16)
%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 = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 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 %31
26: ; preds = %12
%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
store i32 %30, i32* %2
br label %31
31: ; preds = %26, %22, %10
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [33 x i8] c"net_get_flag: ioctl SIOCGIFFLAGS\00"
define i32 @net_get_flag(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 slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %1
%11 = load i32, i32* @IFNAMSIZ
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %11)
%13 = load i32, i32* @SIOCGIFFLAGS
%14 = call i64 @ioctl(i32 %6, i32 %13, %struct.ifreq* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %6)
br label %23
19: ; preds = %10
%20 = call i32 @close(i32 %6)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%22 = load i32, i32* %21
br label %23
23: ; preds = %19, %16, %8
%24 = phi i32 [ -1, %8 ], [ -1, %16 ], [ %22, %19 ]
ret i32 %24
}