#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 {
  scalar_t__ s_port;
};

/* Variables and functions */
unsigned short IPPROTO_TCP;
unsigned short IPPROTO_UDP;
struct servent *getservbyname(char const *, char *);
int ntohs(unsigned short);
int service_to_port(const char *name, unsigned short proto) {
  struct servent *service;
  if (proto == IPPROTO_TCP && (service = getservbyname(name, "tcp")) != NULL)
    return ntohs((unsigned short)service->s_port);
  else if (proto == IPPROTO_UDP &&
           (service = getservbyname(name, "udp")) != NULL)
    return ntohs((unsigned short)service->s_port);
  else
    return -1;
}


x86 assembly (clang, -O0):
<service_to_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%eax
mov    0x2eaa(%rip),%rcx
movzwl (%rcx),%ecx
cmp    %ecx,%eax
jne    117e <service_to_port+0x5e>
mov    -0x10(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1030 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     117e <service_to_port+0x5e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movzwl %ax,%edi
call   1040 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11d0 <service_to_port+0xb0>
movzwl -0x12(%rbp),%eax
mov    0x2e47(%rip),%rcx
movzwl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c9 <service_to_port+0xa9>
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1030 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c9 <service_to_port+0xa9>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movzwl %ax,%edi
call   1040 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11d0 <service_to_port+0xb0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<service_to_port>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2eb9(%rip),%rax
cmp    %bp,(%rax)
jne    1148 <service_to_port+0x28>
lea    0xec5(%rip),%rsi
mov    %rbx,%rdi
call   1030 <getservbyname@plt>
test   %rax,%rax
jne    1168 <service_to_port+0x48>
mov    0x2e81(%rip),%rax
cmp    %bp,(%rax)
jne    1176 <service_to_port+0x56>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
call   1030 <getservbyname@plt>
test   %rax,%rax
je     1176 <service_to_port+0x56>
movzwl (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <ntohs@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i64 }
@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 i32 @service_to_port(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.servent*
store i8* %0, i8** %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 %22
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call %struct.servent* @getservbyname(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %14, %struct.servent** %6
%15 = icmp ne %struct.servent* %14, null
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load %struct.servent*, %struct.servent** %6
%18 = getelementptr inbounds %struct.servent, %struct.servent* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i16
%21 = call i32 @ntohs(i16 zeroext %20)
store i32 %21, i32* %3
br label %39
22: ; preds = %12, %2
%23 = load i16, i16* %5
%24 = zext i16 %23 to i32
%25 = load i16, i16* @IPPROTO_UDP
%26 = zext i16 %25 to i32
%27 = icmp eq i32 %24, %26
br i1 %27, label %28, label %38
28: ; preds = %22
%29 = load i8*, i8** %4
%30 = call %struct.servent* @getservbyname(i8* %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 %38
32: ; preds = %28
%33 = load %struct.servent*, %struct.servent** %6
%34 = getelementptr inbounds %struct.servent, %struct.servent* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i16
%37 = call i32 @ntohs(i16 zeroext %36)
store i32 %37, i32* %3
br label %39
38: ; preds = %28, %22
store i32 -1, i32* %3
br label %39
39: ; preds = %38, %32, %16
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.servent = type { i64 }
@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 i32 @service_to_port(i8* %0, i16 zeroext %1) {
%3 = load i16, i16* @IPPROTO_TCP
%4 = icmp eq i16 %3, %1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq %struct.servent* %6, null
br i1 %7, label %8, label %14
8: ; preds = %5, %2
%9 = load i16, i16* @IPPROTO_UDP
%10 = icmp eq i16 %9, %1
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq %struct.servent* %12, null
br i1 %13, label %20, label %14
14: ; preds = %11, %5
%15 = phi %struct.servent* [ %6, %5 ], [ %12, %11 ]
%16 = getelementptr inbounds %struct.servent, %struct.servent* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i16
%19 = tail call i32 @ntohs(i16 zeroext %18)
br label %20
20: ; preds = %14, %8, %11
%21 = phi i32 [ -1, %11 ], [ -1, %8 ], [ %19, %14 ]
ret i32 %21
}