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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ atoi(char *);
int connect_to(char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ get_socket_error(int);
int /*<<< orphan*/ set_up_timeout(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ wait_for_socket(int, struct timeval *);
int main(int argc, char **argv) {
  int handle_socket;
  struct timeval timeout;
  if (argc != (3 + 1)) {
    fprintf(stderr,
            "Exactly three arguments expected: hostname, numeric port, timeout "
            "in seconds. %d received instead.\n",
            argc - 1);
    exit(EXIT_FAILURE);
  }
  if (!set_up_timeout(&timeout, atoi(argv[3]))) {
    fprintf(stderr,
            "Invalid timeout \"%s\" specified. Must be a positive integer.\n",
            argv[3]);
    exit(EXIT_FAILURE);
  }
  if ((handle_socket = connect_to(argv[1], argv[2])) == -1) {
    fprintf(stderr,
            "Failed to open a socket. Invalid hostname or port number?\n");
    exit(EXIT_FAILURE);
  }
  if (wait_for_socket(handle_socket, &timeout) <= 0) {
    fprintf(stderr, "Connection timed out.\n");
    exit(EXIT_FAILURE);
  }
  if (get_socket_error(handle_socket) != 0) {
    fprintf(stderr, "Connection closed.\n");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11bb <main+0x4b>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,%esi
lea    -0x18(%rbp),%rdi
call   1030 <set_up_timeout@plt>
cmp    $0x0,%eax
jne    1209 <main+0x99>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1040 <connect_to@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124f <main+0xdf>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <wait_for_socket@plt>
cmp    $0x0,%rax
jg     128a <main+0x11a>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <get_socket_error@plt>
cmp    $0x0,%rax
je     12c1 <main+0x151>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x4,%edi
jne    11b6 <main+0x46>
mov    %rsi,%rbx
mov    0x18(%rsi),%rdi
call   1070 <atoi@plt>
lea    0x8(%rsp),%rdi
mov    %eax,%esi
call   1030 <set_up_timeout@plt>
test   %eax,%eax
jne    11d4 <main+0x64>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x18(%rbx),%rdx
lea    0xeba(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    124e <main+0xde>
mov    %edi,%edx
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
add    $0xffffffff,%edx
lea    0xe35(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    124e <main+0xde>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
call   1040 <connect_to@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0x88>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xeab(%rip),%rsi
jmp    1247 <main+0xd7>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <wait_for_socket@plt>
test   %rax,%rax
jg     121d <main+0xad>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xec1(%rip),%rsi
jmp    1247 <main+0xd7>
mov    %ebx,%edi
call   1060 <get_socket_error@plt>
test   %rax,%rax
jne    1237 <main+0xc7>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xeac(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
@stderr = global i32 0
@.str = [100 x i8] c"Exactly three arguments expected: hostname, numeric port, timeout in seconds. %d received instead.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [61 x i8] c"Invalid timeout \22%s\22 specified. Must be a positive integer.\0A\00"
@.str.2 = [59 x i8] c"Failed to open a socket. Invalid hostname or port number?\0A\00"
@.str.3 = [23 x i8] c"Connection timed out.\0A\00"
@.str.4 = [20 x i8] c"Connection closed.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 4
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* %4
%13 = sub nsw i32 %12, 1
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 3
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
%22 = call i32 @set_up_timeout(%struct.timeval* %7, i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 3
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 @connect_to(i8* %35, i8* %38)
store i32 %39, i32* %6
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %32
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %32
%47 = load i32, i32* %6
%48 = call i64 @wait_for_socket(i32 %47, %struct.timeval* %7)
%49 = icmp sle i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %46
%56 = load i32, i32* %6
%57 = call i64 @get_socket_error(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %55
%65 = load i32, i32* @EXIT_SUCCESS
%66 = call i32 @exit(i32 %65)
unreachable
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@stderr = global i32 0
@.str = [100 x i8] c"Exactly three arguments expected: hostname, numeric port, timeout in seconds. %d received instead.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [61 x i8] c"Invalid timeout \22%s\22 specified. Must be a positive integer.\0A\00"
@.str.2 = [59 x i8] c"Failed to open a socket. Invalid hostname or port number?\0A\00"
@.str.3 = [23 x i8] c"Connection timed out.\0A\00"
@.str.4 = [20 x i8] c"Connection closed.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = icmp eq i32 %0, 4
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = add nsw i32 %0, -1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 3
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = call i32 @set_up_timeout(%struct.timeval* %3, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %13
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %12
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @connect_to(i8* %26, i8* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %24
%37 = call i64 @wait_for_socket(i32 %29, %struct.timeval* %3)
%38 = icmp slt i64 %37, 1
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
%45 = call i64 @get_socket_error(i32 %29)
%46 = icmp eq i64 %45, 0
br i1 %46, label %52, label %47
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %44
%53 = load i32, i32* @EXIT_SUCCESS
%54 = call i32 @exit(i32 %53)
unreachable
}