#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 */
typedef int /*<<< orphan*/ uint16_t;
struct servent {
  scalar_t__ s_port;
};

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ endservent();
int /*<<< orphan*/ errx(int, char *, char const *);
struct servent *getservbyname(char const *, char *);
scalar_t__ match(char *, char const *);
int ntohs(int /*<<< orphan*/);
int resolve_port(const char *port) {
  if (match("^[0-9]+$", port))
    return atoi(port);
  struct servent *sv;
  if ((sv = getservbyname(port, "tcp"))) {
    endservent();
    return ntohs((uint16_t)sv->s_port);
  }
  errx(1, "unknown service %s", port);
}


x86 assembly (clang, -O0):
<resolve_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1050 <match@plt>
cmp    $0x0,%rax
je     1197 <resolve_port+0x37>
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    11e7 <resolve_port+0x87>
mov    -0x10(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11d2 <resolve_port+0x72>
mov    $0x0,%al
call   1080 <endservent@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
call   1060 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11e7 <resolve_port+0x87>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe2b(%rip),%rsi
call   1030 <errx@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_port>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
mov    %rbx,%rsi
call   1050 <match@plt>
test   %rax,%rax
je     118a <resolve_port+0x2a>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <atoi@plt>
lea    0xe78(%rip),%rsi
mov    %rbx,%rdi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     11b7 <resolve_port+0x57>
mov    %rax,%r14
xor    %eax,%eax
call   1080 <endservent@plt>
mov    (%r14),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <ntohs@plt>
lea    0xe4f(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <errx@plt>

LLVM IR (-O0):
%struct.servent = type { i64 }
@.str = [9 x i8] c"^[0-9]+$\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [19 x i8] c"unknown service %s\00"
define i32 @resolve_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.servent*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @match(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @atoi(i8* %9)
store i32 %10, i32* %2
br label %25
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call %struct.servent* @getservbyname(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %13, %struct.servent** %4
%14 = icmp ne %struct.servent* %13, null
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = call i32 (...) @endservent()
%17 = load %struct.servent*, %struct.servent** %4
%18 = getelementptr inbounds %struct.servent, %struct.servent* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = call i32 @ntohs(i32 %20)
store i32 %21, i32* %2
br label %25
22: ; preds = %11
%23 = load i8*, i8** %3
%24 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %15, %8
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.servent = type { i64 }
@.str = [9 x i8] c"^[0-9]+$\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [19 x i8] c"unknown service %s\00"
define i32 @resolve_port(i8* %0) {
%2 = tail call i64 @match(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @atoi(i8* %0)
br label %17
6: ; preds = %1
%7 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq %struct.servent* %7, null
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i32 (...) @endservent()
%11 = getelementptr inbounds %struct.servent, %struct.servent* %7, i64 0, i32 0
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 @ntohs(i32 %13)
br label %17
15: ; preds = %6
%16 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %9, %15, %4
%18 = phi i32 [ %5, %4 ], [ %14, %9 ], [ undef, %15 ]
ret i32 %18
}