#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_addr;
  int /*<<< orphan*/ sin_family;
};
struct servent {
  int /*<<< orphan*/ s_port;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ atoi(char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct hostent *gethostbyname(char const *);
struct servent *getservbyname(char const *, char const *);
int /*<<< orphan*/ herror(char const *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int resolve(const char *host, const char *service, const char *proto,
            struct sockaddr_in *addr) {
  unsigned short port;
  struct hostent *hostentry = NULL;
  struct servent *serventry = NULL;
  extern int h_errno;
  addr->sin_family = AF_INET;
  if (!(hostentry = gethostbyname(host))) {
    herror(host);
    return h_errno;
  }
  memcpy(&addr->sin_addr, hostentry->h_addr, hostentry->h_length);
  port = (unsigned short)atoi(service);
  if (port > 0) {
    addr->sin_port = htons(port);
  } else {
    serventry = getservbyname(service, proto);
    if (serventry == NULL) {
      fprintf(stderr, "Failed to get service port: %s\n", service);
      return 1;
    }
    addr->sin_port = serventry->s_port;
  }
  return 0;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11d9 <resolve+0x69>
mov    -0x10(%rbp),%rdi
call   1080 <herror@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <resolve+0x107>
mov    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
cmp    $0x0,%eax
jle    1223 <resolve+0xb3>
movzwl -0x2a(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1270 <resolve+0x100>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1264 <resolve+0xf4>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdad(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <resolve+0x107>
mov    -0x40(%rbp),%rax
mov    (%rax),%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):
<resolve>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rcx)
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11c0 <resolve+0x50>
lea    0x4(%r12),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1090 <atoi@plt>
test   %ax,%ax
je     11d3 <resolve+0x63>
movzwl %ax,%edi
call   1030 <htons@plt>
jmp    11e5 <resolve+0x75>
mov    %rbx,%rdi
call   1080 <herror@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
jmp    11eb <resolve+0x7b>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     11f7 <resolve+0x87>
mov    (%rax),%eax
mov    %eax,(%r12)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    11eb <resolve+0x7b>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.servent = type { i32 }
@AF_INET = global i32 0
@h_errno = external global i32
@stderr = global i32 0
@.str = [32 x i8] c"Failed to get service port: %s\0A\00"
define i32 @resolve(i8* %0, i8* %1, i8* %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.sockaddr_in*
%10 = alloca i16
%11 = alloca %struct.hostent*
%12 = alloca %struct.servent*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
store %struct.hostent* null, %struct.hostent** %11
store %struct.servent* null, %struct.servent** %12
%13 = load i32, i32* @AF_INET
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %13, i32* %15
%16 = load i8*, i8** %6
%17 = call %struct.hostent* @gethostbyname(i8* %16)
store %struct.hostent* %17, %struct.hostent** %11
%18 = icmp ne %struct.hostent* %17, null
br i1 %18, label %23, label %19
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = call i32 @herror(i8* %20)
%22 = load i32, i32* @h_errno
store i32 %22, i32* %5
br label %61
23: ; preds = %4
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 1
%26 = load %struct.hostent*, %struct.hostent** %11
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load %struct.hostent*, %struct.hostent** %11
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i32* %25, i32 %28, i32 %31)
%33 = load i8*, i8** %7
%34 = call i64 @atoi(i8* %33)
%35 = trunc i64 %34 to i16
store i16 %35, i16* %10
%36 = load i16, i16* %10
%37 = zext i16 %36 to i32
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %23
%40 = load i16, i16* %10
%41 = call i32 @htons(i16 zeroext %40)
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 0
store i32 %41, i32* %43
br label %60
44: ; preds = %23
%45 = load i8*, i8** %7
%46 = load i8*, i8** %8
%47 = call %struct.servent* @getservbyname(i8* %45, i8* %46)
store %struct.servent* %47, %struct.servent** %12
%48 = load %struct.servent*, %struct.servent** %12
%49 = icmp eq %struct.servent* %48, null
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %7
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %52)
store i32 1, i32* %5
br label %61
54: ; preds = %44
%55 = load %struct.servent*, %struct.servent** %12
%56 = getelementptr inbounds %struct.servent, %struct.servent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %58, i32 0, i32 0
store i32 %57, i32* %59
br label %60
60: ; preds = %54, %39
store i32 0, i32* %5
br label %61
61: ; preds = %60, %50, %19
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.servent = type { i32 }
@AF_INET = global i32 0
@h_errno = external global i32
@stderr = global i32 0
@.str = [32 x i8] c"Failed to get service port: %s\0A\00"
define i32 @resolve(i8* %0, i8* %1, i8* %2, %struct.sockaddr_in* %3) {
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @herror(i8* %0)
%11 = load i32, i32* @h_errno
br label %36
12: ; preds = %4
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @memcpy(i32* %13, i32 %15, i32 %17)
%19 = tail call i64 @atoi(i8* %1)
%20 = and i64 %19, 65535
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %12
%23 = trunc i64 %19 to i16
%24 = tail call i32 @htons(i16 zeroext %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %24, i32* %25
br label %36
26: ; preds = %12
%27 = tail call %struct.servent* @getservbyname(i8* %1, i8* %2)
%28 = icmp eq %struct.servent* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %36
32: ; preds = %26
%33 = getelementptr inbounds %struct.servent, %struct.servent* %27, i64 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %34, i32* %35
br label %36
36: ; preds = %22, %32, %29, %9
%37 = phi i32 [ 1, %29 ], [ %11, %9 ], [ 0, %32 ], [ 0, %22 ]
ret i32 %37
}