#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int htxcmd_connect_sut(int socket_fd, char *sut_ip, int htxd_port) {
  int result;
  struct sockaddr_in sut_address;
  struct hostent *p_sut_entry;
  char error_string[512];
  p_sut_entry = gethostbyname(sut_ip);
  if (p_sut_entry == NULL) {
    sprintf(error_string,
            "ERROR: while calling gethostbyname with hostname <%s>. Exiting...",
            sut_ip);
    herror(error_string);
    exit(1);
  }
  sut_address.sin_family = AF_INET;
  sut_address.sin_port = htons(htxd_port);
  sut_address.sin_addr = *((struct in_addr *)(p_sut_entry->h_addr));
  memset(&(sut_address.sin_zero), '\0', 8);
  result = connect(socket_fd, (struct sockaddr *)&sut_address,
                   sizeof(struct sockaddr));
  if (result == -1) {
    sprintf(error_string,
            "ERROR: while connecting hostname <%s> and port <%d>. Exiting...",
            sut_ip, htxd_port);
    perror(error_string);
    exit(1);
  }
  return result;
}


x86 assembly (clang, -O0):
<htxcmd_connect_sut>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dc <htxcmd_connect_sut+0x5c>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x230(%rbp),%rdi
call   1060 <herror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    125f <htxcmd_connect_sut+0xdf>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x230(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htxcmd_connect_sut>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebx
mov    %rsi,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11fc <htxcmd_connect_sut+0x7c>
mov    %rax,%rbp
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1227 <htxcmd_connect_sut+0xa7>
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfd(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <herror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe14(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %r15d,%ecx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [66 x i8] c"ERROR: while calling gethostbyname with hostname <%s>. Exiting...\00"
@AF_INET = global i32 0
@.str.1 = [64 x i8] c"ERROR: while connecting hostname <%s> and port <%d>. Exiting...\00"
define i32 @htxcmd_connect_sut(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
%10 = alloca [512 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %9
%13 = load %struct.hostent*, %struct.hostent** %9
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %22
15: ; preds = %3
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%20 = call i32 @herror(i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 3
store i32 %23, i32* %24
%25 = load i32, i32* %6
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%29 = load %struct.hostent*, %struct.hostent** %9
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr*
%33 = bitcast %struct.in_addr* %28 to i8*
%34 = bitcast %struct.in_addr* %32 to i8*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%36 = call i32 @memset(i32* %35, i8 signext 0, i32 8)
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 4)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %50
42: ; preds = %22
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%44 = load i8*, i8** %5
%45 = load i32, i32* %6
%46 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %43, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i8* %44, i32 %45)
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%48 = call i32 @perror(i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %22
%51 = load i32, i32* %7
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [66 x i8] c"ERROR: while calling gethostbyname with hostname <%s>. Exiting...\00"
@AF_INET = global i32 0
@.str.1 = [64 x i8] c"ERROR: while connecting hostname <%s> and port <%d>. Exiting...\00"
define i32 @htxcmd_connect_sut(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca [512 x i8]
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = tail call %struct.hostent* @gethostbyname(i8* %1)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8* %1)
%12 = call i32 @herror(i8* %7)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 %2)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to i32*
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%23 = load i32, i32* %21
store i32 %23, i32* %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%25 = call i32 @memset(i32* %24, i8 signext 0, i32 8)
%26 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%27 = call i32 @connect(i32 %0, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %14
%30 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %2)
%31 = call i32 @perror(i8* %7)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %14
ret i32 %27
}