#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 {
  int p_proto;
};

/* Variables and functions */
int /*<<< orphan*/ endprotoent();
int /*<<< orphan*/ getprotoent_r(struct protoent *, char *, size_t,
                                 struct protoent **);
extern int getprotobynumber_r(int proto, struct protoent *res, char *buf,
                              size_t buflen, struct protoent **res_sig) {
  while (!getprotoent_r(res, buf, buflen, res_sig))
    if (proto == res->p_proto)
      goto found;
  *res_sig = 0;
found:
  endprotoent();
  return *res_sig ? 0 : -1;
}


x86 assembly (clang, -O0):
<getprotobynumber_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%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   1040 <getprotoent_r@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1165 <getprotobynumber_r+0x45>
jmp    117e <getprotobynumber_r+0x5e>
mov    -0x4(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jne    1179 <getprotobynumber_r+0x59>
jmp    1189 <getprotobynumber_r+0x69>
jmp    113b <getprotobynumber_r+0x1b>
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):
<getprotobynumber_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <getprotoent_r@plt>
test   %eax,%eax
jne    115b <getprotobynumber_r+0x3b>
cmp    %ebp,(%rbx)
jne    1140 <getprotobynumber_r+0x20>
jmp    1162 <getprotobynumber_r+0x42>
movq   $0x0,(%r14)
xor    %eax,%eax
call   1030 <endprotoent@plt>
cmpq   $0x1,(%r14)
sbb    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.protoent = type { i32 }
define i32 @getprotobynumber_r(i32 %0, %struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4) {
%6 = alloca i32
%7 = alloca %struct.protoent*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.protoent**
store i32 %0, i32* %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 %11
11: ; preds = %26, %5
%12 = load %struct.protoent*, %struct.protoent** %7
%13 = load i8*, i8** %8
%14 = load i64, i64* %9
%15 = load %struct.protoent**, %struct.protoent*** %10
%16 = call i32 @getprotoent_r(%struct.protoent* %12, i8* %13, i64 %14, %struct.protoent** %15)
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %27
19: ; preds = %11
%20 = load i32, i32* %6
%21 = load %struct.protoent*, %struct.protoent** %7
%22 = getelementptr inbounds %struct.protoent, %struct.protoent* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp eq i32 %20, %23
br i1 %24, label %25, label %26
25: ; preds = %19
br label %29
26: ; preds = %19
br label %11
27: ; preds = %11
%28 = load %struct.protoent**, %struct.protoent*** %10
store %struct.protoent* null, %struct.protoent** %28
br label %29
29: ; preds = %27, %25
%30 = call i32 (...) @endprotoent()
%31 = load %struct.protoent**, %struct.protoent*** %10
%32 = load %struct.protoent*, %struct.protoent** %31
%33 = icmp ne %struct.protoent* %32, null
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 0, i32 -1
ret i32 %35
}

LLVM IR (-O2):
%struct.protoent = type { i32 }
define i32 @getprotobynumber_r(i32 %0, %struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4) {
%6 = getelementptr inbounds %struct.protoent, %struct.protoent* %1, i64 0, i32 0
br label %7
7: ; preds = %10, %5
%8 = tail call i32 @getprotoent_r(%struct.protoent* %1, i8* %2, i64 %3, %struct.protoent** %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, %0
br i1 %12, label %14, label %7
13: ; preds = %7
store %struct.protoent* null, %struct.protoent** %4
br label %14
14: ; preds = %10, %13
%15 = tail call i32 (...) @endprotoent()
%16 = load %struct.protoent*, %struct.protoent** %4
%17 = icmp eq %struct.protoent* %16, null
%18 = sext i1 %17 to i32
ret i32 %18
}