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

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCSIFTXQLEN;
int /*<<< orphan*/ close(int);
int get_ctl_fd();
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int set_qlen(const char *dev, int qlen) {
  struct ifreq ifr;
  int s;
  s = get_ctl_fd();
  if (s < 0)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  ifr.ifr_qlen = qlen;
  if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
    perror("SIOCSIFXQLEN");
    close(s);
    return -1;
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<set_qlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1080 <get_ctl_fd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118f <set_qlen+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <set_qlen+0xa8>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11f9 <set_qlen+0x99>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <set_qlen+0xa8>
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):
<set_qlen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %r15d,%r15d
xor    %eax,%eax
call   1080 <get_ctl_fd@plt>
test   %eax,%eax
js     11d1 <set_qlen+0x71>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    %r14d,0x8(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11e7 <set_qlen+0x87>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11d7 <set_qlen+0x77>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r15d
jmp    11c8 <set_qlen+0x68>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCSIFTXQLEN = global i32 0
@.str = [13 x i8] c"SIOCSIFXQLEN\00"
define i32 @set_qlen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 (...) @get_ctl_fd()
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %32
12: ; preds = %2
%13 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i8*, i8** %4
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i32 %15, i8* %16, i32 %17)
%19 = load i32, i32* %5
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %7
%22 = load i32, i32* @SIOCSIFTXQLEN
%23 = call i64 @ioctl(i32 %21, i32 %22, %struct.ifreq* %6)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %12
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %3
br label %32
29: ; preds = %12
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 0, i32* %3
br label %32
32: ; preds = %29, %25, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCSIFTXQLEN = global i32 0
@.str = [13 x i8] c"SIOCSIFXQLEN\00"
define i32 @set_qlen(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 (...) @get_ctl_fd()
%6 = icmp slt i32 %5, 0
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = load i32, i32* @IFNAMSIZ
%12 = call i32 @strncpy(i32 %10, i8* %0, i32 %11)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %1, i32* %13
%14 = load i32, i32* @SIOCSIFTXQLEN
%15 = call i64 @ioctl(i32 %5, i32 %14, %struct.ifreq* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %7, %17
%20 = phi i32 [ -1, %17 ], [ 0, %7 ]
%21 = call i32 @close(i32 %5)
br label %22
22: ; preds = %19, %2
%23 = phi i32 [ -1, %2 ], [ %20, %19 ]
ret i32 %23
}