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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_IP;
scalar_t__ SIOCDEVPRIVATE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, scalar_t__, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int slaves_remove(const char *device, int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s <device>\n", argv[0]);
    return 1;
  }
  int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (sockfd < 0) {
    perror("socket");
    return 1;
  }
  struct ifreq ifr;
  strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  strncpy(ifr.ifr_slave, argv[1], sizeof(ifr.ifr_slave));
  if (ioctl(sockfd, (SIOCDEVPRIVATE + 1), &ifr) < 0) {
    perror("SIOCVIRTRELEASE");
    close(sockfd);
    return 1;
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<slaves_remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    119c <slaves_remove+0x3c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <slaves_remove+0xfa>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e1 <slaves_remove+0x81>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <slaves_remove+0xfa>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    124b <slaves_remove+0xeb>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <slaves_remove+0xfa>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slaves_remove>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
cmp    $0x1,%esi
jg     1186 <slaves_remove+0x26>
mov    (%rbx),%rsi
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%ebx
jmp    11f5 <slaves_remove+0x95>
mov    %rdi,%rbp
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1200 <slaves_remove+0xa0>
mov    %eax,%r14d
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x8(%rbx),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     1213 <slaves_remove+0xb3>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11f5 <slaves_remove+0x95>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11ed <slaves_remove+0x8d>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [20 x i8] c"Usage: %s <device>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@SIOCDEVPRIVATE = global i64 0
@.str.2 = [16 x i8] c"SIOCVIRTRELEASE\00"
define i32 @slaves_remove(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca %struct.ifreq
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i8**, i8*** %7
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %4
br label %49
17: ; preds = %3
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = load i32, i32* @IPPROTO_IP
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %49
26: ; preds = %17
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %5
%30 = call i32 @strncpy(i32 %28, i8* %29, i32 4)
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8**, i8*** %7
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strncpy(i32 %32, i8* %35, i32 4)
%37 = load i32, i32* %8
%38 = load i64, i64* @SIOCDEVPRIVATE
%39 = add nsw i64 %38, 1
%40 = call i64 @ioctl(i32 %37, i64 %39, %struct.ifreq* %9)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %26
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
store i32 1, i32* %4
br label %49
46: ; preds = %26
%47 = load i32, i32* %8
%48 = call i32 @close(i32 %47)
store i32 0, i32* %4
br label %49
49: ; preds = %46, %42, %24, %12
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [20 x i8] c"Usage: %s <device>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@SIOCDEVPRIVATE = global i64 0
@.str.2 = [16 x i8] c"SIOCVIRTRELEASE\00"
define i32 @slaves_remove(i8* %0, i32 %1, i8** nocapture %2) {
%4 = alloca %struct.ifreq
%5 = icmp slt i32 %1, 2
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i8*, i8** %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %32
9: ; preds = %3
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_IP
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %32
17: ; preds = %9
%18 = bitcast %struct.ifreq* %4 to i8*
%19 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%20 = getelementptr inbounds i8*, i8** %2, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @strncpy(i32 undef, i8* %21, i32 4)
%23 = load i64, i64* @SIOCDEVPRIVATE
%24 = add nsw i64 %23, 1
%25 = call i64 @ioctl(i32 %13, i64 %24, %struct.ifreq* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %17, %27
%30 = phi i32 [ 1, %27 ], [ 0, %17 ]
%31 = call i32 @close(i32 %13)
br label %32
32: ; preds = %15, %29, %6
%33 = phi i32 [ 1, %6 ], [ 1, %15 ], [ %30, %29 ]
ret i32 %33
}