#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 timeval {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  int sock1, sock2;
  if ((sock1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    return (EXIT_FAILURE);
  if ((sock2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    return (EXIT_FAILURE);
  fd_set fdset;
  FD_ZERO(&fdset);
  FD_SET(sock1, &fdset);
  FD_SET(sock2, &fdset);
  struct timeval timeout;
  timeout.tv_sec = 1;
  timeout.tv_usec = 1;
  if (select(sock2 + 1, &fdset, NULL, NULL, &timeout) < 0) {
    fprintf(stderr, "select() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ac <main+0x4c>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1285 <main+0x125>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11e9 <main+0x89>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1285 <main+0x125>
lea    -0x10(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x1,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jge    1279 <main+0x119>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xd9d(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1285 <main+0x125>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e77(%rip),%r14
mov    (%r14),%edi
mov    0x2e4d(%rip),%r15
mov    (%r15),%esi
mov    0x2e5b(%rip),%rbx
mov    (%rbx),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1225 <main+0xc5>
mov    %eax,%ebp
mov    (%r14),%edi
mov    (%r15),%esi
mov    (%rbx),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1225 <main+0xc5>
mov    %eax,%ebx
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
movabs $0x100000001,%rax
mov    %rax,0x8(%rsp)
add    $0x1,%ebx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
js     11fd <main+0x9d>
mov    0x2db5(%rip),%rax
jmp    122c <main+0xcc>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebx
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xde5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"select() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %1
br label %41
13: ; preds = %0
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_TCP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i32, i32* @EXIT_FAILURE
store i32 %20, i32* %1
br label %41
21: ; preds = %13
%22 = call i32 @FD_ZERO(i32* %4)
%23 = load i32, i32* %2
%24 = call i32 @FD_SET(i32 %23, i32* %4)
%25 = load i32, i32* %3
%26 = call i32 @FD_SET(i32 %25, i32* %4)
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 1, i32* %27
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i32 1, i32* %28
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
%31 = call i64 @select(i32 %30, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %21
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
store i32 %38, i32* %1
br label %41
39: ; preds = %21
%40 = load i32, i32* @EXIT_SUCCESS
store i32 %40, i32* %1
br label %41
41: ; preds = %39, %33, %19, %11
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"select() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
br label %37
10: ; preds = %0
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = load i32, i32* @EXIT_FAILURE
br label %37
18: ; preds = %10
%19 = bitcast i32* %1 to i8*
%20 = call i32 @FD_ZERO(i32* %1)
%21 = call i32 @FD_SET(i32 %6, i32* %1)
%22 = call i32 @FD_SET(i32 %14, i32* %1)
%23 = bitcast %struct.timeval* %2 to i8*
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 1, i32* %24
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i32 1, i32* %25
%26 = add nuw nsw i32 %14, 1
%27 = call i64 @select(i32 %26, i32* %1, i32* null, i32* null, %struct.timeval* %2)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %18, %29
%35 = phi i32* [ @EXIT_FAILURE, %29 ], [ @EXIT_SUCCESS, %18 ]
%36 = load i32, i32* %35
br label %37
37: ; preds = %34, %16, %8
%38 = phi i32 [ %9, %8 ], [ %17, %16 ], [ %36, %34 ]
ret i32 %38
}