#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFTXQLEN;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int sysapi_get_txqlen(char *ifname) {
  int ret;
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ret = ioctl(fd, SIOCGIFTXQLEN, &ifr);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  return ifr.ifr_qlen;
}


x86 assembly (clang, -O0):
<sysapi_get_txqlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    118e <sysapi_get_txqlen+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <sysapi_get_txqlen+0x9f>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e9 <sysapi_get_txqlen+0x99>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <sysapi_get_txqlen+0x9f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysapi_get_txqlen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11c8 <sysapi_get_txqlen+0x78>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11c1 <sysapi_get_txqlen+0x71>
mov    (%rsp),%ebx
jmp    11c8 <sysapi_get_txqlen+0x78>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFTXQLEN = global i32 0
define i32 @sysapi_get_txqlen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %31
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = load i32, i32* @IFNAMSIZ
%19 = call i32 @strncpy(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %5
%21 = load i32, i32* @SIOCGIFTXQLEN
%22 = call i32 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %2
br label %31
28: ; preds = %13
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %2
br label %31
31: ; preds = %28, %25, %12
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFTXQLEN = global i32 0
define i32 @sysapi_get_txqlen(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_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 %22, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%11 = load i32, i32* %10
%12 = load i32, i32* @IFNAMSIZ
%13 = call i32 @strncpy(i32 %11, i8* %0, i32 %12)
%14 = load i32, i32* @SIOCGIFTXQLEN
%15 = call i32 @ioctl(i32 %6, i32 %14, %struct.ifreq* %2)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = call i32 @close(i32 %6)
br label %22
19: ; preds = %8
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%21 = load i32, i32* %20
br label %22
22: ; preds = %1, %19, %17
%23 = phi i32 [ -1, %17 ], [ %21, %19 ], [ -1, %1 ]
ret i32 %23
}