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

/* Variables and functions */
struct servent *getservbyname(char *, char *);
int htons(int);
int strtol(char *, char **, int);
int _service_port(char *name, char *proto) {
  struct servent *se;
  char *p;
  int port;
  if ((se = getservbyname(name, proto)) != NULL)
    return se->s_port;
  port = strtol(name, &p, 10);
  if (*name == '\0' || *p != '\0')
    return -1;
  return htons(port);
}


x86 assembly (clang, -O0):
<_service_port>:
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)
cmp    $0x0,%rax
je     1169 <_service_port+0x39>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <_service_port+0x85>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119e <_service_port+0x6e>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11aa <_service_port+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <_service_port+0x85>
mov    -0x2c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_service_port>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <getservbyname@plt>
test   %rax,%rax
je     114a <_service_port+0x1a>
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%edi
mov    $0xffffffff,%eax
cmpb   $0x0,(%rbx)
je     1144 <_service_port+0x14>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1144 <_service_port+0x14>
add    $0x10,%rsp
pop    %rbx
jmp    1030 <htons@plt>

LLVM IR (-O0):
%struct.servent = type { i32 }
define i32 @_service_port(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 = icmp ne %struct.servent* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load %struct.servent*, %struct.servent** %6
%15 = getelementptr inbounds %struct.servent, %struct.servent* %14, i32 0, i32 0
%16 = load i32, i32* %15
store i32 %16, i32* %3
br label %33
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @strtol(i8* %18, i8** %7, i32 10)
store i32 %19, i32* %8
%20 = load i8*, i8** %4
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = load i8*, i8** %7
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24, %17
store i32 -1, i32* %3
br label %33
30: ; preds = %24
%31 = load i32, i32* %8
%32 = call i32 @htons(i32 %31)
store i32 %32, i32* %3
br label %33
33: ; preds = %30, %29, %13
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.servent = type { i32 }
define i32 @_service_port(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 %10, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.servent, %struct.servent* %5, i64 0, i32 0
%9 = load i32, i32* %8
br label %20
10: ; preds = %2
%11 = call i32 @strtol(i8* %0, i8** %3, i32 10)
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @htons(i32 %11)
br label %20
20: ; preds = %10, %14, %18, %7
%21 = phi i32 [ %9, %7 ], [ %19, %18 ], [ -1, %14 ], [ -1, %10 ]
ret i32 %21
}