#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 const *, char *);
int strtol(char const *, char **, int);
int /*<<< orphan*/ usage(char *);
int parse_port(const char *str) {
  struct servent *serv;
  char *ptr;
  int num = strtol(str, &ptr, 10);
  if (!*ptr)
    return num;
  serv = getservbyname(str, "udp");
  if (!serv)
    usage("Unknown port address");
  return serv->s_port;
}


x86 assembly (clang, -O0):
<parse_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1169 <parse_port+0x39>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119d <parse_port+0x6d>
mov    -0x10(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1030 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <parse_port+0x64>
lea    0xe75(%rip),%rdi
call   1050 <usage@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_port>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1176 <parse_port+0x46>
lea    0xea8(%rip),%rsi
mov    %rbx,%rdi
call   1030 <getservbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1174 <parse_port+0x44>
lea    0xe95(%rip),%rdi
call   1050 <usage@plt>
mov    (%rbx),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [4 x i8] c"udp\00"
@.str.1 = [21 x i8] c"Unknown port address\00"
define i32 @parse_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.servent*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strtol(i8* %7, i8** %5, i32 10)
store i32 %8, i32* %6
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = load i32, i32* %6
store i32 %13, i32* %2
br label %25
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call %struct.servent* @getservbyname(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %16, %struct.servent** %4
%17 = load %struct.servent*, %struct.servent** %4
%18 = icmp ne %struct.servent* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = call i32 @usage(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = load %struct.servent*, %struct.servent** %4
%23 = getelementptr inbounds %struct.servent, %struct.servent* %22, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %2
br label %25
25: ; preds = %21, %12
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [4 x i8] c"udp\00"
@.str.1 = [21 x i8] c"Unknown port address\00"
define i32 @parse_port(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i32 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %1
%9 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq %struct.servent* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @usage(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = getelementptr inbounds %struct.servent, %struct.servent* %9, i64 0, i32 0
%15 = load i32, i32* %14
br label %16
16: ; preds = %1, %13
%17 = phi i32 [ %15, %13 ], [ %4, %1 ]
ret i32 %17
}