#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  int tv_sec;
  int tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ so_error;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
#define EINPROGRESS 129
#define EWOULDBLOCK 128
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int terminate(char *, int);
int scanner(const char *host, int port, int timeout) {
  struct sockaddr_in address;
  struct timeval tv;
  fd_set write_fds;
  socklen_t so_error_len;
  int sd, so_error;
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = inet_addr(host);
  address.sin_port = htons(port);
  tv.tv_sec = timeout;
  tv.tv_usec = timeout * 1000000;
  FD_ZERO(&write_fds);
  so_error_len = sizeof(so_error);
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return terminate("socket() An error has occurred", 0);
  fcntl(sd, F_SETFL, O_NONBLOCK);
  if (connect(sd, (struct sockaddr *)&address, sizeof(address)) == -1) {
    switch (errno) {
    case EWOULDBLOCK:
    case EINPROGRESS:
      break;
    default:
      return terminate("connect() An error has occurred", sd);
    }
  }
  FD_SET(sd, &write_fds);
  if (select(sd + 1, NULL, &write_fds, NULL, &tv) == -1)
    return terminate("select() An error has occurred", sd);
  if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &so_error, &so_error_len) < 0)
    return terminate("getsockopt() An error has occurred", sd);
  close(sd);
  return (so_error == 0);
}


x86 assembly (clang, -O0):
<scanner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
imul   $0xf4240,-0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
movl   $0x4,-0x38(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    1240 <scanner+0x90>
lea    0xdcf(%rip),%rdi
xor    %esi,%esi
call   10a0 <terminate@plt>
mov    %eax,-0x4(%rbp)
jmp    134f <scanner+0x19f>
mov    -0x3c(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12af <scanner+0xff>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     1293 <scanner+0xe3>
jmp    128e <scanner+0xde>
jmp    12aa <scanner+0xfa>
mov    -0x3c(%rbp),%esi
lea    0xd82(%rip),%rdi
call   10a0 <terminate@plt>
mov    %eax,-0x4(%rbp)
jmp    134f <scanner+0x19f>
jmp    12af <scanner+0xff>
mov    -0x3c(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x3c(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x34(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1090 <select@plt>
cmp    $0xffffffff,%eax
jne    12f5 <scanner+0x145>
mov    -0x3c(%rbp),%esi
lea    0xd57(%rip),%rdi
call   10a0 <terminate@plt>
mov    %eax,-0x4(%rbp)
jmp    134f <scanner+0x19f>
mov    -0x3c(%rbp),%edi
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
jge    1338 <scanner+0x188>
mov    -0x3c(%rbp),%esi
lea    0xd33(%rip),%rdi
call   10a0 <terminate@plt>
mov    %eax,-0x4(%rbp)
jmp    134f <scanner+0x19f>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x40(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanner>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    0x2e1d(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x28(%rsp)
call   1070 <inet_addr@plt>
mov    %eax,0x24(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x20(%rsp)
mov    %ebx,0x18(%rsp)
imul   $0xf4240,%ebx,%eax
mov    %eax,0x1c(%rsp)
lea    0x14(%rsp),%rdi
call   1080 <FD_ZERO@plt>
movl   $0x4,0x10(%rsp)
mov    (%r14),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c3 <scanner+0x113>
mov    %eax,%ebx
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    125e <scanner+0xae>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    12d7 <scanner+0x127>
lea    0x14(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %ebx,%edi
add    $0x1,%edi
lea    0x18(%rsp),%r8
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1090 <select@plt>
cmp    $0xffffffff,%eax
je     12ce <scanner+0x11e>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d18(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
call   1060 <getsockopt@plt>
test   %rax,%rax
js     12e0 <scanner+0x130>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmpl   $0x0,0xc(%rsp)
sete   %al
jmp    12ee <scanner+0x13e>
lea    0xd36(%rip),%rdi
xor    %esi,%esi
jmp    12e9 <scanner+0x139>
lea    0xd6a(%rip),%rdi
jmp    12e7 <scanner+0x137>
lea    0xd41(%rip),%rdi
jmp    12e7 <scanner+0x137>
lea    0xd77(%rip),%rdi
mov    %ebx,%esi
call   10a0 <terminate@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"socket() An error has occurred\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [32 x i8] c"connect() An error has occurred\00"
@.str.2 = [31 x i8] c"select() An error has occurred\00"
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str.3 = [35 x i8] c"getsockopt() An error has occurred\00"
define i32 @scanner(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i8*, i8** %5
%17 = call i32 @inet_addr(i8* %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %6
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %7
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %7
%26 = mul nsw i32 %25, 1000000
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @FD_ZERO(i32* %10)
store i32 4, i32* %11
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %12
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %3
%34 = call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %34, i32* %4
br label %76
35: ; preds = %3
%36 = load i32, i32* %12
%37 = load i32, i32* @F_SETFL
%38 = load i32, i32* @O_NONBLOCK
%39 = call i32 @fcntl(i32 %36, i32 %37, i32 %38)
%40 = load i32, i32* %12
%41 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%42 = call i32 @connect(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %51
44: ; preds = %35
%45 = load i32, i32* @errno
switch i32 %45, label %47 [
i32 128, label %46
i32 129, label %46
]
46: ; preds = %44, %44
br label %50
47: ; preds = %44
%48 = load i32, i32* %12
%49 = call i32 @terminate(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %48)
store i32 %49, i32* %4
br label %76
50: ; preds = %46
br label %51
51: ; preds = %50, %35
%52 = load i32, i32* %12
%53 = call i32 @FD_SET(i32 %52, i32* %10)
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
%56 = call i32 @select(i32 %55, i32* null, i32* %10, i32* null, %struct.timeval* %9)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = load i32, i32* %12
%60 = call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %59)
store i32 %60, i32* %4
br label %76
61: ; preds = %51
%62 = load i32, i32* %12
%63 = load i32, i32* @SOL_SOCKET
%64 = load i32, i32* @SO_ERROR
%65 = call i64 @getsockopt(i32 %62, i32 %63, i32 %64, i32* %13, i32* %11)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = load i32, i32* %12
%69 = call i32 @terminate(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %68)
store i32 %69, i32* %4
br label %76
70: ; preds = %61
%71 = load i32, i32* %12
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %13
%74 = icmp eq i32 %73, 0
%75 = zext i1 %74 to i32
store i32 %75, i32* %4
br label %76
76: ; preds = %70, %67, %58, %47, %33
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"socket() An error has occurred\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [32 x i8] c"connect() An error has occurred\00"
@.str.2 = [31 x i8] c"select() An error has occurred\00"
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str.3 = [35 x i8] c"getsockopt() An error has occurred\00"
define i32 @scanner(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast %struct.timeval* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @inet_addr(i8* %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 %1)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %2, i32* %20
%21 = mul nsw i32 %2, 1000000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i32 %21, i32* %22
%23 = call i32 @FD_ZERO(i32* %6)
store i32 4, i32* %7
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %3
%29 = call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 0)
br label %62
30: ; preds = %3
%31 = load i32, i32* @F_SETFL
%32 = load i32, i32* @O_NONBLOCK
%33 = call i32 @fcntl(i32 %26, i32 %31, i32 %32)
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @connect(i32 %26, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %43
37: ; preds = %30
%38 = load i32, i32* @errno
%39 = and i32 %38, -2
%40 = icmp eq i32 %39, 128
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 @terminate(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %62
43: ; preds = %37, %30
%44 = call i32 @FD_SET(i32 %26, i32* %6)
%45 = add nuw nsw i32 %26, 1
%46 = call i32 @select(i32 %45, i32* null, i32* %6, i32* null, %struct.timeval* %5)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %62
50: ; preds = %43
%51 = load i32, i32* @SOL_SOCKET
%52 = load i32, i32* @SO_ERROR
%53 = call i64 @getsockopt(i32 %26, i32 %51, i32 %52, i32* %8, i32* %7)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @terminate(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %62
57: ; preds = %50
%58 = call i32 @close(i32 %26)
%59 = load i32, i32* %8
%60 = icmp eq i32 %59, 0
%61 = zext i1 %60 to i32
br label %62
62: ; preds = %57, %55, %48, %41, %28
%63 = phi i32 [ %29, %28 ], [ %42, %41 ], [ %49, %48 ], [ %56, %55 ], [ %61, %57 ]
ret i32 %63
}