#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 hostent {
  char *h_name;
};

/* Variables and functions */
int /*<<< orphan*/ endhostent();
struct hostent *gethostent();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sethostent(int);
scalar_t__ strcmp(char const *, char *);
struct hostent *my_gethostnam(char const *name) {
  struct hostent *ptr = 0;
  sethostent(1);
  while ((ptr = gethostent()) != NULL) {
    printf("searching %s\n", ptr->h_name);
    if (strcmp(name, ptr->h_name) == 0)
      break;
  }
  endhostent();
  return (ptr);
}


x86 assembly (clang, -O0):
<my_gethostnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x1,%edi
call   1060 <sethostent@plt>
mov    $0x0,%al
call   1030 <gethostent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11ba <my_gethostnam+0x6a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11b5 <my_gethostnam+0x65>
jmp    11ba <my_gethostnam+0x6a>
jmp    116e <my_gethostnam+0x1e>
mov    $0x0,%al
call   1070 <endhostent@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_gethostnam>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x1,%edi
call   1060 <sethostent@plt>
lea    0xe97(%rip),%r15
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <gethostent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119a <my_gethostnam+0x4a>
mov    (%rbx),%rsi
mov    %r15,%rdi
call   1040 <printf@plt>
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1170 <my_gethostnam+0x20>
xor    %eax,%eax
call   1070 <endhostent@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i8* }
@.str = [14 x i8] c"searching %s\0A\00"
define %struct.hostent* @my_gethostnam(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.hostent*
store i8* %0, i8** %2
store %struct.hostent* null, %struct.hostent** %3
%4 = call i32 @sethostent(i32 1)
br label %5
5: ; preds = %20, %1
%6 = call %struct.hostent* (...) @gethostent()
store %struct.hostent* %6, %struct.hostent** %3
%7 = icmp ne %struct.hostent* %6, null
br i1 %7, label %8, label %21
8: ; preds = %5
%9 = load %struct.hostent*, %struct.hostent** %3
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i32 0, i32 0
%11 = load i8*, i8** %10
%12 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** %2
%14 = load %struct.hostent*, %struct.hostent** %3
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* %13, i8* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %8
br label %21
20: ; preds = %8
br label %5
21: ; preds = %19, %5
%22 = call i32 (...) @endhostent()
%23 = load %struct.hostent*, %struct.hostent** %3
ret %struct.hostent* %23
}

LLVM IR (-O2):
%struct.hostent = type { i8* }
@.str = [14 x i8] c"searching %s\0A\00"
define %struct.hostent* @my_gethostnam(i8* %0) {
%2 = tail call i32 @sethostent(i32 1)
br label %3
3: ; preds = %6, %1
%4 = tail call %struct.hostent* (...) @gethostent()
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %7
%11 = tail call i64 @strcmp(i8* %0, i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %3
13: ; preds = %6, %3
%14 = tail call i32 (...) @endhostent()
ret %struct.hostent* %4
}