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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bzero(struct ifreq *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int set_if_flag(char *ifname, short flag, int cmd) {
  if (!ifname) {
    fprintf(stderr, "ifname must not be null!");
    return -1;
  }
  int fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  if (fd < 0) {
    fprintf(stderr, "socket error!");
    return -1;
  }
  struct ifreq ifr;
  bzero(&ifr, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ioctl(fd, SIOCGIFFLAGS, &ifr);
  if (cmd)
    ifr.ifr_flags |= flag;
  else
    ifr.ifr_flags &= ~flag;
  if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {
    fprintf(stderr, "ioctl error!");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_if_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1192 <set_if_flag+0x42>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <set_if_flag+0x13c>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e0 <set_if_flag+0x90>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <set_if_flag+0x13c>
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmpl   $0x0,-0x18(%rbp)
je     1235 <set_if_flag+0xe5>
movswl -0x12(%rbp),%ecx
movswl -0x28(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x28(%rbp)
jmp    1246 <set_if_flag+0xf6>
movswl -0x12(%rbp),%ecx
xor    $0xffffffff,%ecx
movswl -0x28(%rbp),%eax
and    %ecx,%eax
mov    %ax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1285 <set_if_flag+0x135>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <set_if_flag+0x13c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_if_flag>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11d7 <set_if_flag+0x87>
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e9 <set_if_flag+0x99>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0xc(%rsp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1040 <ioctl@plt>
test   %r14d,%r14d
je     11fb <set_if_flag+0xab>
or     %r15w,0x8(%rsp)
jmp    1204 <set_if_flag+0xb4>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
jmp    1232 <set_if_flag+0xe2>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
jmp    1232 <set_if_flag+0xe2>
not    %r15d
and    %r15w,0x8(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    123c <set_if_flag+0xec>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i16, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"ifname must not be null!\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str.1 = [14 x i8] c"socket error!\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [13 x i8] c"ioctl error!\00"
define i32 @set_if_flag(i8* %0, i16 signext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.ifreq
store i8* %0, i8** %5
store i16 %1, i16* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %63
15: ; preds = %3
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_RAW
%18 = load i32, i32* @IPPROTO_RAW
%19 = call i32 @socket(i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %63
25: ; preds = %15
%26 = call i32 @bzero(%struct.ifreq* %9, i32 8)
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %5
%30 = load i32, i32* @IFNAMSIZ
%31 = call i32 @strncpy(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %8
%33 = load i32, i32* @SIOCGIFFLAGS
%34 = call i32 @ioctl(i32 %32, i32 %33, %struct.ifreq* %9)
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %25
%38 = load i16, i16* %6
%39 = sext i16 %38 to i32
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%41 = load i16, i16* %40
%42 = sext i16 %41 to i32
%43 = or i32 %42, %39
%44 = trunc i32 %43 to i16
store i16 %44, i16* %40
br label %54
45: ; preds = %25
%46 = load i16, i16* %6
%47 = sext i16 %46 to i32
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%50 = load i16, i16* %49
%51 = sext i16 %50 to i32
%52 = and i32 %51, %48
%53 = trunc i32 %52 to i16
store i16 %53, i16* %49
br label %54
54: ; preds = %45, %37
%55 = load i32, i32* %8
%56 = load i32, i32* @SIOCSIFFLAGS
%57 = call i32 @ioctl(i32 %55, i32 %56, %struct.ifreq* %9)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %63
62: ; preds = %54
store i32 0, i32* %4
br label %63
63: ; preds = %62, %59, %22, %12
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
%struct.ifreq = type { i16, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"ifname must not be null!\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str.1 = [14 x i8] c"socket error!\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [13 x i8] c"ioctl error!\00"
define i32 @set_if_flag(i8* %0, i16 signext %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %46
9: ; preds = %3
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @IPPROTO_RAW
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %46
18: ; preds = %9
%19 = bitcast %struct.ifreq* %4 to i8*
%20 = call i32 @bzero(%struct.ifreq* %4, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @IFNAMSIZ
%24 = tail call i32 @strncpy(i32 %22, i8* %0, i32 %23)
%25 = load i32, i32* @SIOCGIFFLAGS
%26 = call i32 @ioctl(i32 %13, i32 %25, %struct.ifreq* %4)
%27 = icmp eq i32 %2, 0
br i1 %27, label %32, label %28
28: ; preds = %18
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%30 = load i16, i16* %29
%31 = or i16 %30, %1
store i16 %31, i16* %29
br label %37
32: ; preds = %18
%33 = xor i16 %1, -1
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%35 = load i16, i16* %34
%36 = and i16 %35, %33
store i16 %36, i16* %34
br label %37
37: ; preds = %32, %28
%38 = load i32, i32* @SIOCSIFFLAGS
%39 = call i32 @ioctl(i32 %13, i32 %38, %struct.ifreq* %4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %37, %41
%45 = phi i32 [ -1, %41 ], [ 0, %37 ]
br label %46
46: ; preds = %15, %44, %6
%47 = phi i32 [ -1, %6 ], [ -1, %15 ], [ %45, %44 ]
ret i32 %47
}