#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 sin_port;
  int /*<<< orphan*/ sin_family;
};
struct servent {
  int s_port;
  char *s_proto;
  char **s_aliases;
  char *s_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ NI_DGRAM;
int /*<<< orphan*/ errno;
scalar_t__ getnameinfo(void *, int, int /*<<< orphan*/, int /*<<< orphan*/,
                       char *, size_t, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
int getservbyport_r(int port, const char *prots, struct servent *se, char *buf,
                    size_t buflen, struct servent **res) {
  int i;
  struct sockaddr_in sin = {
      .sin_family = AF_INET,
      .sin_port = port,
  };
  i = (uintptr_t)buf & sizeof(char *) - 1;
  if (!i)
    i = sizeof(char *);
  if (buflen < 3 * sizeof(char *) - i) {
    errno = ERANGE;
    return -1;
  }
  buf += sizeof(char *) - i;
  buflen -= sizeof(char *) - i;
  if (strcmp(prots, "tcp") && strcmp(prots, "udp"))
    return -1;
  se->s_port = port;
  se->s_proto = (char *)prots;
  se->s_aliases = (void *)buf;
  buf += 2 * sizeof(char *);
  buflen -= 2 * sizeof(char *);
  se->s_aliases[1] = 0;
  se->s_aliases[0] = se->s_name = buf;
  if (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen,
                  strcmp(prots, "udp") ? 0 : NI_DGRAM) < 0)
    return -1;
  *res = se;
  return 0;
}


x86 assembly (clang, -O0):
<getservbyport_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    116d <getservbyport_r+0x4d>
movl   $0x8,-0x34(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rdx
mov    $0x18,%ecx
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    11a4 <getservbyport_r+0x84>
mov    0x2e53(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <getservbyport_r+0x1df>
movslq -0x34(%rbp),%rcx
mov    $0x8,%eax
sub    %rcx,%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
movslq -0x34(%rbp),%rax
mov    $0x8,%ecx
sub    %rax,%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     120f <getservbyport_r+0xef>
mov    -0x10(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     120f <getservbyport_r+0xef>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <getservbyport_r+0x1df>
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
movq   $0x0,0x8(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rcx,(%rax)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd72(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     12ab <getservbyport_r+0x18b>
xor    %eax,%eax
mov    %eax,-0x5c(%rbp)
jmp    12b7 <getservbyport_r+0x197>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x48(%rbp),%r9
mov    -0x50(%rbp),%r8
mov    -0x58(%rbp),%rdi
mov    -0x5c(%rbp),%eax
mov    $0x8,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
mov    %eax,(%rsp)
call   1030 <getnameinfo@plt>
cmp    $0x0,%rax
jge    12ed <getservbyport_r+0x1cd>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <getservbyport_r+0x1df>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservbyport_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %edi,0x8(%rsp)
mov    %ebx,%eax
and    $0x7,%eax
mov    $0x8,%r14d
cmovne %eax,%r14d
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x18,%eax
sub    %r14,%rax
cmp    %r8,%rax
jbe    117a <getservbyport_r+0x5a>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1240 <getservbyport_r+0x120>
mov    %r8,%r13
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
mov    %r9,0x10(%rsp)
lea    0xe6f(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11be <getservbyport_r+0x9e>
lea    0xe5f(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1240 <getservbyport_r+0x120>
sub    %r14,%rbx
add    $0x8,%rbx
mov    %ebp,(%r15)
mov    %r12,0x8(%r15)
mov    %rbx,0x10(%r15)
lea    (%r14,%r13,1),%rbp
add    $0xffffffffffffffe8,%rbp
movq   $0x0,0x8(%rbx)
add    $0x10,%rbx
mov    %rbx,0x18(%r15)
mov    0x10(%r15),%rax
mov    %rbx,(%rax)
lea    0xe0e(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcmp@plt>
mov    $0x0,%ecx
test   %rax,%rax
jne    1211 <getservbyport_r+0xf1>
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    %rbp,%r9
call   1030 <getnameinfo@plt>
test   %rax,%rax
js     1170 <getservbyport_r+0x50>
mov    0x10(%rsp),%rax
mov    %r15,(%rax)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32, i8*, i8**, i8* }
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@NI_DGRAM = global i32 0
define i32 @getservbyport_r(i32 %0, i8* %1, %struct.servent* %2, i8* %3, i64 %4, %struct.servent** %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.servent*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.servent**
%14 = alloca i32
%15 = alloca %struct.sockaddr_in
store i32 %0, i32* %8
store i8* %1, i8** %9
store %struct.servent* %2, %struct.servent** %10
store i8* %3, i8** %11
store i64 %4, i64* %12
store %struct.servent** %5, %struct.servent*** %13
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
%17 = load i32, i32* %8
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%19 = load i32, i32* @AF_INET
store i32 %19, i32* %18
%20 = load i8*, i8** %11
%21 = ptrtoint i8* %20 to i64
%22 = and i64 %21, 7
%23 = trunc i64 %22 to i32
store i32 %23, i32* %14
%24 = load i32, i32* %14
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %6
store i32 8, i32* %14
br label %27
27: ; preds = %26, %6
%28 = load i64, i64* %12
%29 = load i32, i32* %14
%30 = sext i32 %29 to i64
%31 = sub i64 24, %30
%32 = icmp ult i64 %28, %31
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i32, i32* @ERANGE
store i32 %34, i32* @errno
store i32 -1, i32* %7
br label %97
35: ; preds = %27
%36 = load i32, i32* %14
%37 = sext i32 %36 to i64
%38 = sub i64 8, %37
%39 = load i8*, i8** %11
%40 = getelementptr inbounds i8, i8* %39, i64 %38
store i8* %40, i8** %11
%41 = load i32, i32* %14
%42 = sext i32 %41 to i64
%43 = sub i64 8, %42
%44 = load i64, i64* %12
%45 = sub i64 %44, %43
store i64 %45, i64* %12
%46 = load i8*, i8** %9
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %35
%50 = load i8*, i8** %9
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 -1, i32* %7
br label %97
54: ; preds = %49, %35
%55 = load i32, i32* %8
%56 = load %struct.servent*, %struct.servent** %10
%57 = getelementptr inbounds %struct.servent, %struct.servent* %56, i32 0, i32 0
store i32 %55, i32* %57
%58 = load i8*, i8** %9
%59 = load %struct.servent*, %struct.servent** %10
%60 = getelementptr inbounds %struct.servent, %struct.servent* %59, i32 0, i32 1
store i8* %58, i8** %60
%61 = load i8*, i8** %11
%62 = bitcast i8* %61 to i8**
%63 = load %struct.servent*, %struct.servent** %10
%64 = getelementptr inbounds %struct.servent, %struct.servent* %63, i32 0, i32 2
store i8** %62, i8*** %64
%65 = load i8*, i8** %11
%66 = getelementptr inbounds i8, i8* %65, i64 16
store i8* %66, i8** %11
%67 = load i64, i64* %12
%68 = sub i64 %67, 16
store i64 %68, i64* %12
%69 = load %struct.servent*, %struct.servent** %10
%70 = getelementptr inbounds %struct.servent, %struct.servent* %69, i32 0, i32 2
%71 = load i8**, i8*** %70
%72 = getelementptr inbounds i8*, i8** %71, i64 1
store i8* null, i8** %72
%73 = load i8*, i8** %11
%74 = load %struct.servent*, %struct.servent** %10
%75 = getelementptr inbounds %struct.servent, %struct.servent* %74, i32 0, i32 3
store i8* %73, i8** %75
%76 = load %struct.servent*, %struct.servent** %10
%77 = getelementptr inbounds %struct.servent, %struct.servent* %76, i32 0, i32 2
%78 = load i8**, i8*** %77
%79 = getelementptr inbounds i8*, i8** %78, i64 0
store i8* %73, i8** %79
%80 = bitcast %struct.sockaddr_in* %15 to i8*
%81 = load i8*, i8** %11
%82 = load i64, i64* %12
%83 = load i8*, i8** %9
%84 = call i64 @strcmp(i8* %83, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %54
br label %89
87: ; preds = %54
%88 = load i32, i32* @NI_DGRAM
br label %89
89: ; preds = %87, %86
%90 = phi i32 [ 0, %86 ], [ %88, %87 ]
%91 = call i64 @getnameinfo(i8* %80, i32 8, i32 0, i32 0, i8* %81, i64 %82, i32 %90)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %89
store i32 -1, i32* %7
br label %97
94: ; preds = %89
%95 = load %struct.servent*, %struct.servent** %10
%96 = load %struct.servent**, %struct.servent*** %13
store %struct.servent* %95, %struct.servent** %96
store i32 0, i32* %7
br label %97
97: ; preds = %94, %93, %53, %33
%98 = load i32, i32* %7
ret i32 %98
}

LLVM IR (-O2):
%struct.servent = type { i32, i8*, i8**, i8* }
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@NI_DGRAM = global i32 0
define i32 @getservbyport_r(i32 %0, i8* %1, %struct.servent* %2, i8* %3, i64 %4, %struct.servent** nocapture writeonly %5) {
%7 = alloca %struct.sockaddr_in
%8 = bitcast %struct.sockaddr_in* %7 to i8*
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i32 %0, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1
%11 = load i32, i32* @AF_INET
store i32 %11, i32* %10
%12 = ptrtoint i8* %3 to i64
%13 = trunc i64 %12 to i32
%14 = and i32 %13, 7
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i32 8, i32 %14
%17 = zext i32 %16 to i64
%18 = sub nuw nsw i64 24, %17
%19 = icmp ugt i64 %18, %4
br i1 %19, label %20, label %22
20: ; preds = %6
%21 = load i32, i32* @ERANGE
store i32 %21, i32* @errno
br label %49
22: ; preds = %6
%23 = sub nuw nsw i64 8, %17
%24 = getelementptr inbounds i8, i8* %3, i64 %23
%25 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %49
30: ; preds = %27, %22
%31 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 0
store i32 %0, i32* %31
%32 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 1
store i8* %1, i8** %32
%33 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 2
%34 = bitcast i8*** %33 to i8**
store i8* %24, i8** %34
%35 = getelementptr inbounds i8, i8* %24, i64 16
%36 = add i64 %17, %4
%37 = add i64 %36, -24
%38 = getelementptr inbounds i8, i8* %24, i64 8
%39 = bitcast i8* %38 to i8**
store i8* null, i8** %39
%40 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 3
store i8* %35, i8** %40
%41 = load i8**, i8*** %33
store i8* %35, i8** %41
%42 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
%44 = load i32, i32* @NI_DGRAM
%45 = select i1 %43, i32 %44, i32 0
%46 = call i64 @getnameinfo(i8* %8, i32 8, i32 0, i32 0, i8* %35, i64 %37, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %30
store %struct.servent* %2, %struct.servent** %5
br label %49
49: ; preds = %30, %27, %48, %20
%50 = phi i32 [ -1, %20 ], [ 0, %48 ], [ -1, %27 ], [ -1, %30 ]
ret i32 %50
}