#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 */
struct servent *getservbyport(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *servname(int port, char *proto) {
  struct servent *serv;
  static char str[16];
  if ((serv = getservbyport(htons(port), proto)))
    strncpy(str, serv->s_name, 16);
  else
    snprintf(str, 16, "%i", port);
  return str;
}


x86 assembly (clang, -O0):
<servname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
call   1060 <getservbyport@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     118c <servname+0x4c>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0x2ed3(%rip),%rdi
mov    $0x10,%edx
call   1030 <strncpy@plt>
jmp    11a7 <servname+0x67>
mov    -0x4(%rbp),%ecx
lea    0x2eba(%rip),%rdi
mov    $0x10,%esi
lea    0xe5e(%rip),%rdx
call   1050 <snprintf@plt>
lea    0x2ea2(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<servname>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <htons@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1060 <getservbyport@plt>
test   %rax,%rax
je     1171 <servname+0x31>
mov    (%rax),%esi
lea    0x2eeb(%rip),%rdi
mov    $0x10,%edx
call   1030 <strncpy@plt>
jmp    118b <servname+0x4b>
lea    0x2ed8(%rip),%rdi
lea    0xe81(%rip),%rdx
mov    $0x10,%esi
mov    %ebp,%ecx
call   1050 <snprintf@plt>
lea    0x2ebe(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@servname.str = internal global [16 x i8] zeroinitializer
@.str = [3 x i8] c"%i\00"
define i8* @servname(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.servent*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* %3
%7 = call i32 @htons(i32 %6)
%8 = load i8*, i8** %4
%9 = call %struct.servent* @getservbyport(i32 %7, i8* %8)
store %struct.servent* %9, %struct.servent** %5
%10 = icmp ne %struct.servent* %9, null
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load %struct.servent*, %struct.servent** %5
%13 = getelementptr inbounds %struct.servent, %struct.servent* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strncpy(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @servname.str, i64 0, i64 0), i32 %14, i32 16)
br label %19
16: ; preds = %2
%17 = load i32, i32* %3
%18 = call i32 @snprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @servname.str, i64 0, i64 0), i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %11
ret i8* getelementptr inbounds ([16 x i8], [16 x i8]* @servname.str, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@servname.str = internal global [16 x i8] zeroinitializer
@.str = [3 x i8] c"%i\00"
define i8* @servname(i32 %0, i8* %1) {
%3 = tail call i32 @htons(i32 %0)
%4 = tail call %struct.servent* @getservbyport(i32 %3, i8* %1)
%5 = icmp eq %struct.servent* %4, null
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = getelementptr inbounds %struct.servent, %struct.servent* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @strncpy(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @servname.str, i64 0, i64 0), i32 %8, i32 16)
br label %12
10: ; preds = %2
%11 = tail call i32 @snprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @servname.str, i64 0, i64 0), i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %12
12: ; preds = %10, %6
ret i8* getelementptr inbounds ([16 x i8], [16 x i8]* @servname.str, i64 0, i64 0)
}