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

/* Variables and functions */
int /*<<< orphan*/ IFF_PROMISC;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int unset_promiscuous(const char *ifname) {
  struct ifreq req;
  int fd;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
  if (ioctl(fd, SIOCGIFFLAGS, (char *)&req) < 0)
    goto err;
  req.ifr_flags &= ~IFF_PROMISC;
  if (ioctl(fd, SIOCSIFFLAGS, (char *)&req) < 0)
    goto err;
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<unset_promiscuous>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117e <unset_promiscuous+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <unset_promiscuous+0xcc>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11b3 <unset_promiscuous+0x73>
jmp    11fd <unset_promiscuous+0xbd>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11e9 <unset_promiscuous+0xa9>
jmp    11fd <unset_promiscuous+0xbd>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    120c <unset_promiscuous+0xcc>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unset_promiscuous>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11ca <unset_promiscuous+0x8a>
mov    %eax,%ebx
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11bc <unset_promiscuous+0x7c>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x8(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    11c1 <unset_promiscuous+0x81>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11cf <unset_promiscuous+0x8f>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @unset_promiscuous(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %41
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 = call i32 @strncpy(i32 %14, i8* %15, i32 4)
%17 = load i32, i32* %5
%18 = load i32, i32* @SIOCGIFFLAGS
%19 = bitcast %struct.ifreq* %4 to i8*
%20 = call i64 @ioctl(i32 %17, i32 %18, i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %12
br label %38
23: ; preds = %12
%24 = load i32, i32* @IFF_PROMISC
%25 = xor i32 %24, -1
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = and i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* %5
%30 = load i32, i32* @SIOCSIFFLAGS
%31 = bitcast %struct.ifreq* %4 to i8*
%32 = call i64 @ioctl(i32 %29, i32 %30, i8* %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %23
br label %38
35: ; preds = %23
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
store i32 0, i32* %2
br label %41
38: ; preds = %34, %22
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %2
br label %41
41: ; preds = %38, %35, %11
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @unset_promiscuous(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @PF_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 %26, label %8
8: ; preds = %1
%9 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%10 = load i32, i32* @SIOCGIFFLAGS
%11 = call i64 @ioctl(i32 %6, i32 %10, i8* %3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %8
%14 = load i32, i32* @IFF_PROMISC
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = load i32, i32* @SIOCSIFFLAGS
%20 = call i64 @ioctl(i32 %6, i32 %19, i8* %3)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13, %8
br label %23
23: ; preds = %13, %22
%24 = phi i32 [ -1, %22 ], [ 0, %13 ]
%25 = call i32 @close(i32 %6)
br label %26
26: ; preds = %23, %1
%27 = phi i32 [ -1, %1 ], [ %24, %23 ]
ret i32 %27
}