#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 ERANGE;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int gethostbyname_r(char const *, struct hostent *, char *, size_t,
                    struct hostent **, int *);
char *inet_ntoa(struct in_addr);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, size_t);
char *strerror(int);
int main(void) {
  const char *host = "www.gnu.org";
  struct hostent hostbuf, *hp;
  size_t hstbuflen;
  char *tmphstbuf;
  int res;
  int herr;
  hstbuflen = 16;
  tmphstbuf = malloc(hstbuflen);
  while ((res = gethostbyname_r(host, &hostbuf, tmphstbuf, hstbuflen, &hp,
                                &herr)) == ERANGE) {
    hstbuflen *= 2;
    tmphstbuf = realloc(tmphstbuf, hstbuflen);
  }
  if (res != 0 || hp == NULL) {
    printf("gethostbyname_r failed: %s (errno: %m)\n", strerror(res));
    if (access("/etc/resolv.conf", R_OK)) {
      puts("DNS probably not set up");
      return 0;
    }
    return 1;
  }
  printf("Got: %s %s\n", hp->h_name, inet_ntoa(*(struct in_addr *)hp->h_addr));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x10(%rbp)
movq   $0x10,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%r8
lea    -0x40(%rbp),%r9
call   1050 <gethostbyname_r@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
jne    1200 <main+0x80>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
jmp    11af <main+0x2f>
cmpl   $0x0,-0x3c(%rbp)
jne    1215 <main+0x95>
cmpq   $0x0,-0x28(%rbp)
jne    1271 <main+0xf1>
mov    -0x3c(%rbp),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0xdf6(%rip),%rdi
call   1090 <access@plt>
cmp    $0x0,%rax
je     1265 <main+0xe5>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <main+0x127>
movl   $0x1,-0x4(%rbp)
jmp    12a7 <main+0x127>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x10,%ebx
mov    $0x10,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe59(%rip),%rdi
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    $0x10,%ecx
mov    %rax,%rdx
call   1050 <gethostbyname_r@plt>
mov    0x2e1e(%rip),%r15
cmp    (%r15),%eax
jne    1211 <main+0x91>
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbp
xchg   %ax,%ax
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
mov    %rax,%r14
lea    0xe08(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
mov    %rbp,%r9
call   1050 <gethostbyname_r@plt>
cmp    (%r15),%eax
je     11e0 <main+0x60>
test   %eax,%eax
jne    1245 <main+0xc5>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
je     1245 <main+0xc5>
mov    (%rcx),%rbp
mov    0x8(%rcx),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
lea    0xe29(%rip),%rdi
xor    %ebx,%ebx
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    128c <main+0x10c>
mov    %eax,%edi
call   10a0 <strerror@plt>
lea    0xdb9(%rip),%rdi
xor    %ebx,%ebx
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdi
call   1090 <access@plt>
test   %rax,%rax
je     1287 <main+0x107>
lea    0xdc5(%rip),%rdi
call   1030 <puts@plt>
jmp    128c <main+0x10c>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8*, i64 }
%struct.in_addr = type { i32 }
@.str = [12 x i8] c"www.gnu.org\00"
@ERANGE = global i32 0
@.str.1 = [40 x i8] c"gethostbyname_r failed: %s (errno: %m)\0A\00"
@.str.2 = [17 x i8] c"/etc/resolv.conf\00"
@R_OK = global i32 0
@.str.3 = [24 x i8] c"DNS probably not set up\00"
@.str.4 = [12 x i8] c"Got: %s %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.hostent
%4 = alloca %struct.hostent*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
store i64 16, i64* %5
%9 = load i64, i64* %5
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %6
br label %11
11: ; preds = %18, %0
%12 = load i8*, i8** %2
%13 = load i8*, i8** %6
%14 = load i64, i64* %5
%15 = call i32 @gethostbyname_r(i8* %12, %struct.hostent* %3, i8* %13, i64 %14, %struct.hostent** %4, i32* %8)
store i32 %15, i32* %7
%16 = load i32, i32* @ERANGE
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = load i64, i64* %5
%20 = mul i64 %19, 2
store i64 %20, i64* %5
%21 = load i8*, i8** %6
%22 = load i64, i64* %5
%23 = call i8* @realloc(i8* %21, i64 %22)
store i8* %23, i8** %6
br label %11
24: ; preds = %11
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load %struct.hostent*, %struct.hostent** %4
%29 = icmp eq %struct.hostent* %28, null
br i1 %29, label %30, label %40
30: ; preds = %27, %24
%31 = load i32, i32* %7
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @R_OK
%35 = call i64 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %52
39: ; preds = %30
store i32 1, i32* %1
br label %52
40: ; preds = %27
%41 = load %struct.hostent*, %struct.hostent** %4
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = load %struct.hostent*, %struct.hostent** %4
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 1
%46 = load i64, i64* %45
%47 = inttoptr i64 %46 to %struct.in_addr*
%48 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %43, i8* %50)
store i32 0, i32* %1
br label %52
52: ; preds = %40, %39, %37
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.hostent = type { i8*, i64 }
%struct.in_addr = type { i32 }
@.str = [12 x i8] c"www.gnu.org\00"
@ERANGE = global i32 0
@.str.1 = [40 x i8] c"gethostbyname_r failed: %s (errno: %m)\0A\00"
@.str.2 = [17 x i8] c"/etc/resolv.conf\00"
@R_OK = global i32 0
@.str.3 = [24 x i8] c"DNS probably not set up\00"
@.str.4 = [12 x i8] c"Got: %s %s\0A\00"
define i32 @main() {
%1 = alloca %struct.hostent
%2 = alloca %struct.hostent*
%3 = alloca i32
%4 = bitcast %struct.hostent* %1 to i8*
%5 = bitcast %struct.hostent** %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call dereferenceable_or_null(16) i8* @malloc(i64 16)
%8 = call i32 @gethostbyname_r(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %struct.hostent* %1, i8* %7, i64 16, %struct.hostent** %2, i32* %3)
%9 = load i32, i32* @ERANGE
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %19
11: ; preds = %0, %11
%12 = phi i8* [ %15, %11 ], [ %7, %0 ]
%13 = phi i64 [ %14, %11 ], [ 16, %0 ]
%14 = shl i64 %13, 1
%15 = call i8* @realloc(i8* %12, i64 %14)
%16 = call i32 @gethostbyname_r(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %struct.hostent* %1, i8* %15, i64 %14, %struct.hostent** %2, i32* %3)
%17 = load i32, i32* @ERANGE
%18 = icmp eq i32 %16, %17
br i1 %18, label %11, label %19
19: ; preds = %11, %0
%20 = phi i32 [ %8, %0 ], [ %16, %11 ]
%21 = icmp ne i32 %20, 0
%22 = load %struct.hostent*, %struct.hostent** %2
%23 = icmp eq %struct.hostent* %22, null
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = call i8* @strerror(i32 %20)
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @R_OK
%29 = call i64 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %43, label %31
31: ; preds = %25
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %43
33: ; preds = %19
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 1
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to %struct.in_addr*
%39 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %38, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i8* @inet_ntoa(i32 %40)
%42 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %41)
br label %43
43: ; preds = %25, %33, %31
%44 = phi i32 [ 0, %31 ], [ 0, %33 ], [ 1, %25 ]
ret i32 %44
}