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

/* Variables and functions */
scalar_t__ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int fill_sockaddr(char *hostname, int port, struct sockaddr_in *new_sa) {
  struct hostent *host_ptr;
  if (port < 1) {
    return -2;
  }
  new_sa->sin_family = PF_INET;
  new_sa->sin_port = htons((short)port);
  if (hostname != NULL && strlen(hostname) > 0) {
    host_ptr = gethostbyname(hostname);
    if (!host_ptr) {
      return -3;
    }
    memcpy((char *)&new_sa->sin_addr.s_addr, (char *)host_ptr->h_addr_list[0],
           host_ptr->h_length);
  } else {
    new_sa->sin_addr.s_addr = INADDR_ANY;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fill_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1169 <fill_sockaddr+0x29>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1206 <fill_sockaddr+0xc6>
mov    0x2e68(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x14(%rbp),%eax
movswl %ax,%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11ee <fill_sockaddr+0xae>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jle    11ee <fill_sockaddr+0xae>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cf <fill_sockaddr+0x8f>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1206 <fill_sockaddr+0xc6>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
jmp    11ff <fill_sockaddr+0xbf>
mov    0x2df3(%rip),%rax
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_sockaddr>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    11a9 <fill_sockaddr+0x69>
mov    %rdx,%r14
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rdx)
movswl %si,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%r14)
test   %rbx,%rbx
je     1198 <fill_sockaddr+0x58>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
jle    1198 <fill_sockaddr+0x58>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11b0 <fill_sockaddr+0x70>
mov    0x8(%rax),%rcx
mov    (%rcx),%rsi
mov    (%rax),%edx
mov    %r14,%rdi
call   1060 <memcpy@plt>
jmp    11a5 <fill_sockaddr+0x65>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
xor    %eax,%eax
jmp    11b5 <fill_sockaddr+0x75>
mov    $0xfffffffe,%eax
jmp    11b5 <fill_sockaddr+0x75>
mov    $0xfffffffd,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i32, i64* }
@PF_INET = global i32 0
@INADDR_ANY = global i64 0
define i32 @fill_sockaddr(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = icmp slt i32 %1, 1
br i1 %4, label %30, label %5
5: ; preds = %3
%6 = load i32, i32* @PF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = trunc i32 %1 to i16
%9 = tail call i32 @htons(i16 signext %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = icmp eq i8* %0, null
br i1 %11, label %27, label %12
12: ; preds = %5
%13 = tail call i64 @strlen(i8* %0)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = tail call %struct.hostent* @gethostbyname(i8* %0)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %30, label %18
18: ; preds = %15
%19 = bitcast %struct.sockaddr_in* %2 to i8*
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 1
%21 = load i64*, i64** %20
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%25 = load i32, i32* %24
%26 = tail call i32 @memcpy(i8* %19, i8* %23, i32 %25)
br label %30
27: ; preds = %12, %5
%28 = load i64, i64* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i64 %28, i64* %29
br label %30
30: ; preds = %18, %27, %15, %3
%31 = phi i32 [ -2, %3 ], [ -3, %15 ], [ 0, %27 ], [ 0, %18 ]
ret i32 %31
}