#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 */
struct servent *getservbyname(char const *, char *);
int ntohs(int /*<<< orphan*/);
scalar_t__ strcasecmp(char const *, char *);
int _fetch_default_port(const char *scheme) {
  struct servent *se;
  if ((se = getservbyname(scheme, "tcp")) != NULL)
    return (ntohs(se->s_port));
  if (strcasecmp(scheme, "ftp") == 0)
    return (21);
  if (strcasecmp(scheme, "http") == 0)
    return (80);
  return (0);
}


x86 assembly (clang, -O0):
<_fetch_default_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     116d <_fetch_default_port+0x3d>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    11c0 <_fetch_default_port+0x90>
mov    -0x10(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1193 <_fetch_default_port+0x63>
movl   $0x15,-0x4(%rbp)
jmp    11c0 <_fetch_default_port+0x90>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    11b9 <_fetch_default_port+0x89>
movl   $0x50,-0x4(%rbp)
jmp    11c0 <_fetch_default_port+0x90>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fetch_default_port>:
push   %rbx
mov    %rdi,%rbx
lea    0xec5(%rip),%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     114d <_fetch_default_port+0x1d>
mov    (%rax),%edi
pop    %rbx
jmp    1050 <ntohs@plt>
lea    0xeb0(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     117f <_fetch_default_port+0x4f>
lea    0xea0(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
mov    $0x50,%eax
cmovne %ecx,%eax
pop    %rbx
ret
mov    $0x15,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"ftp\00"
@.str.2 = [5 x i8] c"http\00"
define i32 @_fetch_default_port(i8* %0) {
%2 = tail call %struct.servent* @getservbyname(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq %struct.servent* %2, null
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @ntohs(i32 %6)
br label %15
8: ; preds = %1
%9 = tail call i64 @strcasecmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i64 @strcasecmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
%14 = select i1 %13, i32 80, i32 0
br label %15
15: ; preds = %11, %8, %4
%16 = phi i32 [ %7, %4 ], [ 21, %8 ], [ %14, %11 ]
ret i32 %16
}