#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 int atol(char *);
struct servent *getservbyname(char *, char *);
scalar_t__ isdigit(char);
unsigned int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printerror(int, char *, char *, char *);
unsigned int getportnum(char *name) {
  unsigned int port;
  struct servent *portdesc;
  if (isdigit(*name) != 0)
    port = atol(name);
  else {
    portdesc = getservbyname(name, "tcp");
    if (portdesc == NULL)
      printerror(1, "-ERR", "service not found: %s", name);
    port = ntohs(portdesc->s_port);
    if (port == 0)
      printerror(1, "-ERR", "port error: %s", name);
  }
  return (port);
}


x86 assembly (clang, -O0):
<getportnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     1183 <getportnum+0x33>
mov    -0x8(%rbp),%rdi
call   1070 <atol@plt>
mov    %eax,-0xc(%rbp)
jmp    11f7 <getportnum+0xa7>
mov    -0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11be <getportnum+0x6e>
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xe52(%rip),%rsi
lea    0xe50(%rip),%rdx
call   1030 <printerror@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11f2 <getportnum+0xa2>
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xe1e(%rip),%rsi
lea    0xe32(%rip),%rdx
call   1030 <printerror@plt>
jmp    11f7 <getportnum+0xa7>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getportnum>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     1173 <getportnum+0x23>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <atol@plt>
lea    0xe86(%rip),%rsi
mov    %rbx,%rdi
call   1050 <getservbyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a5 <getportnum+0x55>
lea    0xe73(%rip),%rsi
lea    0xe71(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
call   1030 <printerror@plt>
mov    (%r14),%edi
call   1060 <ntohs@plt>
test   %eax,%eax
jne    11ce <getportnum+0x7e>
lea    0xe4c(%rip),%rsi
lea    0xe60(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
call   1030 <printerror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [4 x i8] c"tcp\00"
@.str.1 = [5 x i8] c"-ERR\00"
@.str.2 = [22 x i8] c"service not found: %s\00"
@.str.3 = [15 x i8] c"port error: %s\00"
define i32 @getportnum(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.servent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = call i64 @isdigit(i8 signext %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @atol(i8* %10)
store i32 %11, i32* %3
br label %31
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call %struct.servent* @getservbyname(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %14, %struct.servent** %4
%15 = load %struct.servent*, %struct.servent** %4
%16 = icmp eq %struct.servent* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = call i32 @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %12
%21 = load %struct.servent*, %struct.servent** %4
%22 = getelementptr inbounds %struct.servent, %struct.servent* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i8*, i8** %2
%29 = call i32 @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %20
br label %31
31: ; preds = %30, %9
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [4 x i8] c"tcp\00"
@.str.1 = [5 x i8] c"-ERR\00"
@.str.2 = [22 x i8] c"service not found: %s\00"
@.str.3 = [15 x i8] c"port error: %s\00"
define i32 @getportnum(i8* %0) {
%2 = load i8, i8* %0
%3 = tail call i64 @isdigit(i8 signext %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call i32 @atol(i8* %0)
br label %19
7: ; preds = %1
%8 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq %struct.servent* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %7
%13 = getelementptr inbounds %struct.servent, %struct.servent* %8, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @ntohs(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @printerror(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %19
19: ; preds = %12, %17, %5
%20 = phi i32 [ %6, %5 ], [ 0, %17 ], [ %15, %12 ]
ret i32 %20
}