#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_add(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 + 0), &ifr) < 0) {
    perror("SIOCVIRTENSLAVE");
    close(sockfd);
    return 1;
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<slaves_add>:
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_add+0x3c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <slaves_add+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_add+0x81>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <slaves_add+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    $0x0,%rsi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    124b <slaves_add+0xeb>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <slaves_add+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_add>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
cmp    $0x1,%esi
jg     1186 <slaves_add+0x26>
mov    (%rbx),%rsi
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%ebx
jmp    11f1 <slaves_add+0x91>
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     11fc <slaves_add+0x9c>
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
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     120f <slaves_add+0xaf>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11f1 <slaves_add+0x91>
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11e9 <slaves_add+0x89>

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"SIOCVIRTENSLAVE\00"
define i32 @slaves_add(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, 0
%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"SIOCVIRTENSLAVE\00"
define i32 @slaves_add(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 %31
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 %31
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 = call i64 @ioctl(i32 %13, i64 %23, %struct.ifreq* %4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %17, %26
%29 = phi i32 [ 1, %26 ], [ 0, %17 ]
%30 = call i32 @close(i32 %13)
br label %31
31: ; preds = %15, %28, %6
%32 = phi i32 [ 1, %6 ], [ 1, %15 ], [ %29, %28 ]
ret i32 %32
}