#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 {
  int /*<<< orphan*/ s_port;
};

/* Variables and functions */
struct servent *getservbyname(char const *, char const *);
int /*<<< orphan*/ log_Printf(int, char *, char const *);
int ntohs(int /*<<< orphan*/);
int strtol(char const *, char **, int /*<<< orphan*/);
int ParsePort(const char *service, const char *proto) {
  struct servent *servent;
  char *cp;
  int port;
  servent = getservbyname(service, proto);
  if (servent != 0)
    return ntohs(servent->s_port);
  port = strtol(service, &cp, 0);
  if (cp == service) {
    log_Printf((23), "ParsePort: %s is not a port name or number.\n", service);
    return 0;
  }
  return port;
}


x86 assembly (clang, -O0):
<ParsePort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117f <ParsePort+0x3f>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11c6 <ParsePort+0x86>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c0 <ParsePort+0x80>
mov    -0x10(%rbp),%rdx
mov    $0x17,%edi
lea    0xe51(%rip),%rsi
call   1030 <log_Printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c6 <ParsePort+0x86>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ParsePort>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <getservbyname@plt>
test   %rax,%rax
je     115d <ParsePort+0x1d>
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <ntohs@plt>
mov    %rsp,%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <strtol@plt>
cmp    %rbx,(%rsp)
je     1176 <ParsePort+0x36>
mov    %eax,%ebp
jmp    118a <ParsePort+0x4a>
lea    0xe83(%rip),%rsi
mov    $0x17,%edi
mov    %rbx,%rdx
call   1030 <log_Printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [45 x i8] c"ParsePort: %s is not a port name or number.\0A\00"
define i32 @ParsePort(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.servent*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i8*, i8** %5
%11 = call %struct.servent* @getservbyname(i8* %9, i8* %10)
store %struct.servent* %11, %struct.servent** %6
%12 = load %struct.servent*, %struct.servent** %6
%13 = icmp ne %struct.servent* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load %struct.servent*, %struct.servent** %6
%16 = getelementptr inbounds %struct.servent, %struct.servent* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @ntohs(i32 %17)
store i32 %18, i32* %3
br label %30
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = call i32 @strtol(i8* %20, i8** %7, i32 0)
store i32 %21, i32* %8
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = call i32 @log_Printf(i32 23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %26)
store i32 0, i32* %3
br label %30
28: ; preds = %19
%29 = load i32, i32* %8
store i32 %29, i32* %3
br label %30
30: ; preds = %28, %25, %14
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [45 x i8] c"ParsePort: %s is not a port name or number.\0A\00"
define i32 @ParsePort(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call %struct.servent* @getservbyname(i8* %0, i8* %1)
%6 = icmp eq %struct.servent* %5, null
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.servent, %struct.servent* %5, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @ntohs(i32 %9)
br label %17
11: ; preds = %2
%12 = call i32 @strtol(i8* %0, i8** %3, i32 0)
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, %0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @log_Printf(i32 23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %11, %15, %7
%18 = phi i32 [ %10, %7 ], [ 0, %15 ], [ %12, %11 ]
ret i32 %18
}