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

/* Variables and functions */
int IFF_PROMISC;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct ifreq *, int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ t01_log(int, char *, ...);
int manage_interface_promisc_mode(const char *interface, int on) {
  int fd;
  struct ifreq ethreq;
  int ioctl_res;
  int promisc_enabled_on_device;
  int ioctl_res_set;
  fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (!fd) {
    t01_log(3, "Can't create socket for promisc mode manager");
    return -1;
  }
  bzero(&ethreq, sizeof(ethreq));
  strncpy(ethreq.ifr_name, interface, IFNAMSIZ);
  ioctl_res = ioctl(fd, SIOCGIFFLAGS, &ethreq);
  if (ioctl_res == -1) {
    t01_log(3, "Can't get interface flags");
    return -1;
  }
  promisc_enabled_on_device = ethreq.ifr_flags & IFF_PROMISC;
  if (on) {
    if (promisc_enabled_on_device) {
      t01_log(0, "Interface %s in promisc mode already", interface);
      return 0;
    } else {
      t01_log(0, "Interface %s in non promisc mode now, switch it on",
              interface);
      ethreq.ifr_flags |= IFF_PROMISC;
      ioctl_res_set = ioctl(fd, SIOCSIFFLAGS, &ethreq);
      if (ioctl_res_set == -1) {
        t01_log(3, "Can't set interface flags");
        return -1;
      }
      return 1;
    }
  } else {
    if (!promisc_enabled_on_device) {
      t01_log(0, "Interface %s in normal mode already", interface);
      return 0;
    } else {
      t01_log(0, "Interface in promisc mode now, switch it off");
      ethreq.ifr_flags &= ~IFF_PROMISC;
      ioctl_res_set = ioctl(fd, SIOCSIFFLAGS, &ethreq);
      if (ioctl_res_set == -1) {
        t01_log(3, "Can't set interface flags");
        return -1;
      }
      return 1;
    }
  }
}


x86 assembly (clang, -O0):
<manage_interface_promisc_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ab <manage_interface_promisc_mode+0x5b>
mov    $0x3,%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    120f <manage_interface_promisc_mode+0xbf>
mov    $0x3,%edi
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
mov    -0x20(%rbp),%eax
mov    0x2d97(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c2 <manage_interface_promisc_mode+0x172>
cmpl   $0x0,-0x28(%rbp)
je     1252 <manage_interface_promisc_mode+0x102>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12b6 <manage_interface_promisc_mode+0x166>
mov    $0x3,%edi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
movl   $0x1,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
cmpl   $0x0,-0x28(%rbp)
jne    12ec <manage_interface_promisc_mode+0x19c>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
xor    %edi,%edi
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
mov    0x2cad(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    134f <manage_interface_promisc_mode+0x1ff>
mov    $0x3,%edi
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   1050 <t01_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <manage_interface_promisc_mode+0x206>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manage_interface_promisc_mode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
je     11e2 <manage_interface_promisc_mode+0x92>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0x4(%rsp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11eb <manage_interface_promisc_mode+0x9b>
mov    0x2de9(%rip),%rbp
mov    0x0(%rbp),%eax
and    (%rsp),%eax
test   %r15d,%r15d
je     11f4 <manage_interface_promisc_mode+0xa4>
test   %eax,%eax
je     1212 <manage_interface_promisc_mode+0xc2>
lea    0xe6a(%rip),%rsi
jmp    1269 <manage_interface_promisc_mode+0x119>
lea    0xe17(%rip),%rsi
jmp    124f <manage_interface_promisc_mode+0xff>
lea    0xe3b(%rip),%rsi
jmp    124f <manage_interface_promisc_mode+0xff>
test   %eax,%eax
je     1262 <manage_interface_promisc_mode+0x112>
lea    0xede(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1050 <t01_log@plt>
mov    0x0(%rbp),%eax
not    %eax
and    %eax,(%rsp)
jmp    122b <manage_interface_promisc_mode+0xdb>
lea    0xe53(%rip),%rsi
xor    %edi,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <t01_log@plt>
mov    0x0(%rbp),%eax
or     %eax,(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    1277 <manage_interface_promisc_mode+0x127>
lea    0xe50(%rip),%rsi
mov    $0x3,%edi
xor    %eax,%eax
call   1050 <t01_log@plt>
mov    $0xffffffff,%ebx
jmp    1277 <manage_interface_promisc_mode+0x127>
lea    0xe50(%rip),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <t01_log@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [45 x i8] c"Can't create socket for promisc mode manager\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [26 x i8] c"Can't get interface flags\00"
@IFF_PROMISC = global i32 0
@.str.2 = [37 x i8] c"Interface %s in promisc mode already\00"
@.str.3 = [51 x i8] c"Interface %s in non promisc mode now, switch it on\00"
@SIOCSIFFLAGS = global i32 0
@.str.4 = [26 x i8] c"Can't set interface flags\00"
@.str.5 = [36 x i8] c"Interface %s in normal mode already\00"
@.str.6 = [45 x i8] c"Interface in promisc mode now, switch it off\00"
define i32 @manage_interface_promisc_mode(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
19: ; preds = %2
%20 = call i32 @bzero(%struct.ifreq* %7, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i8*, i8** %4
%24 = load i32, i32* @IFNAMSIZ
%25 = call i32 @strncpy(i32 %22, i8* %23, i32 %24)
%26 = load i32, i32* %6
%27 = load i32, i32* @SIOCGIFFLAGS
%28 = call i32 @ioctl(i32 %26, i32 %27, %struct.ifreq* %7)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
33: ; preds = %19
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @IFF_PROMISC
%37 = and i32 %35, %36
store i32 %37, i32* %9
%38 = load i32, i32* %5
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %61
40: ; preds = %33
%41 = load i32, i32* %9
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** %4
%45 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 0, i32* %3
br label %82
46: ; preds = %40
%47 = load i8*, i8** %4
%48 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @IFF_PROMISC
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = or i32 %51, %49
store i32 %52, i32* %50
%53 = load i32, i32* %6
%54 = load i32, i32* @SIOCSIFFLAGS
%55 = call i32 @ioctl(i32 %53, i32 %54, %struct.ifreq* %7)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %46
%59 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
60: ; preds = %46
store i32 1, i32* %3
br label %82
61: ; preds = %33
%62 = load i32, i32* %9
%63 = icmp ne i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %61
%65 = load i8*, i8** %4
%66 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %65)
store i32 0, i32* %3
br label %82
67: ; preds = %61
%68 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%69 = load i32, i32* @IFF_PROMISC
%70 = xor i32 %69, -1
%71 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%72 = load i32, i32* %71
%73 = and i32 %72, %70
store i32 %73, i32* %71
%74 = load i32, i32* %6
%75 = load i32, i32* @SIOCSIFFLAGS
%76 = call i32 @ioctl(i32 %74, i32 %75, %struct.ifreq* %7)
store i32 %76, i32* %10
%77 = load i32, i32* %10
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %67
%80 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
81: ; preds = %67
store i32 1, i32* %3
br label %82
82: ; preds = %81, %79, %64, %60, %58, %43, %31, %17
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [45 x i8] c"Can't create socket for promisc mode manager\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [26 x i8] c"Can't get interface flags\00"
@IFF_PROMISC = global i32 0
@.str.2 = [37 x i8] c"Interface %s in promisc mode already\00"
@.str.3 = [51 x i8] c"Interface %s in non promisc mode now, switch it on\00"
@SIOCSIFFLAGS = global i32 0
@.str.4 = [26 x i8] c"Can't set interface flags\00"
@.str.5 = [36 x i8] c"Interface %s in normal mode already\00"
@.str.6 = [45 x i8] c"Interface in promisc mode now, switch it off\00"
define i32 @manage_interface_promisc_mode(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %57
12: ; preds = %2
%13 = call i32 @bzero(%struct.ifreq* %3, i32 8)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = load i32, i32* @IFNAMSIZ
%17 = tail call i32 @strncpy(i32 %15, i8* %0, i32 %16)
%18 = load i32, i32* @SIOCGIFFLAGS
%19 = call i32 @ioctl(i32 %8, i32 %18, %struct.ifreq* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %57
23: ; preds = %12
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @IFF_PROMISC
%27 = and i32 %26, %25
%28 = icmp eq i32 %1, 0
%29 = icmp eq i32 %27, 0
br i1 %28, label %43, label %30
30: ; preds = %23
br i1 %29, label %33, label %31
31: ; preds = %30
%32 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %57
33: ; preds = %30
%34 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%35 = load i32, i32* @IFF_PROMISC
%36 = load i32, i32* %24
%37 = or i32 %36, %35
store i32 %37, i32* %24
%38 = load i32, i32* @SIOCSIFFLAGS
%39 = call i32 @ioctl(i32 %8, i32 %38, %struct.ifreq* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %57
41: ; preds = %33
%42 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %57
43: ; preds = %23
br i1 %29, label %44, label %46
44: ; preds = %43
%45 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %57
46: ; preds = %43
%47 = call i32 (i32, i8*, ...) @t01_log(i32 0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* @IFF_PROMISC
%49 = xor i32 %48, -1
%50 = load i32, i32* %24
%51 = and i32 %50, %49
store i32 %51, i32* %24
%52 = load i32, i32* @SIOCSIFFLAGS
%53 = call i32 @ioctl(i32 %8, i32 %52, %struct.ifreq* %3)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %46
%56 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %46, %33, %55, %44, %41, %31, %21, %10
%58 = phi i32 [ -1, %21 ], [ 0, %31 ], [ -1, %41 ], [ -1, %55 ], [ 0, %44 ], [ -1, %10 ], [ 1, %33 ], [ 1, %46 ]
ret i32 %58
}