#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 */
int getservbyname_r(char const *, char const *, struct servent *, char *, int,
                    struct servent **);
int ntohs(int /*<<< orphan*/);
int qd_getservbyname(const char *name, const char *proto) {
  struct servent serv_info;
  struct servent *serv_info_res;
  enum { buf_len = 4096 };
  char buf[buf_len];
  int r =
      getservbyname_r(name, proto, &serv_info, buf, buf_len, &serv_info_res);
  if (r == 0 && serv_info_res != NULL) {
    return ntohs(serv_info.s_port);
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<qd_getservbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x1030(%rbp),%rcx
lea    -0x20(%rbp),%rdx
mov    $0x1000,%r8d
lea    -0x28(%rbp),%r9
call   1030 <getservbyname_r@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jne    1183 <qd_getservbyname+0x63>
cmpq   $0x0,-0x28(%rbp)
je     1183 <qd_getservbyname+0x63>
mov    -0x20(%rbp),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    118a <qd_getservbyname+0x6a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qd_getservbyname>:
sub    $0x1018,%rsp
mov    %rsp,%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r9
mov    $0x1000,%r8d
call   1030 <getservbyname_r@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    115a <qd_getservbyname+0x3a>
cmpq   $0x0,0x8(%rsp)
je     115a <qd_getservbyname+0x3a>
mov    (%rsp),%edi
call   1040 <ntohs@plt>
add    $0x1018,%rsp
ret

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

LLVM IR (-O2):
%struct.servent = type { i32 }
define i32 @qd_getservbyname(i8* %0, i8* %1) {
%3 = alloca %struct.servent
%4 = alloca %struct.servent*
%5 = alloca [4096 x i8]
%6 = bitcast %struct.servent* %3 to i8*
%7 = bitcast %struct.servent** %4 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = call i32 @getservbyname_r(i8* %0, i8* %1, %struct.servent* %3, i8* %8, i32 4096, %struct.servent** %4)
%10 = icmp eq i32 %9, 0
%11 = load %struct.servent*, %struct.servent** %4
%12 = icmp ne %struct.servent* %11, null
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = getelementptr inbounds %struct.servent, %struct.servent* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
br label %18
18: ; preds = %2, %14
%19 = phi i32 [ %17, %14 ], [ -1, %2 ]
ret i32 %19
}