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

/* Variables and functions */
int TRY_AGAIN;
int errno;
int gethostbyname_r(char const *, struct hostent *, char *, size_t,
                    struct hostent **, int *);
struct hostent *resolve(const char *name, struct hostent *hostbuf, char *buf,
                        size_t len) {
  struct hostent *hp;
  int herr, rc = 0, i;
  for (i = 0; i < 3; i++) {
    rc = gethostbyname_r(name, hostbuf, buf, len, &hp, &herr);
    if (!rc) {
      return hp;
    } else if (herr == TRY_AGAIN) {
      continue;
    } else {
      break;
    }
  }
  errno = rc;
  return NULL;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3,-0x3c(%rbp)
jge    11a1 <resolve+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
lea    -0x34(%rbp),%r9
call   1030 <gethostbyname_r@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1177 <resolve+0x67>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <resolve+0xa5>
mov    -0x34(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
jne    118e <resolve+0x7e>
jmp    1193 <resolve+0x83>
jmp    11a1 <resolve+0x91>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1136 <resolve+0x26>
mov    -0x38(%rbp),%ecx
mov    0x2e2d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
call   1030 <gethostbyname_r@plt>
test   %eax,%eax
je     11a2 <resolve+0x92>
mov    0x4(%rsp),%ecx
mov    0x2e99(%rip),%r13
cmp    0x0(%r13),%ecx
jne    1195 <resolve+0x85>
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <gethostbyname_r@plt>
test   %eax,%eax
je     11a2 <resolve+0x92>
mov    0x4(%rsp),%ecx
cmp    0x0(%r13),%ecx
jne    1195 <resolve+0x85>
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <gethostbyname_r@plt>
test   %eax,%eax
je     11a2 <resolve+0x92>
mov    0x2e3c(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    11a7 <resolve+0x97>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@TRY_AGAIN = global i32 0
@errno = global i32 0
define %struct.hostent* @resolve(i8* %0, %struct.hostent* %1, i8* %2, i64 %3) {
%5 = alloca %struct.hostent*
%6 = alloca i8*
%7 = alloca %struct.hostent*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.hostent*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store %struct.hostent* %1, %struct.hostent** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i32 0, i32* %12
store i32 0, i32* %13
br label %14
14: ; preds = %33, %4
%15 = load i32, i32* %13
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %36
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = load %struct.hostent*, %struct.hostent** %7
%20 = load i8*, i8** %8
%21 = load i64, i64* %9
%22 = call i32 @gethostbyname_r(i8* %18, %struct.hostent* %19, i8* %20, i64 %21, %struct.hostent** %10, i32* %11)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = load %struct.hostent*, %struct.hostent** %10
store %struct.hostent* %26, %struct.hostent** %5
br label %38
27: ; preds = %17
%28 = load i32, i32* %11
%29 = load i32, i32* @TRY_AGAIN
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %33
32: ; preds = %27
br label %36
33: ; preds = %31
%34 = load i32, i32* %13
%35 = add nsw i32 %34, 1
store i32 %35, i32* %13
br label %14
36: ; preds = %32, %14
%37 = load i32, i32* %12
store i32 %37, i32* @errno
store %struct.hostent* null, %struct.hostent** %5
br label %38
38: ; preds = %36, %25
%39 = load %struct.hostent*, %struct.hostent** %5
ret %struct.hostent* %39
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@TRY_AGAIN = global i32 0
@errno = global i32 0
define %struct.hostent* @resolve(i8* %0, %struct.hostent* %1, i8* %2, i64 %3) {
%5 = alloca %struct.hostent*
%6 = alloca i32
%7 = bitcast %struct.hostent** %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = call i32 @gethostbyname_r(i8* %0, %struct.hostent* %1, i8* %2, i64 %3, %struct.hostent** %5, i32* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %21, label %23
11: ; preds = %23
%12 = call i32 @gethostbyname_r(i8* %0, %struct.hostent* %1, i8* %2, i64 %3, %struct.hostent** %5, i32* %6)
%13 = icmp eq i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = load i32, i32* %6
%16 = load i32, i32* @TRY_AGAIN
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = call i32 @gethostbyname_r(i8* %0, %struct.hostent* %1, i8* %2, i64 %3, %struct.hostent** %5, i32* %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18, %11, %4
%22 = load %struct.hostent*, %struct.hostent** %5
br label %29
23: ; preds = %4
%24 = load i32, i32* %6
%25 = load i32, i32* @TRY_AGAIN
%26 = icmp eq i32 %24, %25
br i1 %26, label %11, label %27
27: ; preds = %18, %14, %23
%28 = phi i32 [ %9, %23 ], [ %12, %14 ], [ %19, %18 ]
store i32 %28, i32* @errno
br label %29
29: ; preds = %27, %21
%30 = phi %struct.hostent* [ null, %27 ], [ %22, %21 ]
ret %struct.hostent* %30
}