#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 *);
int /*<<< orphan*/ exit(int);
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 | 0x040000, "-ERR", "service not found: %s", name);
      exit(1);
    }
    port = ntohs(portdesc->s_port);
    if (port == 0) {
      printerror(1 | 0x040000, "-ERR", "port error: %s\n", name);
      exit(1);
    }
  }
  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     1193 <getportnum+0x33>
mov    -0x8(%rbp),%rdi
call   1070 <atol@plt>
mov    %eax,-0xc(%rbp)
jmp    121b <getportnum+0xbb>
mov    -0x8(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <getportnum+0x78>
mov    -0x8(%rbp),%rcx
mov    $0x40001,%edi
lea    0xe42(%rip),%rsi
lea    0xe40(%rip),%rdx
call   1030 <printerror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1216 <getportnum+0xb6>
mov    -0x8(%rbp),%rcx
mov    $0x40001,%edi
lea    0xe04(%rip),%rsi
lea    0xe18(%rip),%rdx
call   1030 <printerror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    121b <getportnum+0xbb>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getportnum>:
push   %rbx
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     117a <getportnum+0x1a>
mov    %rbx,%rdi
pop    %rbx
jmp    1070 <atol@plt>
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <getservbyname@plt>
test   %rax,%rax
je     119b <getportnum+0x3b>
mov    (%rax),%edi
call   1060 <ntohs@plt>
test   %eax,%eax
je     11ab <getportnum+0x4b>
pop    %rbx
ret
lea    0xe62(%rip),%rsi
lea    0xe60(%rip),%rdx
jmp    11b9 <getportnum+0x59>
lea    0xe52(%rip),%rsi
lea    0xe66(%rip),%rdx
mov    $0x40001,%edi
mov    %rbx,%rcx
call   1030 <printerror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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 = [16 x i8] c"port error: %s\0A\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 %33
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 %21
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = call i32 @printerror(i32 262145, 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)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load %struct.servent*, %struct.servent** %4
%23 = getelementptr inbounds %struct.servent, %struct.servent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @ntohs(i32 %24)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i8*, i8** %2
%30 = call i32 @printerror(i32 262145, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
br label %33
33: ; preds = %32, %9
%34 = load i32, i32* %3
ret i32 %34
}

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 = [16 x i8] c"port error: %s\0A\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 %21
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 %13
10: ; preds = %7
%11 = tail call i32 @printerror(i32 262145, 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)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = getelementptr inbounds %struct.servent, %struct.servent* %8, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @ntohs(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @printerror(i32 262145, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13, %5
%22 = phi i32 [ %6, %5 ], [ %16, %13 ]
ret i32 %22
}