#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 */
typedef int /*<<< orphan*/ answer;

/* Variables and functions */
int /*<<< orphan*/ C_IN;
#define HOST_NOT_FOUND 131
#define NO_DATA 130
#define NO_RECOVERY 129
#define TRY_AGAIN 128
int /*<<< orphan*/ T_SOA;
char *basename(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int h_errno;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ res_init();
int res_query(char *, int /*<<< orphan*/, int /*<<< orphan*/, unsigned char *,
              int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int main(int argc, char *argv[]) {
  int rc;
  unsigned char answer[512];
  char *dot;
  if (argc != 2) {
    printf("\nUsage: %s <tld>\n", basename(argv[0]));
    printf("\nWhere\n\t<tld> is the top-level-domain to check\n");
    printf("\nReturns\n");
    printf("\t 0 if <tld> exists\n");
    printf("\t 1 if <tld> does not exist\n");
    printf("\t-1 if an error occured\n");
    printf("\nRequires a configured DNS resolver\n\n");
    exit(-1);
  }
  dot = strchr(argv[1], '.');
  if (dot != NULL && dot[1] != '\0') {
    fprintf(stderr, "TLDs must not contain '.'\n");
    exit(-1);
  }
  if (res_init()) {
    fprintf(stderr, "Unable to initialize DNS resolver\n");
    exit(-1);
  }
  rc = res_query(argv[1], C_IN, T_SOA, answer, sizeof(answer));
  if (rc > 0) {
    printf("TLD \"%s\" exists\n", argv[1]);
    exit(0);
  }
  switch (h_errno) {
  case HOST_NOT_FOUND:
  case NO_DATA:
    printf("TLD \"%s\" does not exist\n", argv[1]);
    exit(1);
    break;
  case TRY_AGAIN:
    fprintf(stderr, "DNS server failed.\n");
    break;
  case NO_RECOVERY:
    fprintf(stderr, "FORMERR, NOTIMP, REFUSED, or other error\n");
    break;
  default:
    fprintf(stderr, "Unknown error: %d\n", h_errno);
  }
  exit(-1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120e <main+0x9e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <basename@plt>
mov    %rax,%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2e,%esi
call   1030 <strchr@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     126a <main+0xfa>
mov    -0x228(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     126a <main+0xfa>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1050 <res_init@plt>
cmp    $0x0,%rax
je     129c <main+0x12c>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    0x2d2c(%rip),%rax
mov    (%rax),%edx
lea    -0x220(%rbp),%rcx
mov    $0x200,%r8d
call   1080 <res_query@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12f2 <main+0x182>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x22c(%rbp)
sub    $0x80,%eax
je     1360 <main+0x1f0>
jmp    1311 <main+0x1a1>
mov    -0x22c(%rbp),%eax
sub    $0x81,%eax
je     137c <main+0x20c>
jmp    1327 <main+0x1b7>
mov    -0x22c(%rbp),%eax
add    $0xffffff7e,%eax
sub    $0x1,%eax
ja     1398 <main+0x228>
jmp    1340 <main+0x1d0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    13b8 <main+0x248>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    13b8 <main+0x248>
mov    0x2c29(%rip),%rax
mov    (%rax),%edi
mov    0x2c30(%rip),%rax
mov    (%rax),%edx
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1202 <main+0x82>
mov    0x8(%rbx),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11ad <main+0x2d>
cmpb   $0x0,0x1(%rax)
jne    127f <main+0xff>
xor    %eax,%eax
call   1060 <res_init@plt>
test   %rax,%rax
jne    126d <main+0xed>
mov    0x8(%rbx),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x200,%r8d
call   1090 <res_query@plt>
test   %eax,%eax
jle    12a0 <main+0x120>
mov    0x8(%rbx),%rsi
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    (%rbx),%rdi
call   1080 <basename@plt>
lea    0xdef(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
lea    0xee1(%rip),%rdi
call   1030 <puts@plt>
lea    0xeed(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
jmp    128f <main+0x10f>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
lea    -0x82(%rdx),%eax
cmp    $0x2,%eax
jb     12d6 <main+0x156>
mov    0x2d0d(%rip),%rax
cmp    $0x80,%edx
je     12f2 <main+0x172>
cmp    $0x81,%edx
jne    12fd <main+0x17d>
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
jmp    128f <main+0x10f>
mov    0x8(%rbx),%rsi
lea    0xd80(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
jmp    128f <main+0x10f>
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"\0AUsage: %s <tld>\0A\00"
@.str.1 = [48 x i8] c"\0AWhere\0A\09<tld> is the top-level-domain to check\0A\00"
@.str.2 = [10 x i8] c"\0AReturns\0A\00"
@.str.3 = [21 x i8] c"\09 0 if <tld> exists\0A\00"
@.str.4 = [29 x i8] c"\09 1 if <tld> does not exist\0A\00"
@.str.5 = [25 x i8] c"\09-1 if an error occured\0A\00"
@.str.6 = [38 x i8] c"\0ARequires a configured DNS resolver\0A\0A\00"
@stderr = global i32 0
@.str.7 = [27 x i8] c"TLDs must not contain '.'\0A\00"
@.str.8 = [35 x i8] c"Unable to initialize DNS resolver\0A\00"
@C_IN = global i32 0
@T_SOA = global i32 0
@.str.9 = [17 x i8] c"TLD \22%s\22 exists\0A\00"
@h_errno = global i32 0
@.str.10 = [25 x i8] c"TLD \22%s\22 does not exist\0A\00"
@.str.11 = [20 x i8] c"DNS server failed.\0A\00"
@.str.12 = [42 x i8] c"FORMERR, NOTIMP, REFUSED, or other error\0A\00"
@.str.13 = [19 x i8] c"Unknown error: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i8* @basename(i8* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i8* @strchr(i8* %27, i8 signext 46)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %41
31: ; preds = %24
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %31, %24
%42 = call i64 (...) @res_init()
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %41
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i32, i32* @C_IN
%53 = load i32, i32* @T_SOA
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%55 = call i32 @res_query(i8* %51, i32 %52, i32 %53, i8* %54, i32 512)
store i32 %55, i32* %6
%56 = load i32, i32* %6
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %48
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i8* %61)
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %48
%65 = load i32, i32* @h_errno
switch i32 %65, label %78 [
i32 131, label %66
i32 130, label %66
i32 128, label %72
i32 129, label %75
]
66: ; preds = %64, %64
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 1
%69 = load i8*, i8** %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0), i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %64
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0))
br label %82
75: ; preds = %64
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.12, i64 0, i64 0))
br label %82
78: ; preds = %64
%79 = load i32, i32* @stderr
%80 = load i32, i32* @h_errno
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0), i32 %80)
br label %82
82: ; preds = %78, %75, %72
%83 = call i32 @exit(i32 -1)
unreachable
}

LLVM IR (-O2):
@.str = [18 x i8] c"\0AUsage: %s <tld>\0A\00"
@stderr = global i32 0
@.str.7 = [27 x i8] c"TLDs must not contain '.'\0A\00"
@.str.8 = [35 x i8] c"Unable to initialize DNS resolver\0A\00"
@C_IN = global i32 0
@T_SOA = global i32 0
@.str.9 = [17 x i8] c"TLD \22%s\22 exists\0A\00"
@h_errno = global i32 0
@.str.10 = [25 x i8] c"TLD \22%s\22 does not exist\0A\00"
@.str.11 = [20 x i8] c"DNS server failed.\0A\00"
@.str.12 = [42 x i8] c"FORMERR, NOTIMP, REFUSED, or other error\0A\00"
@.str.13 = [19 x i8] c"Unknown error: %d\0A\00"
@str = [47 x i8] c"\0AWhere\0A\09<tld> is the top-level-domain to check\00"
@str.14 = [9 x i8] c"\0AReturns\00"
@str.15 = [20 x i8] c"\09 0 if <tld> exists\00"
@str.16 = [28 x i8] c"\09 1 if <tld> does not exist\00"
@str.17 = [24 x i8] c"\09-1 if an error occured\00"
@str.18 = [37 x i8] c"\0ARequires a configured DNS resolver\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %17, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i8* @basename(i8* %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.14, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.15, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.16, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.17, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.18, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i8* @strchr(i8* dereferenceable(1) %19, i8 signext 46)
%21 = icmp eq i8* %20, null
br i1 %21, label %30, label %22
22: ; preds = %17
%23 = getelementptr inbounds i8, i8* %20, i64 1
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %22, %17
%31 = tail call i64 (...) @res_init()
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
%36 = tail call i32 @exit(i32 -1)
unreachable
37: ; preds = %30
%38 = load i8*, i8** %18
%39 = load i32, i32* @C_IN
%40 = load i32, i32* @T_SOA
%41 = call i32 @res_query(i8* %38, i32 %39, i32 %40, i8* %4, i32 512)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load i8*, i8** %18
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i8* %44)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %37
%48 = load i32, i32* @h_errno
switch i32 %48, label %59 [
i32 131, label %49
i32 130, label %49
i32 128, label %53
i32 129, label %56
]
49: ; preds = %47, %47
%50 = load i8*, i8** %18
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0))
br label %62
56: ; preds = %47
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.12, i64 0, i64 0))
br label %62
59: ; preds = %47
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0), i32 %48)
br label %62
62: ; preds = %59, %56, %53
%63 = call i32 @exit(i32 -1)
unreachable
}