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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ERANGE;
int NETDB_INTERNAL;
char *__alloca(size_t);
scalar_t__ __gethostbyname_r(char const *, struct hostent *, char *, size_t,
                             struct hostent **, int *);
scalar_t__ errno;
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int pmap_getport(struct sockaddr_in *, int /*<<< orphan*/, int /*<<< orphan*/,
                 int /*<<< orphan*/);
int getrpcport(const char *host, u_long prognum, u_long versnum, u_int proto) {
  struct sockaddr_in addr;
  struct hostent hostbuf, *hp;
  size_t buflen;
  char *buffer;
  int herr;
  buflen = 1024;
  buffer = __alloca(buflen);
  while (__gethostbyname_r(host, &hostbuf, buffer, buflen, &hp, &herr) != 0 ||
         hp == NULL)
    if (herr != NETDB_INTERNAL || errno != ERANGE)
      return 0;
    else {
      buflen *= 2;
      buffer = __alloca(buflen);
    }
  memcpy((char *)&addr.sin_addr, hp->h_addr, hp->h_length);
  addr.sin_family = AF_INET;
  addr.sin_port = 0;
  return pmap_getport(&addr, prognum, versnum, proto);
}


x86 assembly (clang, -O0):
<getrpcport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x400,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <__alloca@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%r8
lea    -0x54(%rbp),%r9
call   1030 <__gethostbyname_r@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x55(%rbp)
jne    11a4 <getrpcport+0x64>
cmpq   $0x0,-0x40(%rbp)
sete   %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    11b4 <getrpcport+0x74>
jmp    120a <getrpcport+0xca>
mov    -0x54(%rbp),%eax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%eax
jne    11e0 <getrpcport+0xa0>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
je     11ec <getrpcport+0xac>
movl   $0x0,-0x4(%rbp)
jmp    124d <getrpcport+0x10d>
mov    -0x48(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <__alloca@plt>
mov    %rax,-0x50(%rbp)
jmp    116a <getrpcport+0x2a>
lea    -0x30(%rbp),%rdi
add    $0xc,%rdi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    -0x30(%rbp),%rdi
call   1040 <pmap_getport@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrpcport>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,0x1c(%rsp)
mov    %edx,0x18(%rsp)
mov    %esi,0x14(%rsp)
mov    %rdi,%rbp
mov    $0x400,%ebx
mov    $0x400,%edi
call   1060 <__alloca@plt>
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    $0x400,%ecx
mov    %rbp,%rdi
mov    %rax,%rdx
call   1030 <__gethostbyname_r@plt>
test   %rax,%rax
jne    119a <getrpcport+0x5a>
mov    0x8(%rsp),%rax
test   %rax,%rax
jne    120f <getrpcport+0xcf>
mov    0x2e37(%rip),%r12
mov    0x2e38(%rip),%r13
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x4(%rsp),%ecx
xor    %eax,%eax
cmp    (%r12),%ecx
jne    124a <getrpcport+0x10a>
mov    0x2df5(%rip),%rcx
mov    (%rcx),%rcx
cmp    0x0(%r13),%rcx
jne    124a <getrpcport+0x10a>
add    %rbx,%rbx
mov    %rbx,%rdi
call   1060 <__alloca@plt>
mov    %rbp,%rdi
lea    0x20(%rsp),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    %r14,%r8
mov    %r15,%r9
call   1030 <__gethostbyname_r@plt>
test   %rax,%rax
jne    11c0 <getrpcport+0x80>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11c0 <getrpcport+0x80>
lea    0x34(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1050 <memcpy@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x28(%rsp),%rdi
mov    0x14(%rsp),%esi
mov    0x18(%rsp),%edx
mov    0x1c(%rsp),%ecx
call   1040 <pmap_getport@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.hostent = type { i32, i32 }
@NETDB_INTERNAL = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@AF_INET = global i32 0
define i32 @getrpcport(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent
%12 = alloca %struct.hostent*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i64 1024, i64* %13
%16 = load i64, i64* %13
%17 = call i8* @__alloca(i64 %16)
store i8* %17, i8** %14
br label %18
18: ; preds = %43, %4
%19 = load i8*, i8** %6
%20 = load i8*, i8** %14
%21 = load i64, i64* %13
%22 = call i64 @__gethostbyname_r(i8* %19, %struct.hostent* %11, i8* %20, i64 %21, %struct.hostent** %12, i32* %15)
%23 = icmp ne i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = load %struct.hostent*, %struct.hostent** %12
%26 = icmp eq %struct.hostent* %25, null
br label %27
27: ; preds = %24, %18
%28 = phi i1 [ true, %18 ], [ %26, %24 ]
br i1 %28, label %29, label %44
29: ; preds = %27
%30 = load i32, i32* %15
%31 = load i32, i32* @NETDB_INTERNAL
%32 = icmp ne i32 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @ERANGE
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %29
store i32 0, i32* %5
br label %61
38: ; preds = %33
%39 = load i64, i64* %13
%40 = mul i64 %39, 2
store i64 %40, i64* %13
%41 = load i64, i64* %13
%42 = call i8* @__alloca(i64 %41)
store i8* %42, i8** %14
br label %43
43: ; preds = %38
br label %18
44: ; preds = %27
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
%46 = bitcast i32* %45 to i8*
%47 = load %struct.hostent*, %struct.hostent** %12
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.hostent*, %struct.hostent** %12
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @memcpy(i8* %46, i32 %49, i32 %52)
%54 = load i32, i32* @AF_INET
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %54, i32* %55
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i64 0, i64* %56
%57 = load i32, i32* %7
%58 = load i32, i32* %8
%59 = load i32, i32* %9
%60 = call i32 @pmap_getport(%struct.sockaddr_in* %10, i32 %57, i32 %58, i32 %59)
store i32 %60, i32* %5
br label %61
61: ; preds = %44, %37
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.hostent = type { i32, i32 }
@NETDB_INTERNAL = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@AF_INET = global i32 0
define i32 @getrpcport(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.hostent
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.hostent* %6 to i8*
%11 = bitcast %struct.hostent** %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = tail call i8* @__alloca(i64 1024)
%14 = call i64 @__gethostbyname_r(i8* %0, %struct.hostent* %6, i8* %13, i64 1024, %struct.hostent** %7, i32* %8)
%15 = icmp ne i64 %14, 0
%16 = load %struct.hostent*, %struct.hostent** %7
%17 = icmp eq %struct.hostent* %16, null
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %36
19: ; preds = %4, %28
%20 = phi i64 [ %29, %28 ], [ 1024, %4 ]
%21 = load i32, i32* %8
%22 = load i32, i32* @NETDB_INTERNAL
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @ERANGE
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %49
28: ; preds = %24
%29 = shl i64 %20, 1
%30 = call i8* @__alloca(i64 %29)
%31 = call i64 @__gethostbyname_r(i8* %0, %struct.hostent* %6, i8* %30, i64 %29, %struct.hostent** %7, i32* %8)
%32 = icmp ne i64 %31, 0
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = icmp eq %struct.hostent* %33, null
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %19, label %36
36: ; preds = %28, %4
%37 = phi %struct.hostent* [ %16, %4 ], [ %33, %28 ]
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
%39 = bitcast i32* %38 to i8*
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i64 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @memcpy(i8* %39, i32 %41, i32 %43)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i64 0, i64* %47
%48 = call i32 @pmap_getport(%struct.sockaddr_in* %5, i32 %1, i32 %2, i32 %3)
br label %49
49: ; preds = %19, %24, %36
%50 = phi i32 [ %48, %36 ], [ 0, %24 ], [ 0, %19 ]
ret i32 %50
}