#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_name;
  char *s_proto;
};

/* Variables and functions */
int /*<<< orphan*/ endservent();
struct servent *getservent();
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ setservent(int);
scalar_t__ strcmp(char const *, char *);
struct servent *my_getservnam(char const *name, char const *proto) {
  struct servent *ptr = 0;
  setservent(1);
  while ((ptr = getservent()) != NULL) {
    printf("searching %s, %s\n", ptr->s_name, ptr->s_proto);
    if (strcmp(name, ptr->s_name) == 0 && strcmp(proto, ptr->s_proto) == 0)
      break;
  }
  endservent();
  return (ptr);
}


x86 assembly (clang, -O0):
<my_getservnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x1,%edi
call   1050 <setservent@plt>
mov    $0x0,%al
call   1030 <getservent@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11e1 <my_getservnam+0x91>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11dc <my_getservnam+0x8c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11dc <my_getservnam+0x8c>
jmp    11e1 <my_getservnam+0x91>
jmp    1172 <my_getservnam+0x22>
mov    $0x0,%al
call   1070 <endservent@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getservnam>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1,%edi
call   1050 <setservent@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getservent@plt>
test   %rax,%rax
je     11d3 <my_getservnam+0x83>
lea    0xe83(%rip),%r12
mov    %rax,%rbx
jmp    119f <my_getservnam+0x4f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getservent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d1 <my_getservnam+0x81>
mov    (%rbx),%rsi
mov    0x8(%rbx),%rdx
mov    %r12,%rdi
call   1040 <printf@plt>
mov    (%rbx),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1190 <my_getservnam+0x40>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1190 <my_getservnam+0x40>
jmp    11d3 <my_getservnam+0x83>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <endservent@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.servent = type { i8*, i8* }
@.str = [18 x i8] c"searching %s, %s\0A\00"
define %struct.servent* @my_getservnam(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.servent*
store i8* %0, i8** %3
store i8* %1, i8** %4
store %struct.servent* null, %struct.servent** %5
%6 = call i32 @setservent(i32 1)
br label %7
7: ; preds = %32, %2
%8 = call %struct.servent* (...) @getservent()
store %struct.servent* %8, %struct.servent** %5
%9 = icmp ne %struct.servent* %8, null
br i1 %9, label %10, label %33
10: ; preds = %7
%11 = load %struct.servent*, %struct.servent** %5
%12 = getelementptr inbounds %struct.servent, %struct.servent* %11, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = load %struct.servent*, %struct.servent** %5
%15 = getelementptr inbounds %struct.servent, %struct.servent* %14, i32 0, i32 1
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %16)
%18 = load i8*, i8** %3
%19 = load %struct.servent*, %struct.servent** %5
%20 = getelementptr inbounds %struct.servent, %struct.servent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %18, i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %10
%25 = load i8*, i8** %4
%26 = load %struct.servent*, %struct.servent** %5
%27 = getelementptr inbounds %struct.servent, %struct.servent* %26, i32 0, i32 1
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %25, i8* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %33
32: ; preds = %24, %10
br label %7
33: ; preds = %31, %7
%34 = call i32 (...) @endservent()
%35 = load %struct.servent*, %struct.servent** %5
ret %struct.servent* %35
}

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