#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 {
  scalar_t__ s_name;
};
typedef scalar_t__ socklen_t;

/* Variables and functions */
int EAI_OVERFLOW;
int NI_DGRAM;
int NI_NUMERICSERV;
struct servent *getservbyport(int /*<<< orphan*/, char *);
unsigned short ntohs(int /*<<< orphan*/);
scalar_t__ snprintf(char *, scalar_t__, char *, unsigned short);
int /*<<< orphan*/ strcpy(char *, scalar_t__);
scalar_t__ strlen(scalar_t__);
int getnameinfo_get_serv(const struct sockaddr_in *sockaddr, char *service,
                         socklen_t servicelen, int flags) {
  struct servent *servent;
  unsigned short port;
  if ((flags & NI_NUMERICSERV) != NI_NUMERICSERV) {
    servent = getservbyport(sockaddr->sin_port,
                            ((flags & NI_DGRAM) == NI_DGRAM) ? "udp" : "tcp");
    if (servent && servent->s_name) {
      if (strlen(servent->s_name) >= servicelen)
        return EAI_OVERFLOW;
      strcpy(service, servent->s_name);
      return 0;
    }
  }
  port = ntohs(sockaddr->sin_port);
  if (snprintf(service, servicelen, "%u", port) >= servicelen)
    return EAI_OVERFLOW;
  return 0;
}


x86 assembly (clang, -O0):
<getnameinfo_get_serv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e77(%rip),%rcx
and    (%rcx),%eax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%eax
je     121b <getnameinfo_get_serv+0xcb>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%ecx
mov    0x2e36(%rip),%rax
and    (%rax),%ecx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
lea    0xe60(%rip),%rsi
lea    0xe55(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
call   1070 <getservbyport@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1216 <getnameinfo_get_serv+0xc6>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     1216 <getnameinfo_get_serv+0xc6>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
cmp    -0x20(%rbp),%rax
jl     11fa <getnameinfo_get_serv+0xaa>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <getnameinfo_get_serv+0x114>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1264 <getnameinfo_get_serv+0x114>
jmp    121b <getnameinfo_get_serv+0xcb>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %ax,-0x32(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xdcf(%rip),%rdx
movzwl -0x32(%rbp),%ecx
call   1050 <snprintf@plt>
cmp    -0x20(%rbp),%rax
jl     125d <getnameinfo_get_serv+0x10d>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <getnameinfo_get_serv+0x114>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnameinfo_get_serv>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e80(%rip),%rax
not    %ecx
test   %ecx,(%rax)
je     11ba <getnameinfo_get_serv+0x6a>
mov    (%rbx),%edi
mov    0x2e51(%rip),%rax
and    (%rax),%ecx
lea    0xe80(%rip),%rax
lea    0xe7d(%rip),%rsi
cmove  %rax,%rsi
call   1070 <getservbyport@plt>
test   %rax,%rax
je     11ba <getnameinfo_get_serv+0x6a>
mov    %rax,%r12
mov    (%rax),%rdi
test   %rdi,%rdi
je     11ba <getnameinfo_get_serv+0x6a>
call   1040 <strlen@plt>
cmp    %r15,%rax
jge    11e0 <getnameinfo_get_serv+0x90>
mov    (%r12),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    11e9 <getnameinfo_get_serv+0x99>
mov    (%rbx),%edi
call   1060 <ntohs@plt>
movzwl %ax,%ecx
lea    0xe3d(%rip),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <snprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r15,%rcx
jl     11e9 <getnameinfo_get_serv+0x99>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.servent = type { i64 }
@NI_NUMERICSERV = global i32 0
@NI_DGRAM = global i32 0
@.str = [4 x i8] c"udp\00"
@.str.1 = [4 x i8] c"tcp\00"
@EAI_OVERFLOW = global i32 0
@.str.2 = [3 x i8] c"%u\00"
define i32 @getnameinfo_get_serv(%struct.sockaddr_in* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.servent*
%11 = alloca i16
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = load i32, i32* @NI_NUMERICSERV
%14 = and i32 %12, %13
%15 = load i32, i32* @NI_NUMERICSERV
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %52
17: ; preds = %4
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* %9
%22 = load i32, i32* @NI_DGRAM
%23 = and i32 %21, %22
%24 = load i32, i32* @NI_DGRAM
%25 = icmp eq i32 %23, %24
%26 = zext i1 %25 to i64
%27 = select i1 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%28 = call %struct.servent* @getservbyport(i32 %20, i8* %27)
store %struct.servent* %28, %struct.servent** %10
%29 = load %struct.servent*, %struct.servent** %10
%30 = icmp ne %struct.servent* %29, null
br i1 %30, label %31, label %51
31: ; preds = %17
%32 = load %struct.servent*, %struct.servent** %10
%33 = getelementptr inbounds %struct.servent, %struct.servent* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %31
%37 = load %struct.servent*, %struct.servent** %10
%38 = getelementptr inbounds %struct.servent, %struct.servent* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = call i64 @strlen(i64 %39)
%41 = load i64, i64* %8
%42 = icmp sge i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = load i32, i32* @EAI_OVERFLOW
store i32 %44, i32* %5
br label %66
45: ; preds = %36
%46 = load i8*, i8** %7
%47 = load %struct.servent*, %struct.servent** %10
%48 = getelementptr inbounds %struct.servent, %struct.servent* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = call i32 @strcpy(i8* %46, i64 %49)
store i32 0, i32* %5
br label %66
51: ; preds = %31, %17
br label %52
52: ; preds = %51, %4
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call zeroext i16 @ntohs(i32 %55)
store i16 %56, i16* %11
%57 = load i8*, i8** %7
%58 = load i64, i64* %8
%59 = load i16, i16* %11
%60 = call i64 @snprintf(i8* %57, i64 %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %59)
%61 = load i64, i64* %8
%62 = icmp sge i64 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %52
%64 = load i32, i32* @EAI_OVERFLOW
store i32 %64, i32* %5
br label %66
65: ; preds = %52
store i32 0, i32* %5
br label %66
66: ; preds = %65, %63, %45, %43
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.servent = type { i64 }
@NI_NUMERICSERV = global i32 0
@NI_DGRAM = global i32 0
@.str = [4 x i8] c"udp\00"
@.str.1 = [4 x i8] c"tcp\00"
@EAI_OVERFLOW = global i32 0
@.str.2 = [3 x i8] c"%u\00"
define i32 @getnameinfo_get_serv(%struct.sockaddr_in* nocapture %0, i8* %1, i64 %2, i32 %3) {
%5 = load i32, i32* @NI_NUMERICSERV
%6 = and i32 %5, %3
%7 = icmp eq i32 %6, %5
br i1 %7, label %29, label %8
8: ; preds = %4
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @NI_DGRAM
%12 = and i32 %11, %3
%13 = icmp eq i32 %12, %11
%14 = select i1 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%15 = tail call %struct.servent* @getservbyport(i32 %10, i8* %14)
%16 = icmp eq %struct.servent* %15, null
br i1 %16, label %29, label %17
17: ; preds = %8
%18 = getelementptr inbounds %struct.servent, %struct.servent* %15, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = tail call i64 @strlen(i64 %19)
%23 = icmp slt i64 %22, %2
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = load i32, i32* @EAI_OVERFLOW
br label %37
26: ; preds = %21
%27 = load i64, i64* %18
%28 = tail call i32 @strcpy(i8* %1, i64 %27)
br label %37
29: ; preds = %8, %17, %4
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call zeroext i16 @ntohs(i32 %31)
%33 = tail call i64 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %32)
%34 = icmp slt i64 %33, %2
%35 = load i32, i32* @EAI_OVERFLOW
%36 = select i1 %34, i32 0, i32 %35
br label %37
37: ; preds = %29, %26, %24
%38 = phi i32 [ %25, %24 ], [ 0, %26 ], [ %36, %29 ]
ret i32 %38
}