#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 *, int /*<<< orphan*/ *);
int /*<<< orphan*/ logwarn(char *, char const *, int);
int ntohs(int /*<<< orphan*/);
int strtol(char const *, char **, int);
int resolve_service(const char *service, int defport) {
  struct servent *sse;
  char *endptr;
  int portnum = defport, tmpport;
  sse = getservbyname(service, NULL);
  if (sse) {
    portnum = ntohs(sse->s_port);
  } else {
    tmpport = strtol(service, &endptr, 10);
    if (endptr > service)
      portnum = tmpport;
    else
      logwarn("Could not resolve service '%s', defaulting to %d\n", service,
              portnum);
  }
  return portnum;
}


x86 assembly (clang, -O0):
<resolve_service>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1184 <resolve_service+0x44>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x24(%rbp)
jmp    11ca <resolve_service+0x8a>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11b2 <resolve_service+0x72>
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11c5 <resolve_service+0x85>
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe40(%rip),%rdi
call   1030 <logwarn@plt>
jmp    11ca <resolve_service+0x8a>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_service>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %esi,%esi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     1161 <resolve_service+0x21>
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <ntohs@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    %rbx,(%rsp)
jbe    117b <resolve_service+0x3b>
mov    %eax,%ebp
jmp    118c <resolve_service+0x4c>
lea    0xe7e(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <logwarn@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [50 x i8] c"Could not resolve service '%s', defaulting to %d\0A\00"
define i32 @resolve_service(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.servent*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
store i32 %9, i32* %7
%10 = load i8*, i8** %3
%11 = call %struct.servent* @getservbyname(i8* %10, i32* null)
store %struct.servent* %11, %struct.servent** %5
%12 = load %struct.servent*, %struct.servent** %5
%13 = icmp ne %struct.servent* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load %struct.servent*, %struct.servent** %5
%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* %7
br label %32
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = call i32 @strtol(i8* %20, i8** %6, i32 10)
store i32 %21, i32* %8
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = icmp ugt i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* %8
store i32 %26, i32* %7
br label %31
27: ; preds = %19
%28 = load i8*, i8** %3
%29 = load i32, i32* %7
%30 = call i32 @logwarn(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %28, i32 %29)
br label %31
31: ; preds = %27, %25
br label %32
32: ; preds = %31, %14
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [50 x i8] c"Could not resolve service '%s', defaulting to %d\0A\00"
define i32 @resolve_service(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call %struct.servent* @getservbyname(i8* %0, i32* null)
%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 10)
%13 = load i8*, i8** %3
%14 = icmp ugt i8* %13, %0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @logwarn(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
br label %17
17: ; preds = %11, %15, %7
%18 = phi i32 [ %10, %7 ], [ %1, %15 ], [ %12, %11 ]
ret i32 %18
}