#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;
};
struct servent {
  char *s_name;
  char **s_aliases;
  char *s_proto;
  int /*<<< orphan*/ s_port;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ strcmp(char const *, char *);
int getservbyname_r(const char *name, const char *prots, struct servent *se,
                    char *buf, size_t buflen, struct servent **res) {
  struct addrinfo *ai, hint = {.ai_family = AF_INET};
  int i;
  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"))
    hint.ai_protocol = IPPROTO_TCP;
  else if (!strcmp(prots, "udp"))
    hint.ai_protocol = IPPROTO_UDP;
  else
    return -1;
  if (getaddrinfo(0, name, &hint, &ai) < 0)
    return -1;
  se->s_name = (char *)name;
  se->s_aliases = (void *)buf;
  se->s_aliases[0] = se->s_name;
  se->s_aliases[1] = 0;
  se->s_port = ((struct sockaddr_in *)ai->ai_addr)->sin_port;
  se->s_proto = (char *)prots;
  freeaddrinfo(ai);
  *res = se;
  return 0;
}


x86 assembly (clang, -O0):
<getservbyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jne    1187 <getservbyname_r+0x57>
movl   $0x8,-0x54(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x54(%rbp),%rdx
mov    $0x18,%ecx
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    11be <getservbyname_r+0x8e>
mov    0x2e31(%rip),%rax
mov    (%rax),%ecx
mov    0x2e18(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <getservbyname_r+0x1b7>
movslq -0x54(%rbp),%rcx
mov    $0x8,%eax
sub    %rcx,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
movslq -0x54(%rbp),%rax
mov    $0x8,%ecx
sub    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1213 <getservbyname_r+0xe3>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
jmp    124e <getservbyname_r+0x11e>
mov    -0x18(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    123d <getservbyname_r+0x10d>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
jmp    1249 <getservbyname_r+0x119>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <getservbyname_r+0x1b7>
jmp    124e <getservbyname_r+0x11e>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
lea    -0x50(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1040 <getaddrinfo@plt>
cmp    $0x0,%rax
jge    1277 <getservbyname_r+0x147>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <getservbyname_r+0x1b7>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x8(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x40(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservbyname_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
movq   $0x0,0x10(%rsp)
mov    %ebx,%eax
and    $0x7,%eax
mov    $0x8,%r14d
cmovne %eax,%r14d
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x18,%eax
sub    %r14,%rax
cmp    %r8,%rax
jbe    118f <getservbyname_r+0x5f>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    0x2e45(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    123d <getservbyname_r+0x10d>
mov    %r9,%rbp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
lea    0xe5e(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
mov    %rbp,0x8(%rsp)
je     11d4 <getservbyname_r+0xa4>
lea    0xe4a(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcmp@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    123d <getservbyname_r+0x10d>
mov    0x2de6(%rip),%rax
jmp    11db <getservbyname_r+0xab>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xor    %ebp,%ebp
lea    0x10(%rsp),%rdx
lea    0x20(%rsp),%rcx
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <getaddrinfo@plt>
test   %rax,%rax
js     1185 <getservbyname_r+0x55>
sub    %r14,%rbx
add    $0x8,%rbx
mov    %r13,(%r12)
mov    %rbx,0x8(%r12)
mov    %r13,(%rbx)
mov    0x8(%r12),%rax
movq   $0x0,0x8(%rax)
mov    0x20(%rsp),%rdi
mov    (%rdi),%rax
mov    (%rax),%eax
mov    %eax,0x18(%r12)
mov    %r15,0x10(%r12)
call   1050 <freeaddrinfo@plt>
mov    0x8(%rsp),%rax
mov    %r12,(%rax)
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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