#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 in_addr {
  int dummy;
};
struct hostent {
  char *h_name;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ gethostbyname_r(char *, struct hostent *, char *, int,
                           struct hostent **, int *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int main() {
  struct hostent host, *res;
  char buf[2048];
  int fnord;
  if (gethostbyname_r("localhost", &host, buf, 2048, &res, &fnord))
    return 2;
  printf("%s -> %s\n", res->h_name, inet_ntoa(*(struct in_addr *)res->h_addr));
  assert(strcmp(res->h_name, "localhost") == 0);
  assert(strcmp((char *)inet_ntoa(*(struct in_addr *)res->h_addr),
                "127.0.0.1") == 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x820(%rbp),%rdx
lea    0xe90(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x800,%ecx
lea    -0x20(%rbp),%r8
lea    -0x824(%rbp),%r9
call   1040 <gethostbyname_r@plt>
cmp    $0x0,%rax
je     119f <main+0x4f>
movl   $0x2,-0x4(%rbp)
jmp    122c <main+0xdc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x830(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    -0x830(%rbp),%rsi
mov    %rax,%rdx
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe20(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rdi
lea    0xe05(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x820,%rsp
lea    0xea1(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    %rsp,%r8
lea    0xc(%rsp),%r9
mov    $0x800,%ecx
call   1040 <gethostbyname_r@plt>
mov    %rax,%rcx
mov    $0x2,%eax
test   %rcx,%rcx
jne    11fb <main+0xab>
mov    (%rsp),%rax
mov    (%rax),%rbx
mov    0x8(%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
lea    0xe69(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    (%rsp),%rax
mov    (%rax),%rdi
lea    0xe46(%rip),%rsi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    (%rsp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
lea    0xe31(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
xor    %eax,%eax
add    $0x820,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i8*, i64 }
%struct.in_addr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [10 x i8] c"%s -> %s\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.hostent
%3 = alloca %struct.hostent*
%4 = alloca [2048 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%7 = call i64 @gethostbyname_r(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.hostent* %2, i8* %6, i32 2048, %struct.hostent** %3, i32* %5)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 2, i32* %1
br label %40
10: ; preds = %0
%11 = load %struct.hostent*, %struct.hostent** %3
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = load %struct.hostent*, %struct.hostent** %3
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to %struct.in_addr*
%18 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i8* @inet_ntoa(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %20)
%22 = load %struct.hostent*, %struct.hostent** %3
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load %struct.hostent*, %struct.hostent** %3
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr*
%33 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i8* @inet_ntoa(i32 %34)
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
store i32 0, i32* %1
br label %40
40: ; preds = %10, %9
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
%struct.hostent = type { i8*, i64 }
%struct.in_addr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [10 x i8] c"%s -> %s\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
define i32 @main() {
%1 = alloca %struct.hostent
%2 = alloca %struct.hostent*
%3 = alloca [2048 x i8]
%4 = alloca i32
%5 = bitcast %struct.hostent* %1 to i8*
%6 = bitcast %struct.hostent** %2 to i8*
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
%9 = call i64 @gethostbyname_r(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.hostent* %1, i8* %7, i32 2048, %struct.hostent** %2, i32* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %40
11: ; preds = %0
%12 = load %struct.hostent*, %struct.hostent** %2
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to %struct.in_addr*
%18 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %17, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i8* @inet_ntoa(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %20)
%22 = load %struct.hostent*, %struct.hostent** %2
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load %struct.hostent*, %struct.hostent** %2
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i64 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr*
%33 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %32, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i8* @inet_ntoa(i32 %34)
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
br label %40
40: ; preds = %0, %11
%41 = phi i32 [ 0, %11 ], [ 2, %0 ]
ret i32 %41
}