#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 set_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):
<set_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 <set_promiscuous+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <set_promiscuous+0xc9>
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 <set_promiscuous+0x73>
jmp    11fa <set_promiscuous+0xba>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11e6 <set_promiscuous+0xa6>
jmp    11fa <set_promiscuous+0xba>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1209 <set_promiscuous+0xc9>
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):
<set_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     11c8 <set_promiscuous+0x88>
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     11ba <set_promiscuous+0x7a>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    11bf <set_promiscuous+0x7f>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11cd <set_promiscuous+0x8d>
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 @set_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 %40
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 %37
23: ; preds = %12
%24 = load i32, i32* @IFF_PROMISC
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* %5
%29 = load i32, i32* @SIOCSIFFLAGS
%30 = bitcast %struct.ifreq* %4 to i8*
%31 = call i64 @ioctl(i32 %28, i32 %29, i8* %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %23
br label %37
34: ; preds = %23
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
store i32 0, i32* %2
br label %40
37: ; preds = %33, %22
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %40
40: ; preds = %37, %34, %11
%41 = load i32, i32* %2
ret i32 %41
}

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 @set_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 %25, 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 %21, label %13
13: ; preds = %8
%14 = load i32, i32* @IFF_PROMISC
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = or i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @SIOCSIFFLAGS
%19 = call i64 @ioctl(i32 %6, i32 %18, i8* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %13, %8
br label %22
22: ; preds = %13, %21
%23 = phi i32 [ -1, %21 ], [ 0, %13 ]
%24 = call i32 @close(i32 %6)
br label %25
25: ; preds = %22, %1
%26 = phi i32 [ -1, %1 ], [ %23, %22 ]
ret i32 %26
}