#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 {
  char *s_proto;
  int /*<<< orphan*/ s_port;
};

/* Variables and functions */
int _servid_error(char const *, int);
struct servent *getservbyname(char const *, char const *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ setservent(int);
int _servid(char const *protocol, char const *service) {
  struct servent *se;
  setservent(1);
  if ((se = getservbyname(service, protocol)) == NULL)
    return _servid_error(service, 1);
  printf("%d/%s\n", ntohs(se->s_port), se->s_proto);
  return 0;
}


x86 assembly (clang, -O0):
<_servid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x1,%edi
call   1050 <setservent@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119b <_servid+0x4b>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1060 <_servid_error@plt>
mov    %eax,-0x4(%rbp)
jmp    11c3 <_servid+0x73>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
call   1070 <ntohs@plt>
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_servid>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1,%edi
call   1050 <setservent@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     119a <_servid+0x4a>
mov    %rax,%rbx
mov    0x8(%rax),%edi
call   1070 <ntohs@plt>
mov    (%rbx),%rdx
lea    0xe77(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
mov    $0x1,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <_servid_error@plt>

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

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