#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 servent {
  char *s_name;
};

/* Variables and functions */
unsigned short IPPROTO_TCP;
unsigned short IPPROTO_UDP;
struct servent *getservbyport(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(unsigned int);
char *port_to_service(unsigned int port, unsigned short proto) {
  struct servent *service;
  if (proto == IPPROTO_TCP &&
      (service = getservbyport(htons(port), "tcp")) != NULL)
    return service->s_name;
  else if (proto == IPPROTO_UDP &&
           (service = getservbyport(htons(port), "udp")) != NULL)
    return service->s_name;
  else
    return (char *)NULL;
}


x86 assembly (clang, -O0):
<port_to_service>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0xc(%rbp)
mov    %ax,-0xe(%rbp)
movzwl -0xe(%rbp),%eax
mov    0x2eab(%rip),%rcx
movzwl (%rcx),%ecx
cmp    %ecx,%eax
jne    117c <port_to_service+0x5c>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%edi
lea    0xea7(%rip),%rsi
call   1040 <getservbyport@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     117c <port_to_service+0x5c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <port_to_service+0xae>
movzwl -0xe(%rbp),%eax
mov    0x2e49(%rip),%rcx
movzwl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c6 <port_to_service+0xa6>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%edi
lea    0xe61(%rip),%rsi
call   1040 <getservbyport@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11c6 <port_to_service+0xa6>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <port_to_service+0xae>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<port_to_service>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eba(%rip),%rax
cmp    %bp,(%rax)
jne    114d <port_to_service+0x2d>
mov    %ebx,%edi
call   1030 <htons@plt>
lea    0xebf(%rip),%rsi
mov    %eax,%edi
call   1040 <getservbyport@plt>
test   %rax,%rax
jne    1173 <port_to_service+0x53>
mov    0x2e7c(%rip),%rax
cmp    %bp,(%rax)
jne    1178 <port_to_service+0x58>
mov    %ebx,%edi
call   1030 <htons@plt>
lea    0xe9d(%rip),%rsi
mov    %eax,%edi
call   1040 <getservbyport@plt>
test   %rax,%rax
je     1178 <port_to_service+0x58>
mov    (%rax),%rax
jmp    117a <port_to_service+0x5a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i8* }
@IPPROTO_TCP = global i16 0
@.str = [4 x i8] c"tcp\00"
@IPPROTO_UDP = global i16 0
@.str.1 = [4 x i8] c"udp\00"
define i8* @port_to_service(i32 %0, i16 zeroext %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i16
%6 = alloca %struct.servent*
store i32 %0, i32* %4
store i16 %1, i16* %5
%7 = load i16, i16* %5
%8 = zext i16 %7 to i32
%9 = load i16, i16* @IPPROTO_TCP
%10 = zext i16 %9 to i32
%11 = icmp eq i32 %8, %10
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 @htons(i32 %13)
%15 = call %struct.servent* @getservbyport(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %15, %struct.servent** %6
%16 = icmp ne %struct.servent* %15, null
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load %struct.servent*, %struct.servent** %6
%19 = getelementptr inbounds %struct.servent, %struct.servent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %3
br label %37
21: ; preds = %12, %2
%22 = load i16, i16* %5
%23 = zext i16 %22 to i32
%24 = load i16, i16* @IPPROTO_UDP
%25 = zext i16 %24 to i32
%26 = icmp eq i32 %23, %25
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
%30 = call %struct.servent* @getservbyport(i32 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %30, %struct.servent** %6
%31 = icmp ne %struct.servent* %30, null
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load %struct.servent*, %struct.servent** %6
%34 = getelementptr inbounds %struct.servent, %struct.servent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
store i8* %35, i8** %3
br label %37
36: ; preds = %27, %21
store i8* null, i8** %3
br label %37
37: ; preds = %36, %32, %17
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
%struct.servent = type { i8* }
@IPPROTO_TCP = global i16 0
@.str = [4 x i8] c"tcp\00"
@IPPROTO_UDP = global i16 0
@.str.1 = [4 x i8] c"udp\00"
define i8* @port_to_service(i32 %0, i16 zeroext %1) {
%3 = load i16, i16* @IPPROTO_TCP
%4 = icmp eq i16 %3, %1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @htons(i32 %0)
%7 = tail call %struct.servent* @getservbyport(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq %struct.servent* %7, null
br i1 %8, label %9, label %16
9: ; preds = %5, %2
%10 = load i16, i16* @IPPROTO_UDP
%11 = icmp eq i16 %10, %1
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = tail call i32 @htons(i32 %0)
%14 = tail call %struct.servent* @getservbyport(i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq %struct.servent* %14, null
br i1 %15, label %20, label %16
16: ; preds = %12, %5
%17 = phi %struct.servent* [ %7, %5 ], [ %14, %12 ]
%18 = getelementptr inbounds %struct.servent, %struct.servent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
br label %20
20: ; preds = %16, %9, %12
%21 = phi i8* [ null, %12 ], [ null, %9 ], [ %19, %16 ]
ret i8* %21
}