#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;
};
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ hent;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int gethostbyaddr_r(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                    struct hostent *, char *, int, struct hostent **, int *);
int gethostbyname2_r(char *, int /*<<< orphan*/, struct hostent *, char *, int,
                     struct hostent **, int *);
int gethostbyname_r(char *, struct hostent *, char *, int, struct hostent **,
                    int *);
int gethostent_r(struct hostent *, char *, int, struct hostent **, int *);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct hostent *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  size_t i;
  char buf[1024];
  in_addr_t addr;
  addr = inet_addr("127.0.0.1");
  for (i = 0; i < sizeof(size_t) * 2; ++i) {
    struct hostent hent, *hentres;
    int ret, herr;
    printf("Testing misalignment of %2zi bytes: ", i);
    memset(&hent, 0x00, sizeof(hent));
    ret = gethostent_r(&hent, buf + i, sizeof(buf) - i, &hentres, &herr);
    printf("%sgethostent_r() ", (ret ? "!!!" : ""));
    memset(&hent, 0x00, sizeof(hent));
    ret = gethostbyname_r("localhost", &hent, buf + i, sizeof(buf) - i,
                          &hentres, &herr);
    printf("%sgethostbyname_r() ", (ret ? "!!!" : ""));
    memset(&hent, 0x00, sizeof(hent));
    ret = gethostbyname2_r("localhost", AF_INET, &hent, buf + i,
                           sizeof(buf) - i, &hentres, &herr);
    printf("%sgethostbyname2_r() ", (ret ? "!!!" : ""));
    memset(&hent, 0x00, sizeof(hent));
    ret = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf + i,
                          sizeof(buf) - i, &hentres, &herr);
    printf("%sgethostbyaddr_r() ", (ret ? "!!!" : ""));
    puts("OK!");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x424(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x10,-0x18(%rbp)
jae    13f0 <main+0x270>
mov    -0x18(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    -0x420(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    $0x400,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%edx
lea    -0x428(%rbp),%rdi
lea    -0x430(%rbp),%rcx
lea    -0x438(%rbp),%r8
call   1040 <gethostent_r@plt>
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%ecx
lea    0xe53(%rip),%rsi
lea    0xe14(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    -0x420(%rbp),%rdx
add    -0x18(%rbp),%rdx
mov    $0x400,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%ecx
lea    0xdd3(%rip),%rdi
lea    -0x428(%rbp),%rsi
lea    -0x430(%rbp),%r8
lea    -0x438(%rbp),%r9
call   1050 <gethostbyname_r@plt>
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%ecx
lea    0xdda(%rip),%rsi
lea    0xd9b(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
lea    -0x420(%rbp),%rcx
add    -0x18(%rbp),%rcx
mov    $0x400,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%r8d
lea    0xd50(%rip),%rdi
lea    -0x428(%rbp),%rdx
lea    -0x430(%rbp),%r9
lea    -0x438(%rbp),%rax
mov    %rax,(%rsp)
call   1090 <gethostbyname2_r@plt>
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%ecx
lea    0xd53(%rip),%rsi
lea    0xd14(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
lea    -0x420(%rbp),%r8
add    -0x18(%rbp),%r8
mov    $0x400,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%r9d
lea    -0x424(%rbp),%rdi
mov    $0x4,%esi
lea    -0x428(%rbp),%rcx
lea    -0x430(%rbp),%r10
lea    -0x438(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   10a0 <gethostbyaddr_r@plt>
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%ecx
lea    0xcc2(%rip),%rsi
lea    0xc83(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xcae(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xcb5(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11b3 <main+0x33>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
lea    0xe68(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0x400,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    0xe53(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    (%rsp,%rbp,1),%r14
add    $0x20,%r14
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
call   1040 <gethostent_r@plt>
test   %eax,%eax
lea    0xe44(%rip),%rsi
lea    0xe75(%rip),%rax
cmove  %rax,%rsi
lea    0xe20(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    0xe19(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %ebx,%ecx
lea    0x10(%rsp),%r12
mov    %r12,%r8
lea    0x8(%rsp),%r13
mov    %r13,%r9
call   1050 <gethostbyname_r@plt>
test   %eax,%eax
lea    0xdef(%rip),%rsi
lea    0xe20(%rip),%rax
cmove  %rax,%rsi
lea    0xdeb(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
sub    $0x8,%rsp
lea    0xdb7(%rip),%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %ebx,%r8d
mov    %r12,%r9
push   %r13
mov    %r13,%r12
call   1090 <gethostbyname2_r@plt>
add    $0x10,%rsp
test   %eax,%eax
lea    0xd90(%rip),%rsi
lea    0xdc1(%rip),%r13
cmove  %r13,%rsi
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    %r15,%rcx
mov    %r14,%r8
mov    %ebx,%r9d
push   %r12
lea    0x18(%rsp),%rax
push   %rax
call   10a0 <gethostbyaddr_r@plt>
add    $0x10,%rsp
test   %eax,%eax
lea    0xd32(%rip),%rsi
cmove  %r13,%rsi
lea    0xd60(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd67(%rip),%rdi
call   1030 <puts@plt>
add    $0x1,%rbp
add    $0xffffffff,%ebx
cmp    $0x10,%rbp
jne    11b0 <main+0x30>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [37 x i8] c"Testing misalignment of %2zi bytes: \00"
@.str.2 = [18 x i8] c"%sgethostent_r() \00"
@.str.3 = [4 x i8] c"!!!\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [10 x i8] c"localhost\00"
@.str.6 = [21 x i8] c"%sgethostbyname_r() \00"
@AF_INET = global i32 0
@.str.7 = [22 x i8] c"%sgethostbyname2_r() \00"
@.str.8 = [21 x i8] c"%sgethostbyaddr_r() \00"
@.str.9 = [4 x i8] c"OK!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca %struct.hostent
%10 = alloca %struct.hostent*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %13, i32* %8
store i64 0, i64* %6
br label %14
14: ; preds = %75, %2
%15 = load i64, i64* %6
%16 = icmp ult i64 %15, 16
br i1 %16, label %17, label %78
17: ; preds = %14
%18 = load i64, i64* %6
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %18)
%20 = call i32 @memset(%struct.hostent* %9, i32 0, i32 4)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = load i64, i64* %6
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i64, i64* %6
%25 = sub i64 1024, %24
%26 = trunc i64 %25 to i32
%27 = call i32 @gethostent_r(%struct.hostent* %9, i8* %23, i32 %26, %struct.hostent** %10, i32* %12)
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp ne i32 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @memset(%struct.hostent* %9, i32 0, i32 4)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = load i64, i64* %6
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i64, i64* %6
%38 = sub i64 1024, %37
%39 = trunc i64 %38 to i32
%40 = call i32 @gethostbyname_r(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %struct.hostent* %9, i8* %36, i32 %39, %struct.hostent** %10, i32* %12)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp ne i32 %41, 0
%43 = zext i1 %42 to i64
%44 = select i1 %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %44)
%46 = call i32 @memset(%struct.hostent* %9, i32 0, i32 4)
%47 = load i32, i32* @AF_INET
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = load i64, i64* %6
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = load i64, i64* %6
%52 = sub i64 1024, %51
%53 = trunc i64 %52 to i32
%54 = call i32 @gethostbyname2_r(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %47, %struct.hostent* %9, i8* %50, i32 %53, %struct.hostent** %10, i32* %12)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp ne i32 %55, 0
%57 = zext i1 %56 to i64
%58 = select i1 %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %58)
%60 = call i32 @memset(%struct.hostent* %9, i32 0, i32 4)
%61 = load i32, i32* @AF_INET
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%63 = load i64, i64* %6
%64 = getelementptr inbounds i8, i8* %62, i64 %63
%65 = load i64, i64* %6
%66 = sub i64 1024, %65
%67 = trunc i64 %66 to i32
%68 = call i32 @gethostbyaddr_r(i32* %8, i32 4, i32 %61, %struct.hostent* %9, i8* %64, i32 %67, %struct.hostent** %10, i32* %12)
store i32 %68, i32* %11
%69 = load i32, i32* %11
%70 = icmp ne i32 %69, 0
%71 = zext i1 %70 to i64
%72 = select i1 %70, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %72)
%74 = call i32 @puts(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
br label %75
75: ; preds = %17
%76 = load i64, i64* %6
%77 = add i64 %76, 1
store i64 %77, i64* %6
br label %14
78: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [37 x i8] c"Testing misalignment of %2zi bytes: \00"
@.str.2 = [18 x i8] c"%sgethostent_r() \00"
@.str.3 = [4 x i8] c"!!!\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [10 x i8] c"localhost\00"
@.str.6 = [21 x i8] c"%sgethostbyname_r() \00"
@AF_INET = global i32 0
@.str.7 = [22 x i8] c"%sgethostbyname2_r() \00"
@.str.8 = [21 x i8] c"%sgethostbyaddr_r() \00"
@.str.9 = [4 x i8] c"OK!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca %struct.hostent
%6 = alloca %struct.hostent*
%7 = alloca i32
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
%10 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %10, i32* %4
%11 = bitcast %struct.hostent* %5 to i8*
%12 = bitcast %struct.hostent** %6 to i8*
%13 = bitcast i32* %7 to i8*
br label %14
14: ; preds = %2, %14
%15 = phi i64 [ 0, %2 ], [ %43, %14 ]
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = call i32 @memset(%struct.hostent* %5, i32 0, i32 4)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %15
%19 = trunc i64 %15 to i32
%20 = sub nuw nsw i32 1024, %19
%21 = call i32 @gethostent_r(%struct.hostent* %5, i8* %18, i32 %20, %struct.hostent** %6, i32* %7)
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @memset(%struct.hostent* %5, i32 0, i32 4)
%26 = call i32 @gethostbyname_r(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %struct.hostent* %5, i8* %18, i32 %20, %struct.hostent** %6, i32* %7)
%27 = icmp eq i32 %26, 0
%28 = select i1 %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %28)
%30 = call i32 @memset(%struct.hostent* %5, i32 0, i32 4)
%31 = load i32, i32* @AF_INET
%32 = call i32 @gethostbyname2_r(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %31, %struct.hostent* %5, i8* %18, i32 %20, %struct.hostent** %6, i32* %7)
%33 = icmp eq i32 %32, 0
%34 = select i1 %33, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %34)
%36 = call i32 @memset(%struct.hostent* %5, i32 0, i32 4)
%37 = load i32, i32* @AF_INET
%38 = call i32 @gethostbyaddr_r(i32* %4, i32 4, i32 %37, %struct.hostent* %5, i8* %18, i32 %20, %struct.hostent** %6, i32* %7)
%39 = icmp eq i32 %38, 0
%40 = select i1 %39, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %40)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
%43 = add nuw nsw i64 %15, 1
%44 = icmp eq i64 %43, 16
br i1 %44, label %45, label %14
45: ; preds = %14
ret i32 0
}