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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
struct hostent *gethostbyname(char const *);
struct servent *getservbyname(char const *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ inet_aton(char const *, struct in_addr *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
short strtol(char const *, char **, int /*<<< orphan*/);
int set_address(const char *host_name, const char *service_name,
                struct sockaddr_in *address) {
  struct hostent *he;
  struct servent *se;
  char *endptr;
  short port;
  memset(address, 0, sizeof *address);
  address->sin_family = AF_INET;
  if (host_name != NULL) {
    if (!inet_aton(host_name, &address->sin_addr)) {
      he = gethostbyname(host_name);
      if (he == NULL)
        return -1;
      address->sin_addr = *(struct in_addr *)he->h_addr;
    }
  } else
    address->sin_addr.s_addr = htonl(INADDR_ANY);
  port = strtol(service_name, &endptr, 0);
  if (*endptr == 0)
    address->sin_port = htons(port);
  else {
    se = getservbyname(service_name, "tcp");
    if (se == NULL)
      return -1;
    address->sin_port = se->s_port;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1202 <set_address+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%eax
jne    11fd <set_address+0x8d>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ed <set_address+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <set_address+0x120>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rax)
jmp    1219 <set_address+0xa9>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %ax,-0x3a(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1252 <set_address+0xe2>
movswl -0x3a(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1289 <set_address+0x119>
mov    -0x18(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127d <set_address+0x10d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <set_address+0x120>
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_address>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r15)
test   %rbx,%rbx
je     11cd <set_address+0x5d>
lea    0x4(%r15),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <inet_aton@plt>
test   %eax,%eax
jne    11df <set_address+0x6f>
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     121c <set_address+0xac>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%r12)
jmp    11df <set_address+0x6f>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%r15)
mov    %rsp,%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     120d <set_address+0x9d>
lea    0xe04(%rip),%rsi
mov    %r14,%rdi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     121c <set_address+0xac>
mov    (%rax),%eax
jmp    1215 <set_address+0xa5>
movswl %ax,%edi
call   1030 <htons@plt>
mov    %eax,(%r15)
xor    %eax,%eax
jmp    1221 <set_address+0xb1>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.servent = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"tcp\00"
define i32 @set_address(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 %struct.servent*
%10 = alloca i8*
%11 = alloca i16
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%13 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
store i32 %14, i32* %16
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %41
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 1
%23 = call i32 @inet_aton(i8* %20, %struct.in_addr* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %19
%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 %30, label %31
30: ; preds = %25
store i32 -1, i32* %4
br label %72
31: ; preds = %25
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 1
%34 = load %struct.hostent*, %struct.hostent** %8
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to %struct.in_addr*
%38 = bitcast %struct.in_addr* %33 to i8*
%39 = bitcast %struct.in_addr* %37 to i8*
br label %40
40: ; preds = %31, %19
br label %47
41: ; preds = %3
%42 = load i32, i32* @INADDR_ANY
%43 = call i32 @htonl(i32 %42)
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %44, i32 0, i32 1
%46 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %45, i32 0, i32 0
store i32 %43, i32* %46
br label %47
47: ; preds = %41, %40
%48 = load i8*, i8** %6
%49 = call signext i16 @strtol(i8* %48, i8** %10, i32 0)
store i16 %49, i16* %11
%50 = load i8*, i8** %10
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %47
%55 = load i16, i16* %11
%56 = call i32 @htons(i16 signext %55)
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 0
store i32 %56, i32* %58
br label %71
59: ; preds = %47
%60 = load i8*, i8** %6
%61 = call %struct.servent* @getservbyname(i8* %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %61, %struct.servent** %9
%62 = load %struct.servent*, %struct.servent** %9
%63 = icmp eq %struct.servent* %62, null
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 -1, i32* %4
br label %72
65: ; preds = %59
%66 = load %struct.servent*, %struct.servent** %9
%67 = getelementptr inbounds %struct.servent, %struct.servent* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %69, i32 0, i32 0
store i32 %68, i32* %70
br label %71
71: ; preds = %65, %54
store i32 0, i32* %4
br label %72
72: ; preds = %71, %64, %30
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.servent = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"tcp\00"
define i32 @set_address(i8* %0, i8* %1, %struct.sockaddr_in* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = icmp eq i8* %0, null
br i1 %9, label %23, label %10
10: ; preds = %3
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%12 = tail call i32 @inet_aton(i8* %0, %struct.in_addr* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = tail call %struct.hostent* @gethostbyname(i8* %0)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %43, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i32*
%21 = getelementptr %struct.in_addr, %struct.in_addr* %11, i64 0, i32 0
%22 = load i32, i32* %20
store i32 %22, i32* %21
br label %27
23: ; preds = %3
%24 = load i32, i32* @INADDR_ANY
%25 = tail call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %25, i32* %26
br label %27
27: ; preds = %10, %17, %23
%28 = call signext i16 @strtol(i8* %1, i8** %4, i32 0)
%29 = load i8*, i8** %4
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = tail call i32 @htons(i16 signext %28)
br label %40
34: ; preds = %27
%35 = tail call %struct.servent* @getservbyname(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq %struct.servent* %35, null
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = getelementptr inbounds %struct.servent, %struct.servent* %35, i64 0, i32 0
%39 = load i32, i32* %38
br label %40
40: ; preds = %37, %32
%41 = phi i32 [ %33, %32 ], [ %39, %37 ]
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %41, i32* %42
br label %43
43: ; preds = %40, %34, %14
%44 = phi i32 [ -1, %14 ], [ -1, %34 ], [ 0, %40 ]
ret i32 %44
}