#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ fds;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dbgprintf(char *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(short);
int read(int, char *, size_t const);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ set_nonblock_mode(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *get_remote_hostname(int ip, int port, int timeout, char *hostname,
                          const size_t hostnamesize) {
  int s;
  struct sockaddr_in sa;
  long flags;
  ssize_t nbytes;
  fd_set fds[1];
  struct timeval tv;
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1)
    err(1, "socket");
  bzero(&sa, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons((short)port);
  sa.sin_addr.s_addr = htonl(ip);
  dbgprintf("setting non-blocking mode on socket\n");
  set_nonblock_mode(s, 1);
  dbgprintf("calling connect...\n");
  connect(s, (struct sockaddr *)&sa, sizeof(sa));
  dbgprintf("select(2)...\n");
  FD_ZERO(fds);
  FD_SET(s, fds);
  tv.tv_sec = timeout / 1000;
  tv.tv_usec = (timeout % 1000) * 1000;
  if (select(sizeof(fds) / sizeof(*fds), fds, fds, fds, &tv) != 0)
    err(1, "select");
  dbgprintf("select(2) returned\n");
  dbgprintf("reading the hostname... ");
  nbytes = read(s, hostname, hostnamesize - 1);
  close(s);
  if (nbytes == -1) {
    dbgprintf("read(2) failed, the socket wasn't successfully connected\n");
    return NULL;
  } else {
    hostname[nbytes] = '\0';
    return hostname;
  }
}


x86 assembly (clang, -O0):
<get_remote_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1220 <get_remote_hostname+0x50>
mov    $0x1,%edi
lea    0xde5(%rip),%rsi
call   1090 <err@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
movswl %ax,%edi
call   1030 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x38(%rbp)
lea    0xdad(%rip),%rdi
call   10a0 <dbgprintf@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   10b0 <set_nonblock_mode@plt>
lea    0xdb9(%rip),%rdi
call   10a0 <dbgprintf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
lea    0xdb0(%rip),%rdi
call   10a0 <dbgprintf@plt>
lea    -0x48(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x2c(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x4c(%rbp)
lea    -0x48(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x48(%rbp),%rcx
mov    $0x1,%edi
lea    -0x50(%rbp),%r8
call   1080 <select@plt>
cmp    $0x0,%rax
je     1301 <get_remote_hostname+0x131>
mov    $0x1,%edi
lea    0xd52(%rip),%rsi
call   1090 <err@plt>
lea    0xd4d(%rip),%rdi
call   10a0 <dbgprintf@plt>
lea    0xd55(%rip),%rdi
call   10a0 <dbgprintf@plt>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0xffffffff,-0x44(%rbp)
jne    135b <get_remote_hostname+0x18b>
lea    0xd39(%rip),%rdi
call   10a0 <dbgprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    136f <get_remote_hostname+0x19f>
mov    -0x20(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_remote_hostname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r12
mov    %rcx,0x28(%rsp)
mov    %edx,0xc(%rsp)
mov    %esi,%r15d
mov    %edi,%ebx
mov    0x2df2(%rip),%r14
mov    (%r14),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1222 <get_remote_hostname+0x52>
lea    0xde8(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x20(%rsp)
movswl %r15w,%edi
call   1030 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    %ebx,%edi
call   1040 <htonl@plt>
mov    %eax,0x18(%rsp)
lea    0xdad(%rip),%rdi
call   10a0 <dbgprintf@plt>
mov    %r13d,%edi
mov    $0x1,%esi
call   10b0 <set_nonblock_mode@plt>
lea    0xdb9(%rip),%rdi
call   10a0 <dbgprintf@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
lea    0xdb1(%rip),%rdi
call   10a0 <dbgprintf@plt>
lea    0x34(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
movslq 0xc(%rsp),%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x10(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1080 <select@plt>
test   %rax,%rax
je     1309 <get_remote_hostname+0x139>
lea    0xd4f(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
lea    0xd45(%rip),%rdi
call   10a0 <dbgprintf@plt>
lea    0xd4d(%rip),%rdi
call   10a0 <dbgprintf@plt>
add    $0xffffffffffffffff,%r12
mov    %r13d,%edi
mov    0x28(%rsp),%rbp
mov    %rbp,%rsi
mov    %r12,%rdx
call   1060 <read@plt>
mov    %eax,%ebx
mov    %r13d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%ebx
je     1351 <get_remote_hostname+0x181>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
jmp    135f <get_remote_hostname+0x18f>
lea    0xd2a(%rip),%rdi
call   10a0 <dbgprintf@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, 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 = [7 x i8] c"socket\00"
@.str.1 = [37 x i8] c"setting non-blocking mode on socket\0A\00"
@.str.2 = [20 x i8] c"calling connect...\0A\00"
@.str.3 = [14 x i8] c"select(2)...\0A\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [20 x i8] c"select(2) returned\0A\00"
@.str.6 = [25 x i8] c"reading the hostname... \00"
@.str.7 = [58 x i8] c"read(2) failed, the socket wasn't successfully connected\0A\00"
define i8* @get_remote_hostname(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca i64
%15 = alloca i32
%16 = alloca [1 x i32]
%17 = alloca %struct.timeval
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %5
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %5
%26 = call i32 @bzero(%struct.sockaddr_in* %13, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* %8
%30 = trunc i32 %29 to i16
%31 = call i32 @htons(i16 signext %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* %7
%34 = call i32 @htonl(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = call i32 @dbgprintf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %12
%39 = call i32 @set_nonblock_mode(i32 %38, i32 1)
%40 = call i32 @dbgprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %12
%42 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%43 = call i32 @connect(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = call i32 @dbgprintf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%45 = getelementptr inbounds [1 x i32], [1 x i32]* %16, i64 0, i64 0
%46 = call i32 @FD_ZERO(i32* %45)
%47 = load i32, i32* %12
%48 = getelementptr inbounds [1 x i32], [1 x i32]* %16, i64 0, i64 0
%49 = call i32 @FD_SET(i32 %47, i32* %48)
%50 = load i32, i32* %9
%51 = sdiv i32 %50, 1000
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 0
store i32 %51, i32* %52
%53 = load i32, i32* %9
%54 = srem i32 %53, 1000
%55 = mul nsw i32 %54, 1000
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
store i32 %55, i32* %56
%57 = getelementptr inbounds [1 x i32], [1 x i32]* %16, i64 0, i64 0
%58 = getelementptr inbounds [1 x i32], [1 x i32]* %16, i64 0, i64 0
%59 = getelementptr inbounds [1 x i32], [1 x i32]* %16, i64 0, i64 0
%60 = call i64 @select(i32 1, i32* %57, i32* %58, i32* %59, %struct.timeval* %17)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %25
%63 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %25
%65 = call i32 @dbgprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%66 = call i32 @dbgprintf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%67 = load i32, i32* %12
%68 = load i8*, i8** %10
%69 = load i64, i64* %11
%70 = sub i64 %69, 1
%71 = call i32 @read(i32 %67, i8* %68, i64 %70)
store i32 %71, i32* %15
%72 = load i32, i32* %12
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %15
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %64
%77 = call i32 @dbgprintf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0))
store i8* null, i8** %6
br label %84
78: ; preds = %64
%79 = load i8*, i8** %10
%80 = load i32, i32* %15
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %79, i64 %81
store i8 0, i8* %82
%83 = load i8*, i8** %10
store i8* %83, i8** %6
br label %84
84: ; preds = %78, %76
%85 = load i8*, i8** %6
ret i8* %85
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, 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 = [7 x i8] c"socket\00"
@.str.1 = [37 x i8] c"setting non-blocking mode on socket\0A\00"
@.str.2 = [20 x i8] c"calling connect...\0A\00"
@.str.3 = [14 x i8] c"select(2)...\0A\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [20 x i8] c"select(2) returned\0A\00"
@.str.6 = [25 x i8] c"reading the hostname... \00"
@.str.7 = [58 x i8] c"read(2) failed, the socket wasn't successfully connected\0A\00"
define i8* @get_remote_hostname(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca [1 x i32]
%8 = alloca %struct.timeval
%9 = bitcast %struct.sockaddr_in* %6 to i8*
%10 = bitcast [1 x i32]* %7 to i8*
%11 = bitcast %struct.timeval* %8 to i8*
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %5
%19 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %20, i32* %21
%22 = trunc i32 %1 to i16
%23 = tail call i32 @htons(i16 signext %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %23, i32* %24
%25 = tail call i32 @htonl(i32 %0)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %25, i32* %26
%27 = tail call i32 @dbgprintf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @set_nonblock_mode(i32 %14, i32 1)
%29 = tail call i32 @dbgprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i32 @connect(i32 %14, %struct.sockaddr* %30, i32 12)
%32 = call i32 @dbgprintf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%33 = getelementptr inbounds [1 x i32], [1 x i32]* %7, i64 0, i64 0
%34 = call i32 @FD_ZERO(i32* %33)
%35 = call i32 @FD_SET(i32 %14, i32* %33)
%36 = sdiv i32 %2, 1000
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 %36, i32* %37
%38 = srem i32 %2, 1000
%39 = mul nsw i32 %38, 1000
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i32 %39, i32* %40
%41 = call i64 @select(i32 1, i32* %33, i32* %33, i32* %33, %struct.timeval* %8)
%42 = icmp eq i64 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %18
%44 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %18
%46 = call i32 @dbgprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @dbgprintf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%48 = add i64 %4, -1
%49 = call i32 @read(i32 %14, i8* %3, i64 %48)
%50 = call i32 @close(i32 %14)
%51 = icmp eq i32 %49, -1
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @dbgprintf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0))
br label %57
54: ; preds = %45
%55 = sext i32 %49 to i64
%56 = getelementptr inbounds i8, i8* %3, i64 %55
store i8 0, i8* %56
br label %57
57: ; preds = %54, %52
%58 = phi i8* [ null, %52 ], [ %3, %54 ]
ret i8* %58
}