#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 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;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_NODELAY;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fault(char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int connect_host(char *name, unsigned int port) {
  int fd;
  struct hostent *hostent;
  struct sockaddr_in sin;
  int on;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    fault("socket");
  if (!(hostent = gethostbyname(name)))
    fault("gethostbyname: %s: %s\n", name, strerror(errno));
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  memcpy(&sin.sin_addr.s_addr, hostent->h_addr, sizeof(struct in_addr));
  if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    fault("connect: %s:%u: %s\n", name, port, strerror(errno));
  on = 1;
  if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
    syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
  on = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
    syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
  return fd;
}


x86 assembly (clang, -O0):
<connect_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11e2 <connect_host+0x42>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1090 <fault@plt>
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1223 <connect_host+0x83>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1090 <fault@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12ac <connect_host+0x10c>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1090 <fault@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
mov    0x2d1a(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12f6 <connect_host+0x156>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
call   1080 <syslog@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
mov    0x2c90(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1340 <connect_host+0x1a0>
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
lea    0xd14(%rip),%rsi
call   1080 <syslog@plt>
mov    -0x10(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_host>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e1d(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    11e4 <connect_host+0x44>
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1090 <fault@plt>
mov    %r15,%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1215 <connect_host+0x75>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdfe(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fault@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    1286 <connect_host+0xe6>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda7(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <fault@plt>
movl   $0x1,0x4(%rsp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jns    12cd <connect_host+0x12d>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
call   1080 <syslog@plt>
movl   $0x1,0x4(%rsp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%esi
mov    0x2cbb(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jns    1314 <connect_host+0x174>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
call   1080 <syslog@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [23 x i8] c"gethostbyname: %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"connect: %s:%u: %s\0A\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@LOG_WARNING = global i32 0
@.str.3 = [29 x i8] c"setsockopt (TCP_NODELAY): %m\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.4 = [30 x i8] c"setsockopt (SO_KEEPALIVE): %m\00"
define i32 @connect_host(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @fault(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8*, i8** %3
%17 = call %struct.hostent* @gethostbyname(i8* %16)
store %struct.hostent* %17, %struct.hostent** %6
%18 = icmp ne %struct.hostent* %17, null
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 (i8*, ...) @fault(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %22)
br label %24
24: ; preds = %19, %15
%25 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
%33 = load %struct.hostent*, %struct.hostent** %6
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i32* %32, i32 %35, i32 4)
%37 = load i32, i32* %5
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %24
%42 = load i8*, i8** %3
%43 = load i32, i32* %4
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 (i8*, ...) @fault(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %42, i32 %43, i32 %45)
br label %47
47: ; preds = %41, %24
store i32 1, i32* %8
%48 = load i32, i32* %5
%49 = load i32, i32* @IPPROTO_TCP
%50 = load i32, i32* @TCP_NODELAY
%51 = call i64 @setsockopt(i32 %48, i32 %49, i32 %50, i32* %8, i32 4)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i32, i32* @LOG_WARNING
%55 = call i32 @syslog(i32 %54, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %53, %47
store i32 1, i32* %8
%57 = load i32, i32* %5
%58 = load i32, i32* @SOL_SOCKET
%59 = load i32, i32* @SO_KEEPALIVE
%60 = call i64 @setsockopt(i32 %57, i32 %58, i32 %59, i32* %8, i32 4)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56
%63 = load i32, i32* @LOG_WARNING
%64 = call i32 @syslog(i32 %63, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %62, %56
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [23 x i8] c"gethostbyname: %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"connect: %s:%u: %s\0A\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@LOG_WARNING = global i32 0
@.str.3 = [29 x i8] c"setsockopt (TCP_NODELAY): %m\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.4 = [30 x i8] c"setsockopt (SO_KEEPALIVE): %m\00"
define i32 @connect_host(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @fault(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = tail call %struct.hostent* @gethostbyname(i8* %0)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = tail call i32 (i8*, ...) @fault(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %18)
br label %20
20: ; preds = %16, %13
%21 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 %1)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i32* %26, i32 %28, i32 4)
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @connect(i32 %9, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = load i32, i32* @errno
%35 = call i32 @strerror(i32 %34)
%36 = call i32 (i8*, ...) @fault(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1, i32 %35)
br label %37
37: ; preds = %33, %20
store i32 1, i32* %4
%38 = load i32, i32* @IPPROTO_TCP
%39 = load i32, i32* @TCP_NODELAY
%40 = call i64 @setsockopt(i32 %9, i32 %38, i32 %39, i32* %4, i32 4)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* @LOG_WARNING
%44 = call i32 @syslog(i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %42, %37
store i32 1, i32* %4
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_KEEPALIVE
%48 = call i64 @setsockopt(i32 %9, i32 %46, i32 %47, i32* %4, i32 4)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = load i32, i32* @LOG_WARNING
%52 = call i32 @syslog(i32 %51, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %50, %45
ret i32 %9
}