#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;
struct servent {
  int /*<<< orphan*/ s_port;
};
struct rpcent {
  char const *r_name;
  scalar_t__ r_aliases;
};

/* Variables and functions */
int getrpcbynumber_r(int /*<<< orphan*/, struct rpcent *, char *, int,
                     struct rpcent **);
int getservbyname_r(char const *, char const *, struct servent *, char *, int,
                    struct servent **);
int ntohs(int /*<<< orphan*/);
int getservport(u_long number, const char *proto) {
  char rpcdata[1024], servdata[1024];
  struct rpcent rpcbuf, *rpcp;
  struct servent servbuf, *servp = NULL;
  int ret;
  ret = getrpcbynumber_r(number, &rpcbuf, rpcdata, sizeof rpcdata, &rpcp);
  if (ret == 0 && rpcp != NULL) {
    ret = getservbyname_r(rpcp->r_name, proto, &servbuf, servdata,
                          sizeof servdata, &servp);
    if ((ret != 0 || servp == NULL) && rpcp->r_aliases) {
      const char **a;
      for (a = (const char **)rpcp->r_aliases; *a != NULL; a++) {
        ret = getservbyname_r(*a, proto, &servbuf, servdata, sizeof servdata,
                              &servp);
        if (ret == 0 && servp != NULL)
          break;
      }
    }
  }
  if (ret == 0 && servp != NULL)
    return ntohs(servp->s_port);
  return 0;
}


x86 assembly (clang, -O0):
<getservport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x838(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rdx
lea    -0x820(%rbp),%rsi
mov    $0x400,%ecx
lea    -0x828(%rbp),%r8
call   1030 <getrpcbynumber_r@plt>
mov    %eax,-0x83c(%rbp)
cmpl   $0x0,-0x83c(%rbp)
jne    128e <getservport+0x15e>
cmpq   $0x0,-0x828(%rbp)
je     128e <getservport+0x15e>
mov    -0x828(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x810(%rbp),%rcx
lea    -0x830(%rbp),%rdx
mov    $0x400,%r8d
lea    -0x838(%rbp),%r9
call   1040 <getservbyname_r@plt>
mov    %eax,-0x83c(%rbp)
cmpl   $0x0,-0x83c(%rbp)
jne    11df <getservport+0xaf>
cmpq   $0x0,-0x838(%rbp)
jne    1289 <getservport+0x159>
mov    -0x828(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1289 <getservport+0x159>
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x848(%rbp)
mov    -0x848(%rbp),%rax
cmpq   $0x0,(%rax)
je     1284 <getservport+0x154>
mov    -0x848(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x810(%rbp),%rcx
lea    -0x830(%rbp),%rdx
mov    $0x400,%r8d
lea    -0x838(%rbp),%r9
call   1040 <getservbyname_r@plt>
mov    %eax,-0x83c(%rbp)
cmpl   $0x0,-0x83c(%rbp)
jne    1268 <getservport+0x138>
cmpq   $0x0,-0x838(%rbp)
je     1268 <getservport+0x138>
jmp    1284 <getservport+0x154>
jmp    126d <getservport+0x13d>
mov    -0x848(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x848(%rbp)
jmp    1203 <getservport+0xd3>
jmp    1289 <getservport+0x159>
jmp    128e <getservport+0x15e>
cmpl   $0x0,-0x83c(%rbp)
jne    12bf <getservport+0x18f>
cmpq   $0x0,-0x838(%rbp)
je     12bf <getservport+0x18f>
mov    -0x838(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    12c6 <getservport+0x196>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservport>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x830,%rsp
mov    %rsi,%r13
movq   $0x0,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x430(%rsp),%rdx
lea    0x18(%rsp),%r8
mov    $0x400,%ecx
call   1030 <getrpcbynumber_r@plt>
xor    %ecx,%ecx
test   %eax,%eax
jne    1227 <getservport+0xf7>
mov    0x18(%rsp),%rax
test   %rax,%rax
je     1227 <getservport+0xf7>
mov    (%rax),%rdi
lea    0x10(%rsp),%rdx
lea    0x30(%rsp),%rcx
lea    0x8(%rsp),%r9
mov    %r13,%rsi
mov    $0x400,%r8d
call   1040 <getservbyname_r@plt>
mov    0x8(%rsp),%rdx
test   %eax,%eax
jne    11ae <getservport+0x7e>
test   %rdx,%rdx
jne    1211 <getservport+0xe1>
mov    0x18(%rsp),%rcx
mov    0x8(%rcx),%rbx
test   %rbx,%rbx
je     1213 <getservport+0xe3>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     1213 <getservport+0xe3>
add    $0x8,%rbx
lea    0x10(%rsp),%r14
lea    0x30(%rsp),%r15
lea    0x8(%rsp),%r12
jmp    11ec <getservport+0xbc>
nopl   0x0(%rax)
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
je     1213 <getservport+0xe3>
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    $0x400,%r8d
mov    %r12,%r9
call   1040 <getservbyname_r@plt>
mov    0x8(%rsp),%rdx
test   %eax,%eax
jne    11e0 <getservport+0xb0>
test   %rdx,%rdx
je     11e0 <getservport+0xb0>
xor    %eax,%eax
xor    %ecx,%ecx
test   %eax,%eax
jne    1227 <getservport+0xf7>
test   %rdx,%rdx
je     1227 <getservport+0xf7>
mov    (%rdx),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    %ecx,%eax
add    $0x830,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.rpcent = type { i8*, i64 }
%struct.servent = type { i32 }
define i32 @getservport(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca %struct.rpcent
%9 = alloca %struct.rpcent*
%10 = alloca %struct.servent
%11 = alloca %struct.servent*
%12 = alloca i32
%13 = alloca i8**
store i32 %0, i32* %4
store i8* %1, i8** %5
store %struct.servent* null, %struct.servent** %11
%14 = load i32, i32* %4
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = call i32 @getrpcbynumber_r(i32 %14, %struct.rpcent* %8, i8* %15, i32 1024, %struct.rpcent** %9)
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %66
19: ; preds = %2
%20 = load %struct.rpcent*, %struct.rpcent** %9
%21 = icmp ne %struct.rpcent* %20, null
br i1 %21, label %22, label %66
22: ; preds = %19
%23 = load %struct.rpcent*, %struct.rpcent** %9
%24 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = load i8*, i8** %5
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%28 = call i32 @getservbyname_r(i8* %25, i8* %26, %struct.servent* %10, i8* %27, i32 1024, %struct.servent** %11)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %22
%32 = load %struct.servent*, %struct.servent** %11
%33 = icmp eq %struct.servent* %32, null
br i1 %33, label %34, label %65
34: ; preds = %31, %22
%35 = load %struct.rpcent*, %struct.rpcent** %9
%36 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %35, i32 0, i32 1
%37 = load i64, i64* %36
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %65
39: ; preds = %34
%40 = load %struct.rpcent*, %struct.rpcent** %9
%41 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %40, i32 0, i32 1
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to i8**
store i8** %43, i8*** %13
br label %44
44: ; preds = %61, %39
%45 = load i8**, i8*** %13
%46 = load i8*, i8** %45
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %64
48: ; preds = %44
%49 = load i8**, i8*** %13
%50 = load i8*, i8** %49
%51 = load i8*, i8** %5
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%53 = call i32 @getservbyname_r(i8* %50, i8* %51, %struct.servent* %10, i8* %52, i32 1024, %struct.servent** %11)
store i32 %53, i32* %12
%54 = load i32, i32* %12
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %48
%57 = load %struct.servent*, %struct.servent** %11
%58 = icmp ne %struct.servent* %57, null
br i1 %58, label %59, label %60
59: ; preds = %56
br label %64
60: ; preds = %56, %48
br label %61
61: ; preds = %60
%62 = load i8**, i8*** %13
%63 = getelementptr inbounds i8*, i8** %62, i32 1
store i8** %63, i8*** %13
br label %44
64: ; preds = %59, %44
br label %65
65: ; preds = %64, %34, %31
br label %66
66: ; preds = %65, %19, %2
%67 = load i32, i32* %12
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %77
69: ; preds = %66
%70 = load %struct.servent*, %struct.servent** %11
%71 = icmp ne %struct.servent* %70, null
br i1 %71, label %72, label %77
72: ; preds = %69
%73 = load %struct.servent*, %struct.servent** %11
%74 = getelementptr inbounds %struct.servent, %struct.servent* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @ntohs(i32 %75)
store i32 %76, i32* %3
br label %78
77: ; preds = %69, %66
store i32 0, i32* %3
br label %78
78: ; preds = %77, %72
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.rpcent = type { i8*, i64 }
%struct.servent = type { i32 }
define i32 @getservport(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca %struct.rpcent
%6 = alloca %struct.rpcent*
%7 = alloca %struct.servent
%8 = alloca %struct.servent*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = bitcast %struct.rpcent* %5 to i8*
%12 = bitcast %struct.rpcent** %6 to i8*
%13 = bitcast %struct.servent* %7 to i8*
%14 = bitcast %struct.servent** %8 to i8*
store %struct.servent* null, %struct.servent** %8
%15 = call i32 @getrpcbynumber_r(i32 %0, %struct.rpcent* %5, i8* %9, i32 1024, %struct.rpcent** %6)
%16 = icmp eq i32 %15, 0
%17 = load %struct.rpcent*, %struct.rpcent** %6
%18 = icmp ne %struct.rpcent* %17, null
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %59
20: ; preds = %2
%21 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %17, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @getservbyname_r(i8* %22, i8* %1, %struct.servent* %7, i8* %10, i32 1024, %struct.servent** %8)
%24 = icmp ne i32 %23, 0
%25 = load %struct.servent*, %struct.servent** %8
%26 = icmp eq %struct.servent* %25, null
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %49
28: ; preds = %20
%29 = load %struct.rpcent*, %struct.rpcent** %6
%30 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %29, i64 0, i32 1
%31 = load i64, i64* %30
%32 = icmp eq i64 %31, 0
br i1 %32, label %49, label %33
33: ; preds = %28
%34 = inttoptr i64 %31 to i8**
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %49, label %40
37: ; preds = %40
%38 = load i8*, i8** %48
%39 = icmp eq i8* %38, null
br i1 %39, label %49, label %40
40: ; preds = %33, %37
%41 = phi i8* [ %38, %37 ], [ %35, %33 ]
%42 = phi i8** [ %48, %37 ], [ %34, %33 ]
%43 = call i32 @getservbyname_r(i8* %41, i8* %1, %struct.servent* %7, i8* %10, i32 1024, %struct.servent** %8)
%44 = icmp eq i32 %43, 0
%45 = load %struct.servent*, %struct.servent** %8
%46 = icmp ne %struct.servent* %45, null
%47 = select i1 %44, i1 %46, i1 false
%48 = getelementptr inbounds i8*, i8** %42, i64 1
br i1 %47, label %49, label %37
49: ; preds = %40, %37, %33, %28, %20
%50 = phi %struct.servent* [ %25, %28 ], [ %25, %20 ], [ %25, %33 ], [ %45, %37 ], [ %45, %40 ]
%51 = phi i32 [ %23, %28 ], [ 0, %20 ], [ %23, %33 ], [ 0, %40 ], [ %43, %37 ]
%52 = icmp eq i32 %51, 0
%53 = icmp ne %struct.servent* %50, null
%54 = select i1 %52, i1 %53, i1 false
br i1 %54, label %55, label %59
55: ; preds = %49
%56 = getelementptr inbounds %struct.servent, %struct.servent* %50, i64 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @ntohs(i32 %57)
br label %59
59: ; preds = %2, %49, %55
%60 = phi i32 [ %58, %55 ], [ 0, %49 ], [ 0, %2 ]
ret i32 %60
}