#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 */
#define AF_INET 129
#define AF_INET6 128
int EINVAL;
int ERANGE;
int __dns_gethostbyx_r(char const *, struct hostent *, char *, size_t,
                       struct hostent **, int *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int diet_gethostbyname2_r(const char *name, int AF, struct hostent *result,
                          char *buf, size_t buflen, struct hostent **RESULT,
                          int *h_errnop) {
  size_t L = strlen(name);
  int lookfor = 0;
  switch (AF) {
  case AF_INET:
    lookfor = 1;
    break;
  case AF_INET6:
    lookfor = 28;
    break;
  default:
    *h_errnop = EINVAL;
    return 1;
  }
  result->h_name = buf;
  if (buflen < L) {
    *h_errnop = ERANGE;
    return 1;
  }
  strcpy(buf, name);
  return __dns_gethostbyx_r(name, result, buf + L, buflen - L, RESULT, h_errnop,
                            lookfor);
}


x86 assembly (clang, -O0):
<diet_gethostbyname2_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
sub    $0x80,%eax
je     119c <diet_gethostbyname2_r+0x6c>
jmp    117d <diet_gethostbyname2_r+0x4d>
mov    -0x48(%rbp),%eax
sub    $0x81,%eax
jne    11a8 <diet_gethostbyname2_r+0x78>
jmp    1190 <diet_gethostbyname2_r+0x60>
movl   $0x1,-0x44(%rbp)
jmp    11c3 <diet_gethostbyname2_r+0x93>
movl   $0x1c,-0x44(%rbp)
jmp    11c3 <diet_gethostbyname2_r+0x93>
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1232 <diet_gethostbyname2_r+0x102>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    11f7 <diet_gethostbyname2_r+0xc7>
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1232 <diet_gethostbyname2_r+0x102>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rcx
sub    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    0x10(%rbp),%r9
mov    -0x44(%rbp),%eax
mov    %eax,(%rsp)
call   1050 <__dns_gethostbyx_r@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<diet_gethostbyname2_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,%r12
mov    %rcx,%rbx
mov    %rdx,%r13
mov    %esi,%r15d
mov    %rdi,%rbp
call   1040 <strlen@plt>
mov    %rax,%r14
cmp    $0x81,%r15d
je     1174 <diet_gethostbyname2_r+0x44>
cmp    $0x80,%r15d
jne    118c <diet_gethostbyname2_r+0x5c>
mov    $0x1c,%r15d
jmp    117a <diet_gethostbyname2_r+0x4a>
mov    $0x1,%r15d
mov    %rbx,0x0(%r13)
cmp    %r12,%r14
jbe    11a3 <diet_gethostbyname2_r+0x73>
mov    0x2e5e(%rip),%rax
jmp    1193 <diet_gethostbyname2_r+0x63>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    0x50(%rsp),%rcx
mov    %eax,(%rcx)
mov    $0x1,%eax
jmp    11d3 <diet_gethostbyname2_r+0xa3>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
add    %r14,%rbx
sub    %r14,%r12
mov    %r15d,(%rsp)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    0x10(%rsp),%r8
mov    0x50(%rsp),%r9
call   1050 <__dns_gethostbyx_r@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8* }
@EINVAL = global i32 0
@ERANGE = global i32 0
define i32 @diet_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, %struct.hostent** %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca %struct.hostent**
%15 = alloca i32*
%16 = alloca i64
%17 = alloca i32
store i8* %0, i8** %9
store i32 %1, i32* %10
store %struct.hostent* %2, %struct.hostent** %11
store i8* %3, i8** %12
store i64 %4, i64* %13
store %struct.hostent** %5, %struct.hostent*** %14
store i32* %6, i32** %15
%18 = load i8*, i8** %9
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %16
store i32 0, i32* %17
%20 = load i32, i32* %10
switch i32 %20, label %23 [
i32 129, label %21
i32 128, label %22
]
21: ; preds = %7
store i32 1, i32* %17
br label %26
22: ; preds = %7
store i32 28, i32* %17
br label %26
23: ; preds = %7
%24 = load i32, i32* @EINVAL
%25 = load i32*, i32** %15
store i32 %24, i32* %25
store i32 1, i32* %8
br label %52
26: ; preds = %22, %21
%27 = load i8*, i8** %12
%28 = load %struct.hostent*, %struct.hostent** %11
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
store i8* %27, i8** %29
%30 = load i64, i64* %13
%31 = load i64, i64* %16
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* @ERANGE
%35 = load i32*, i32** %15
store i32 %34, i32* %35
store i32 1, i32* %8
br label %52
36: ; preds = %26
%37 = load i8*, i8** %12
%38 = load i8*, i8** %9
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %9
%41 = load %struct.hostent*, %struct.hostent** %11
%42 = load i8*, i8** %12
%43 = load i64, i64* %16
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = load i64, i64* %13
%46 = load i64, i64* %16
%47 = sub i64 %45, %46
%48 = load %struct.hostent**, %struct.hostent*** %14
%49 = load i32*, i32** %15
%50 = load i32, i32* %17
%51 = call i32 @__dns_gethostbyx_r(i8* %40, %struct.hostent* %41, i8* %44, i64 %47, %struct.hostent** %48, i32* %49, i32 %50)
store i32 %51, i32* %8
br label %52
52: ; preds = %36, %33, %23
%53 = load i32, i32* %8
ret i32 %53
}

LLVM IR (-O2):
%struct.hostent = type { i8* }
@EINVAL = global i32 0
@ERANGE = global i32 0
define i32 @diet_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, %struct.hostent** %5, i32* %6) {
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
switch i32 %1, label %10 [
i32 129, label %12
i32 128, label %9
]
9: ; preds = %7
br label %12
10: ; preds = %7
%11 = load i32, i32* @EINVAL
store i32 %11, i32* %6
br label %23
12: ; preds = %7, %9
%13 = phi i32 [ 28, %9 ], [ 1, %7 ]
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
store i8* %3, i8** %14
%15 = icmp ugt i64 %8, %4
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* @ERANGE
store i32 %17, i32* %6
br label %23
18: ; preds = %12
%19 = tail call i32 @strcpy(i8* %3, i8* %0)
%20 = getelementptr inbounds i8, i8* %3, i64 %8
%21 = sub i64 %4, %8
%22 = tail call i32 @__dns_gethostbyx_r(i8* %0, %struct.hostent* %2, i8* %20, i64 %21, %struct.hostent** %5, i32* %6, i32 %13)
br label %23
23: ; preds = %18, %16, %10
%24 = phi i32 [ 1, %10 ], [ 1, %16 ], [ %22, %18 ]
ret i32 %24
}