#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 protoent {
  scalar_t__ *p_aliases;
  scalar_t__ p_name;
};

/* Variables and functions */
int /*<<< orphan*/ endprotoent();
int /*<<< orphan*/ getprotoent_r(struct protoent *, char *, size_t,
                                 struct protoent **);
int /*<<< orphan*/ strcmp(scalar_t__, char const *);
extern int getprotobyname_r(const char *name, struct protoent *res, char *buf,
                            size_t buflen, struct protoent **res_sig) {
  while (!getprotoent_r(res, buf, buflen, res_sig)) {
    int i;
    if (!strcmp(res->p_name, name))
      goto found;
    for (i = 0; res->p_aliases[i]; ++i)
      if (!strcmp(res->p_aliases[i], name))
        goto found;
  }
  *res_sig = 0;
found:
  endprotoent();
  return *res_sig ? 0 : -1;
}


x86 assembly (clang, -O0):
<getprotobyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <getprotoent_r@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1176 <getprotobyname_r+0x46>
jmp    11f0 <getprotobyname_r+0xc0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    1195 <getprotobyname_r+0x65>
jmp    11fb <getprotobyname_r+0xcb>
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11eb <getprotobyname_r+0xbb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11d8 <getprotobyname_r+0xa8>
jmp    11fb <getprotobyname_r+0xcb>
jmp    11dd <getprotobyname_r+0xad>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119c <getprotobyname_r+0x6c>
jmp    114c <getprotobyname_r+0x1c>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1030 <endprotoent@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getprotobyname_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r13
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rcx,%rdx
mov    %r8,%rcx
call   1050 <getprotoent_r@plt>
test   %eax,%eax
jne    11aa <getprotobyname_r+0x7a>
mov    0x8(%rbx),%rdi
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11b2 <getprotobyname_r+0x82>
mov    (%rbx),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     119c <getprotobyname_r+0x6c>
mov    $0x8,%ebp
nop
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11b2 <getprotobyname_r+0x82>
mov    (%rbx),%rax
mov    (%rax,%rbp,1),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    1180 <getprotobyname_r+0x50>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
jmp    1156 <getprotobyname_r+0x26>
movq   $0x0,(%r12)
xor    %eax,%eax
call   1030 <endprotoent@plt>
cmpq   $0x1,(%r12)
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.protoent = type { i64*, i64 }
define i32 @getprotobyname_r(i8* %0, %struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4) {
%6 = alloca i8*
%7 = alloca %struct.protoent*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.protoent**
%11 = alloca i32
store i8* %0, i8** %6
store %struct.protoent* %1, %struct.protoent** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store %struct.protoent** %4, %struct.protoent*** %10
br label %12
12: ; preds = %54, %5
%13 = load %struct.protoent*, %struct.protoent** %7
%14 = load i8*, i8** %8
%15 = load i64, i64* %9
%16 = load %struct.protoent**, %struct.protoent*** %10
%17 = call i32 @getprotoent_r(%struct.protoent* %13, i8* %14, i64 %15, %struct.protoent** %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %55
20: ; preds = %12
%21 = load %struct.protoent*, %struct.protoent** %7
%22 = getelementptr inbounds %struct.protoent, %struct.protoent* %21, i32 0, i32 1
%23 = load i64, i64* %22
%24 = load i8*, i8** %6
%25 = call i32 @strcmp(i64 %23, i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %20
br label %57
28: ; preds = %20
store i32 0, i32* %11
br label %29
29: ; preds = %51, %28
%30 = load %struct.protoent*, %struct.protoent** %7
%31 = getelementptr inbounds %struct.protoent, %struct.protoent* %30, i32 0, i32 0
%32 = load i64*, i64** %31
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i64, i64* %32, i64 %34
%36 = load i64, i64* %35
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %54
38: ; preds = %29
%39 = load %struct.protoent*, %struct.protoent** %7
%40 = getelementptr inbounds %struct.protoent, %struct.protoent* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i64, i64* %41, i64 %43
%45 = load i64, i64* %44
%46 = load i8*, i8** %6
%47 = call i32 @strcmp(i64 %45, i8* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %38
br label %57
50: ; preds = %38
br label %51
51: ; preds = %50
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
br label %29
54: ; preds = %29
br label %12
55: ; preds = %12
%56 = load %struct.protoent**, %struct.protoent*** %10
store %struct.protoent* null, %struct.protoent** %56
br label %57
57: ; preds = %55, %49, %27
%58 = call i32 (...) @endprotoent()
%59 = load %struct.protoent**, %struct.protoent*** %10
%60 = load %struct.protoent*, %struct.protoent** %59
%61 = icmp ne %struct.protoent* %60, null
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 0, i32 -1
ret i32 %63
}

LLVM IR (-O2):
%struct.protoent = type { i64*, i64 }
define i32 @getprotobyname_r(i8* %0, %struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4) {
%6 = tail call i32 @getprotoent_r(%struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %33
8: ; preds = %5
%9 = getelementptr inbounds %struct.protoent, %struct.protoent* %1, i64 0, i32 1
%10 = getelementptr inbounds %struct.protoent, %struct.protoent* %1, i64 0, i32 0
br label %14
11: ; preds = %22, %18
%12 = tail call i32 @getprotoent_r(%struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %33
14: ; preds = %8, %11
%15 = load i64, i64* %9
%16 = tail call i32 @strcmp(i64 %15, i8* %0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %34, label %18
18: ; preds = %14
%19 = load i64*, i64** %10
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, 0
br i1 %21, label %11, label %27
22: ; preds = %27
%23 = load i64*, i64** %10
%24 = getelementptr inbounds i64, i64* %23, i64 %32
%25 = load i64, i64* %24
%26 = icmp eq i64 %25, 0
br i1 %26, label %11, label %27
27: ; preds = %18, %22
%28 = phi i64 [ %32, %22 ], [ 0, %18 ]
%29 = phi i64 [ %25, %22 ], [ %20, %18 ]
%30 = tail call i32 @strcmp(i64 %29, i8* %0)
%31 = icmp eq i32 %30, 0
%32 = add nuw i64 %28, 1
br i1 %31, label %34, label %22
33: ; preds = %11, %5
store %struct.protoent* null, %struct.protoent** %4
br label %34
34: ; preds = %14, %27, %33
%35 = tail call i32 (...) @endprotoent()
%36 = load %struct.protoent*, %struct.protoent** %4
%37 = icmp eq %struct.protoent* %36, null
%38 = sext i1 %37 to i32
ret i32 %38
}