#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 s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  scalar_t__ sin_family;
};
struct hostent {
  scalar_t__ h_addrtype;
  int h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned int);
int inet_addr(char *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int chitcp_addr_construct(char *host, char *port, struct sockaddr_in *addr) {
  struct hostent *he;
  unsigned iport;
  memset(addr, 0, sizeof(struct sockaddr_in));
  addr->sin_family = AF_INET;
  addr->sin_addr.s_addr = inet_addr(host);
  if (addr->sin_addr.s_addr == -1) {
    he = gethostbyname(host);
    if (he == NULL || he->h_addrtype != AF_INET || he->h_length != 4) {
      perror("Invalid host");
      return -1;
    }
    memcpy((char *)&addr->sin_addr, he->h_addr, sizeof(addr->sin_addr));
  }
  sscanf(port, "%u", &iport);
  addr->sin_port = htons(iport);
  return 0;
}


x86 assembly (clang, -O0):
<chitcp_addr_construct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0xffffffff,0x4(%rax)
jne    1234 <chitcp_addr_construct+0xc4>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1203 <chitcp_addr_construct+0x93>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <chitcp_addr_construct+0x93>
mov    -0x28(%rbp),%rax
cmpl   $0x4,0x8(%rax)
je     121b <chitcp_addr_construct+0xab>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <chitcp_addr_construct+0xef>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xdce(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x2c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chitcp_addr_construct>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %r14d,%r14d
mov    %rdx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e4a(%rip),%r12
mov    (%r12),%rax
mov    %rax,0x8(%rbx)
mov    %rbp,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x4(%rbx)
cmp    $0xffffffff,%eax
jne    11e3 <chitcp_addr_construct+0x73>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1212 <chitcp_addr_construct+0xa2>
mov    (%rax),%rcx
cmp    (%r12),%rcx
jne    1212 <chitcp_addr_construct+0xa2>
cmpl   $0x4,0x8(%rax)
jne    1212 <chitcp_addr_construct+0xa2>
lea    0x4(%rbx),%rdi
mov    0xc(%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    0xe23(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
call   1080 <sscanf@plt>
mov    0xc(%rsp),%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1202 <chitcp_addr_construct+0x92>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i64 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i32, i32 }
@AF_INET = global i64 0
@.str = [13 x i8] c"Invalid host\00"
@.str.1 = [3 x i8] c"%u\00"
define i32 @chitcp_addr_construct(i8* %0, i8* %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.hostent*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%10 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%11 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 16)
%12 = load i64, i64* @AF_INET
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i64 %12, i64* %14
%15 = load i8*, i8** %5
%16 = call i32 @inet_addr(i8* %15)
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %16, i32* %19
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %51
25: ; preds = %3
%26 = load i8*, i8** %5
%27 = call %struct.hostent* @gethostbyname(i8* %26)
store %struct.hostent* %27, %struct.hostent** %8
%28 = load %struct.hostent*, %struct.hostent** %8
%29 = icmp eq %struct.hostent* %28, null
br i1 %29, label %41, label %30
30: ; preds = %25
%31 = load %struct.hostent*, %struct.hostent** %8
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @AF_INET
%35 = icmp ne i64 %33, %34
br i1 %35, label %41, label %36
36: ; preds = %30
%37 = load %struct.hostent*, %struct.hostent** %8
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = icmp ne i32 %39, 4
br i1 %40, label %41, label %43
41: ; preds = %36, %30, %25
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %58
43: ; preds = %36
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %44, i32 0, i32 1
%46 = bitcast %struct.TYPE_2__* %45 to i8*
%47 = load %struct.hostent*, %struct.hostent** %8
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = call i32 @memcpy(i8* %46, i32 %49, i32 4)
br label %51
51: ; preds = %43, %3
%52 = load i8*, i8** %6
%53 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%54 = load i32, i32* %9
%55 = call i32 @htons(i32 %54)
%56 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i32 0, i32 0
store i32 %55, i32* %57
store i32 0, i32* %4
br label %58
58: ; preds = %51, %41
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i64 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i32, i32 }
@AF_INET = global i64 0
@.str = [13 x i8] c"Invalid host\00"
@.str.1 = [3 x i8] c"%u\00"
define i32 @chitcp_addr_construct(i8* %0, i8* nocapture %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 16)
%7 = load i64, i64* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i64 %7, i64* %8
%9 = tail call i32 @inet_addr(i8* %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i64 0, i32 0
store i32 %9, i32* %11
%12 = icmp eq i32 %9, -1
br i1 %12, label %13, label %32
13: ; preds = %3
%14 = tail call %struct.hostent* @gethostbyname(i8* %0)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %25, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @AF_INET
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 4
br i1 %24, label %27, label %25
25: ; preds = %21, %16, %13
%26 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %37
27: ; preds = %21
%28 = bitcast %struct.TYPE_2__* %10 to i8*
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 2
%30 = load i32, i32* %29
%31 = tail call i32 @memcpy(i8* %28, i32 %30, i32 4)
br label %32
32: ; preds = %27, %3
%33 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%34 = load i32, i32* %4
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %35, i32* %36
br label %37
37: ; preds = %32, %25
%38 = phi i32 [ -1, %25 ], [ 0, %32 ]
ret i32 %38
}