#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int open_telnet(char *ifname) {
  int fd, on = 1;
  struct ifreq ifr;
  struct sockaddr_in sa;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
    return -1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(23);
  sa.sin_addr = ((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr;
  if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
    return -1;
  if (listen(fd, 1) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<open_telnet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c5 <open_telnet+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <open_telnet+0x128>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    1205 <open_telnet+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <open_telnet+0x128>
mov    -0x14(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x17,%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    127f <open_telnet+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <open_telnet+0x128>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12a2 <open_telnet+0x122>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <open_telnet+0x128>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_telnet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbp
movl   $0x1,0xc(%rsp)
mov    0x2e3c(%rip),%r15
mov    (%r15),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1271 <open_telnet+0xf1>
mov    %eax,%ebx
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
js     1271 <open_telnet+0xf1>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dba(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    $0x17,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x20(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1271 <open_telnet+0xf1>
mov    %ebx,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @open_telnet(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %52
14: ; preds = %1
%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* %4
%21 = load i32, i32* @SIOCGIFADDR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %14
store i32 -1, i32* %2
br label %52
25: ; preds = %14
%26 = load i32, i32* %4
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %5, i32 4)
%30 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @htons(i32 23)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%36 = bitcast i32* %35 to %struct.sockaddr_in*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %4
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %25
store i32 -1, i32* %2
br label %52
45: ; preds = %25
%46 = load i32, i32* %4
%47 = call i64 @listen(i32 %46, i32 1)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 -1, i32* %2
br label %52
50: ; preds = %45
%51 = load i32, i32* %4
store i32 %51, i32* %2
br label %52
52: ; preds = %50, %49, %44, %24, %13
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @open_telnet(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.ifreq
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%6 = bitcast %struct.ifreq* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %37, label %12
12: ; preds = %1
%13 = load i32, i32* @IFNAMSIZ
%14 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %13)
%15 = load i32, i32* @SIOCGIFADDR
%16 = call i64 @ioctl(i32 %10, i32 %15, %struct.ifreq* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %37, label %18
18: ; preds = %12
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %10, i32 %19, i32 %20, i32* %2, i32 4)
%22 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 23)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i64 @bind(i32 %10, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %18
%34 = call i64 @listen(i32 %10, i32 1)
%35 = icmp slt i64 %34, 0
%36 = select i1 %35, i32 -1, i32 %10
br label %37
37: ; preds = %33, %18, %12, %1
%38 = phi i32 [ -1, %1 ], [ -1, %12 ], [ -1, %18 ], [ %36, %33 ]
ret i32 %38
}