#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_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct in_addr {
  unsigned long s_addr;
};
struct hostent {
  unsigned long *h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, struct in_addr *);
scalar_t__ isdigit(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned long *, int);
int get_sockaddr(void *vp_sock_addr, char *host, int port) {
  struct sockaddr_in *sock_addr = (struct sockaddr_in *)vp_sock_addr;
  unsigned long host_ip_address;
  struct in_addr in_addr;
  if (isdigit((int)*host)) {
    if (inet_aton(host, &in_addr) == 0)
      return -1;
    host_ip_address = in_addr.s_addr;
    memcpy(&sock_addr->sin_addr, &host_ip_address, sizeof(sock_addr->sin_addr));
  } else {
    struct hostent *host_name;
    host_name = gethostbyname(host);
    if (!host_name)
      return -1;
    memcpy(&sock_addr->sin_addr, host_name->h_addr,
           sizeof(sock_addr->sin_addr));
  }
  sock_addr->sin_family = AF_INET;
  sock_addr->sin_port = htons(port);
  return 0;
}


x86 assembly (clang, -O0):
<get_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11c7 <get_sockaddr+0x77>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    11a4 <get_sockaddr+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <get_sockaddr+0xdb>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x8,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
jmp    1204 <get_sockaddr+0xb4>
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11eb <get_sockaddr+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <get_sockaddr+0xdb>
mov    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sockaddr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
movsbl (%rsi),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1192 <get_sockaddr+0x42>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
je     11ca <get_sockaddr+0x7a>
mov    (%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
jmp    11a6 <get_sockaddr+0x56>
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11ca <get_sockaddr+0x7a>
lea    0x8(%rbx),%rdi
mov    (%rax),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    11cf <get_sockaddr+0x7f>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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