#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 {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_NONE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ message(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int service_connect(const char *host_name, const int port) {
  struct sockaddr_in addr;
  struct hostent *host_p;
  int fd;
  int opt = 1;
  (void)memset((char *)&addr, 0, sizeof(addr));
  if ((addr.sin_addr.s_addr = inet_addr((char *)host_name)) == INADDR_NONE) {
    host_p = gethostbyname(host_name);
    if (!host_p) {
      message(0, "%s: host not found\n", host_name);
      return (-1);
    }
    (void)memcpy((char *)(&addr.sin_addr), host_p->h_addr, host_p->h_length);
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons((unsigned short)port);
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return (-1);
  }
  (void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(opt));
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    (void)close(fd);
    perror(host_name);
    return (-1);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<service_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    0x2def(%rip),%rcx
cmp    (%rcx),%eax
jne    1241 <service_connect+0x91>
mov    -0x10(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1227 <service_connect+0x77>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xdea(%rip),%rsi
call   1030 <message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <service_connect+0x14a>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
movzwl %ax,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1298 <service_connect+0xe8>
lea    0xd8d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <service_connect+0x14a>
mov    -0x2c(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
je     12f4 <service_connect+0x144>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <service_connect+0x14a>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<service_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%eax
jne    120b <service_connect+0x5b>
mov    %r14,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     127f <service_connect+0xcf>
lea    0x8(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   10a0 <memcpy@plt>
mov    0x2dce(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
movzwl %bp,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1292 <service_connect+0xe2>
mov    %eax,%ebp
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
je     12a3 <service_connect+0xf3>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14,%rdi
jmp    1299 <service_connect+0xe9>
lea    0xd7a(%rip),%rsi
xor    %edi,%edi
mov    %r14,%rdx
call   1030 <message@plt>
jmp    129e <service_connect+0xee>
lea    0xd7b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@INADDR_NONE = global i32 0
@.str = [20 x i8] c"%s: host not found\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @service_connect(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*
store i32 1, i32* %4
%7 = call i32 @memset(i8* %5, i32 0, i32 12)
%8 = call i32 @inet_addr(i8* %0)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i64 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @INADDR_NONE
%12 = icmp eq i32 %8, %11
br i1 %12, label %13, label %25
13: ; preds = %2
%14 = call %struct.hostent* @gethostbyname(i8* %0)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @message(i32 0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %47
18: ; preds = %13
%19 = bitcast %struct.TYPE_2__* %9 to i8*
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i8* %19, i32 %21, i32 %23)
br label %25
25: ; preds = %18, %2
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %26, i32* %27
%28 = trunc i32 %1 to i16
%29 = call i32 @htons(i16 zeroext %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %47
37: ; preds = %25
%38 = load i32, i32* @IPPROTO_TCP
%39 = load i32, i32* @TCP_NODELAY
%40 = call i32 @setsockopt(i32 %33, i32 %38, i32 %39, i8* %6, i32 4)
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i64 @connect(i32 %33, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %37
%45 = call i32 @close(i32 %33)
%46 = call i32 @perror(i8* %0)
br label %47
47: ; preds = %37, %44, %35, %16
%48 = phi i32 [ -1, %35 ], [ -1, %44 ], [ -1, %16 ], [ %33, %37 ]
ret i32 %48
}