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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int fillsockaddrStr(struct sockaddr_in *sockaddr, const char *IPAddrStr,
                    int portN) {
  struct hostent *phe;
  memset(sockaddr, 0, sizeof(struct sockaddr_in));
  sockaddr->sin_family = AF_INET;
  sockaddr->sin_port = htons(portN);
  if ((phe = gethostbyname(IPAddrStr)) != 0)
    memcpy(&sockaddr->sin_addr, phe->h_addr, phe->h_length);
  else if (inet_aton(IPAddrStr, &(sockaddr->sin_addr)) == 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<fillsockaddrStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11c6 <fillsockaddrStr+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
jmp    11ee <fillsockaddrStr+0x9e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    11e9 <fillsockaddrStr+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <fillsockaddrStr+0xa5>
jmp    11ee <fillsockaddrStr+0x9e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fillsockaddrStr>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     119a <fillsockaddrStr+0x4a>
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %rbx,%rdi
call   1060 <memcpy@plt>
jmp    11aa <fillsockaddrStr+0x5a>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <inet_aton@plt>
test   %rax,%rax
je     11b1 <fillsockaddrStr+0x61>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    11ac <fillsockaddrStr+0x5c>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
@AF_INET = global i32 0
define i32 @fillsockaddrStr(%struct.sockaddr_in* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.hostent*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%10 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %11, i32* %13
%14 = load i32, i32* %7
%15 = call i32 @htons(i32 %14)
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load i8*, i8** %6
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %8
%20 = icmp ne %struct.hostent* %19, null
br i1 %20, label %21, label %31
21: ; preds = %3
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 0
%24 = load %struct.hostent*, %struct.hostent** %8
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @memcpy(i32* %23, i32 %26, i32 %29)
br label %39
31: ; preds = %3
%32 = load i8*, i8** %6
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = call i64 @inet_aton(i8* %32, i32* %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 -1, i32* %4
br label %40
38: ; preds = %31
br label %39
39: ; preds = %38, %21
store i32 0, i32* %4
br label %40
40: ; preds = %39, %37
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
@AF_INET = global i32 0
define i32 @fillsockaddrStr(%struct.sockaddr_in* %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(%struct.sockaddr_in* %0, i32 0, i32 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 %2)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
store i32 %7, i32* %8
%9 = tail call %struct.hostent* @gethostbyname(i8* %1)
%10 = icmp eq %struct.hostent* %9, null
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
br i1 %10, label %18, label %12
12: ; preds = %3
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @memcpy(i32* %11, i32 %14, i32 %16)
br label %21
18: ; preds = %3
%19 = tail call i64 @inet_aton(i8* %1, i32* %11)
%20 = icmp eq i64 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %18, %12
br label %22
22: ; preds = %18, %21
%23 = phi i32 [ 0, %21 ], [ -1, %18 ]
ret i32 %23
}