#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;
  scalar_t__ *ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int get_ioctl_sock();
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(scalar_t__ *, char *, int);
int set_unset_dev_flags(char *ifname, int flags, int set) {
  struct ifreq ifr;
  int s;
  s = get_ioctl_sock();
  if (s < 0)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  ifr.ifr_name[IFNAMSIZ - 1] = 0;
  if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
    return -1;
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  ifr.ifr_name[IFNAMSIZ - 1] = 0;
  if (set)
    ifr.ifr_flags |= flags;
  else
    ifr.ifr_flags &= ~flags;
  if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<set_unset_dev_flags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   1060 <get_ioctl_sock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1172 <set_unset_dev_flags+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <set_unset_dev_flags+0x12b>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11e1 <set_unset_dev_flags+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <set_unset_dev_flags+0x12b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
cmpl   $0x0,-0x18(%rbp)
je     122d <set_unset_dev_flags+0xed>
mov    -0x14(%rbp),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1239 <set_unset_dev_flags+0xf9>
mov    -0x14(%rbp),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1264 <set_unset_dev_flags+0x124>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <set_unset_dev_flags+0x12b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_unset_dev_flags>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r12
xor    %eax,%eax
call   1060 <get_ioctl_sock@plt>
mov    $0xffffffff,%r13d
test   %eax,%eax
js     1222 <set_unset_dev_flags+0xe2>
mov    %eax,%r15d
mov    %ebx,0x4(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x10(%rsp),%rdi
mov    0x2e3f(%rip),%r14
mov    (%r14),%edx
add    $0xffffffff,%edx
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    0x10(%rsp),%rax
movslq (%r14),%rcx
movq   $0x0,-0x8(%rax,%rcx,8)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1222 <set_unset_dev_flags+0xe2>
mov    0x10(%rsp),%rdi
mov    (%r14),%edx
add    $0xffffffff,%edx
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    0x10(%rsp),%rax
movslq (%r14),%rcx
movq   $0x0,-0x8(%rax,%rcx,8)
test   %ebp,%ebp
je     11fb <set_unset_dev_flags+0xbb>
mov    0x4(%rsp),%eax
or     %eax,0x8(%rsp)
jmp    1205 <set_unset_dev_flags+0xc5>
mov    0x4(%rsp),%eax
not    %eax
and    %eax,0x8(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1050 <ioctl@plt>
mov    %rax,%r13
sar    $0x3f,%r13
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i64* }
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @set_unset_dev_flags(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = tail call i32 (...) @get_ioctl_sock()
%7 = icmp slt i32 %6, 0
br i1 %7, label %48, label %8
8: ; preds = %3
%9 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 16)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%11 = load i64*, i64** %10
%12 = load i32, i32* @IFNAMSIZ
%13 = add nsw i32 %12, -1
%14 = call i32 @strncpy(i64* %11, i8* %0, i32 %13)
%15 = load i64*, i64** %10
%16 = load i32, i32* @IFNAMSIZ
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i64, i64* %15, i64 %18
store i64 0, i64* %19
%20 = load i32, i32* @SIOCGIFFLAGS
%21 = call i64 @ioctl(i32 %6, i32 %20, %struct.ifreq* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %48, label %23
23: ; preds = %8
%24 = load i64*, i64** %10
%25 = load i32, i32* @IFNAMSIZ
%26 = add nsw i32 %25, -1
%27 = call i32 @strncpy(i64* %24, i8* %0, i32 %26)
%28 = load i64*, i64** %10
%29 = load i32, i32* @IFNAMSIZ
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i64, i64* %28, i64 %31
store i64 0, i64* %32
%33 = icmp eq i32 %2, 0
br i1 %33, label %38, label %34
34: ; preds = %23
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%36 = load i32, i32* %35
%37 = or i32 %36, %1
store i32 %37, i32* %35
br label %43
38: ; preds = %23
%39 = xor i32 %1, -1
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, %39
store i32 %42, i32* %40
br label %43
43: ; preds = %38, %34
%44 = load i32, i32* @SIOCSIFFLAGS
%45 = call i64 @ioctl(i32 %6, i32 %44, %struct.ifreq* %4)
%46 = ashr i64 %45, 63
%47 = trunc i64 %46 to i32
br label %48
48: ; preds = %43, %8, %3
%49 = phi i32 [ -1, %3 ], [ -1, %8 ], [ %47, %43 ]
ret i32 %49
}