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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
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_clr_flag(char *ifname, short flag) {
  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_clr_flag: ioctl SIOCGIFFLAGS");
    close(skfd);
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ifr.ifr_flags &= ~flag;
  if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
    perror("net_clr_flag: ioctl SIOCSIFFLAGS");
    close(skfd);
    return -1;
  }
  close(skfd);
  return (0);
}


x86 assembly (clang, -O0):
<net_clr_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a0 <net_clr_flag+0x50>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <net_clr_flag+0x118>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11f4 <net_clr_flag+0xa4>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <net_clr_flag+0x118>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
movswl -0x12(%rbp),%ecx
xor    $0xffffffff,%ecx
movswl -0x20(%rbp),%eax
and    %ecx,%eax
mov    %ax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1259 <net_clr_flag+0x109>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <net_clr_flag+0x118>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_clr_flag>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e6 <net_clr_flag+0x96>
mov    %eax,%ebx
mov    0x2e3e(%rip),%r15
mov    (%r15),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11f9 <net_clr_flag+0xa9>
mov    0x4(%rsp),%edi
mov    (%r15),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
not    %ebp
and    %bp,(%rsp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1202 <net_clr_flag+0xb2>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11db <net_clr_flag+0x8b>
lea    0xe0d(%rip),%rdi
jmp    1209 <net_clr_flag+0xb9>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11db <net_clr_flag+0x8b>

LLVM IR (-O0):
%struct.ifreq = type { i16, 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_clr_flag: ioctl SIOCGIFFLAGS\00"
@SIOCSIFFLAGS = global i32 0
@.str.2 = [33 x i8] c"net_clr_flag: ioctl SIOCSIFFLAGS\00"
define i32 @net_clr_flag(i8* %0, i16 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
14: ; preds = %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %4
%18 = load i32, i32* @IFNAMSIZ
%19 = call i32 @strncpy(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %7
%21 = load i32, i32* @SIOCGIFFLAGS
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %53
28: ; preds = %14
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i8*, i8** %4
%32 = load i32, i32* @IFNAMSIZ
%33 = call i32 @strncpy(i32 %30, i8* %31, i32 %32)
%34 = load i16, i16* %5
%35 = sext i16 %34 to i32
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%38 = load i16, i16* %37
%39 = sext i16 %38 to i32
%40 = and i32 %39, %36
%41 = trunc i32 %40 to i16
store i16 %41, i16* %37
%42 = load i32, i32* %7
%43 = load i32, i32* @SIOCSIFFLAGS
%44 = call i64 @ioctl(i32 %42, i32 %43, %struct.ifreq* %6)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %28
%47 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %3
br label %53
50: ; preds = %28
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
store i32 0, i32* %3
br label %53
53: ; preds = %50, %46, %24, %12
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.ifreq = type { i16, 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_clr_flag: ioctl SIOCGIFFLAGS\00"
@SIOCSIFFLAGS = global i32 0
@.str.2 = [33 x i8] c"net_clr_flag: ioctl SIOCSIFFLAGS\00"
define i32 @net_clr_flag(i8* %0, i16 signext %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %37
11: ; preds = %2
%12 = load i32, i32* @IFNAMSIZ
%13 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %12)
%14 = load i32, i32* @SIOCGIFFLAGS
%15 = call i64 @ioctl(i32 %7, i32 %14, %struct.ifreq* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @close(i32 %7)
br label %37
20: ; preds = %11
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @IFNAMSIZ
%24 = call i32 @strncpy(i32 %22, i8* %0, i32 %23)
%25 = xor i16 %1, -1
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%27 = load i16, i16* %26
%28 = and i16 %27, %25
store i16 %28, i16* %26
%29 = load i32, i32* @SIOCSIFFLAGS
%30 = call i64 @ioctl(i32 %7, i32 %29, %struct.ifreq* %3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @close(i32 %7)
br label %37
35: ; preds = %20
%36 = call i32 @close(i32 %7)
br label %37
37: ; preds = %35, %32, %17, %9
%38 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %32 ], [ 0, %35 ]
ret i32 %38
}