#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_name;
};

/* Variables and functions */
unsigned int IPPROTO_TCP;
int /*<<< orphan*/ bzero(char *, int);
struct servent *getservbyport(unsigned int, char *);
int ntohs(unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
void servlook(int servnames, unsigned int port, unsigned int protocol,
              char *target, int maxlen) {
  static struct servent *sve;
  bzero(target, maxlen + 1);
  if (servnames) {
    if (protocol == IPPROTO_TCP)
      sve = getservbyport(port, "tcp");
    else
      sve = getservbyport(port, "udp");
    if (sve != NULL) {
      strncpy(target, sve->s_name, maxlen);
    } else {
      sprintf(target, "%u", ntohs(port));
    }
  } else {
    sprintf(target, "%u", ntohs(port));
  }
}


x86 assembly (clang, -O0):
<servlook>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1050 <bzero@plt>
cmpl   $0x0,-0x4(%rbp)
je     1214 <servlook+0xc4>
mov    -0xc(%rbp),%eax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%eax
jne    11af <servlook+0x5f>
mov    -0x8(%rbp),%edi
lea    0xe62(%rip),%rsi
call   1070 <getservbyport@plt>
mov    %rax,0x2ea6(%rip)
jmp    11c5 <servlook+0x75>
mov    -0x8(%rbp),%edi
lea    0xe4b(%rip),%rsi
call   1070 <getservbyport@plt>
mov    %rax,0x2e8b(%rip)
cmpq   $0x0,0x2e83(%rip)
je     11ed <servlook+0x9d>
mov    -0x18(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
jmp    120f <servlook+0xbf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <ntohs@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xdfe(%rip),%rsi
call   1060 <sprintf@plt>
jmp    1236 <servlook+0xe6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <ntohs@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xdd7(%rip),%rsi
call   1060 <sprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<servlook>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r14d
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %esi,%r15d
mov    %edi,%ebp
lea    0x1(%r14),%esi
mov    %rcx,%rdi
call   1050 <bzero@plt>
test   %ebp,%ebp
je     11bb <servlook+0x6b>
mov    0x2e6b(%rip),%rax
cmp    %r12d,(%rax)
lea    0xe79(%rip),%rax
lea    0xe76(%rip),%rsi
cmove  %rax,%rsi
mov    %r15d,%edi
call   1070 <getservbyport@plt>
mov    %rax,0x2eaf(%rip)
test   %rax,%rax
je     11bb <servlook+0x6b>
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r14d,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strncpy@plt>
mov    %r15d,%edi
call   1040 <ntohs@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sprintf@plt>

LLVM IR (-O0):
%struct.servent = type { i32 }
@servlook.sve = internal global %struct.servent* null
@IPPROTO_TCP = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@.str.2 = [3 x i8] c"%u\00"
define void @servlook(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%11 = load i8*, i8** %9
%12 = load i32, i32* %10
%13 = add nsw i32 %12, 1
%14 = call i32 @bzero(i8* %11, i32 %13)
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %43
17: ; preds = %5
%18 = load i32, i32* %8
%19 = load i32, i32* @IPPROTO_TCP
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %7
%23 = call %struct.servent* @getservbyport(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.servent* %23, %struct.servent** @servlook.sve
br label %27
24: ; preds = %17
%25 = load i32, i32* %7
%26 = call %struct.servent* @getservbyport(i32 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %26, %struct.servent** @servlook.sve
br label %27
27: ; preds = %24, %21
%28 = load %struct.servent*, %struct.servent** @servlook.sve
%29 = icmp ne %struct.servent* %28, null
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i8*, i8** %9
%32 = load %struct.servent*, %struct.servent** @servlook.sve
%33 = getelementptr inbounds %struct.servent, %struct.servent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* %10
%36 = call i32 @strncpy(i8* %31, i32 %34, i32 %35)
br label %42
37: ; preds = %27
%38 = load i8*, i8** %9
%39 = load i32, i32* %7
%40 = call i32 @ntohs(i32 %39)
%41 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %37, %30
br label %48
43: ; preds = %5
%44 = load i8*, i8** %9
%45 = load i32, i32* %7
%46 = call i32 @ntohs(i32 %45)
%47 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %43, %42
ret void
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@servlook.sve = internal unnamed_addr global %struct.servent* null
@IPPROTO_TCP = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@.str.2 = [3 x i8] c"%u\00"
define void @servlook(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = add nsw i32 %4, 1
%7 = tail call i32 @bzero(i8* %3, i32 %6)
%8 = icmp eq i32 %0, 0
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = load i32, i32* @IPPROTO_TCP
%11 = icmp eq i32 %10, %2
%12 = select i1 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%13 = tail call %struct.servent* @getservbyport(i32 %1, i8* %12)
store %struct.servent* %13, %struct.servent** @servlook.sve
%14 = icmp eq %struct.servent* %13, null
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = getelementptr inbounds %struct.servent, %struct.servent* %13, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @strncpy(i8* %3, i32 %17, i32 %4)
br label %25
19: ; preds = %9
%20 = tail call i32 @ntohs(i32 %1)
%21 = tail call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %25
22: ; preds = %5
%23 = tail call i32 @ntohs(i32 %1)
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %15, %19, %22
ret void
}