#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 */
unsigned short atoi(char *);
struct servent *getservbyname(char *, char *);
scalar_t__ isdigit(char);
unsigned short ntohs(int /*<<< orphan*/);
int getservice(char *name, unsigned short *port) {
  struct servent *se;
  if (!name)
    return -1;
  if (isdigit(name[0]))
    *port = atoi(name);
  else {
    if (!(se = getservbyname(name, "tcp")))
      return -1;
    *port = ntohs(se->s_port);
  }
  return 0;
}


x86 assembly (clang, -O0):
<getservice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <getservice+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <getservice+0x9b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1195 <getservice+0x55>
mov    -0x10(%rbp),%rdi
call   1060 <atoi@plt>
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
jmp    11d4 <getservice+0x94>
mov    -0x10(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bf <getservice+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <getservice+0x9b>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservice>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     118c <getservice+0x4c>
mov    %rsi,%r14
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     116b <getservice+0x2b>
mov    %rbx,%rdi
call   1060 <atoi@plt>
jmp    1186 <getservice+0x46>
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     118c <getservice+0x4c>
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %ax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [4 x i8] c"tcp\00"
define i32 @getservice(i8* %0, i16* %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 i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i32 -1, i32* %3
br label %32
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = call i64 @isdigit(i8 signext %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = call zeroext i16 @atoi(i8* %17)
%19 = load i16*, i16** %5
store i16 %18, i16* %19
br label %31
20: ; preds = %10
%21 = load i8*, i8** %4
%22 = call %struct.servent* @getservbyname(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %22, %struct.servent** %6
%23 = icmp ne %struct.servent* %22, null
br i1 %23, label %25, label %24
24: ; preds = %20
store i32 -1, i32* %3
br label %32
25: ; preds = %20
%26 = load %struct.servent*, %struct.servent** %6
%27 = getelementptr inbounds %struct.servent, %struct.servent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call zeroext i16 @ntohs(i32 %28)
%30 = load i16*, i16** %5
store i16 %29, i16* %30
br label %31
31: ; preds = %25, %16
store i32 0, i32* %3
br label %32
32: ; preds = %31, %24, %9
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [4 x i8] c"tcp\00"
define i32 @getservice(i8* %0, i16* nocapture writeonly %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %19, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = tail call i64 @isdigit(i8 signext %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call zeroext i16 @atoi(i8* %0)
br label %17
10: ; preds = %4
%11 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq %struct.servent* %11, null
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.servent, %struct.servent* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call zeroext i16 @ntohs(i32 %15)
br label %17
17: ; preds = %13, %8
%18 = phi i16 [ %16, %13 ], [ %9, %8 ]
store i16 %18, i16* %1
br label %19
19: ; preds = %10, %2, %17
%20 = phi i32 [ 0, %17 ], [ -1, %2 ], [ -1, %10 ]
ret i32 %20
}