#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ tv;
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int construct_query(int /*<<< orphan*/ *, int, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int sendto(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
           struct sockaddr *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createDNSResponse(char *hostname, char *nameserver, char *dnsResponse) {
  in_addr_t nameserver_addr = inet_addr(nameserver);
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("Creating socket failed: ");
    exit(1);
  }
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 300000;
  if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
    perror("Error");
  }
  uint8_t query[1500];
  int query_len = construct_query(query, 1500, hostname);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(53);
  addr.sin_addr.s_addr = nameserver_addr;
  int send_count =
      sendto(sock, query, query_len, 0, (struct sockaddr *)&addr, sizeof(addr));
  if (send_count < 0) {
    perror("Send failed");
    exit(1);
  }
  int recvCount;
  if ((recvCount = recv(sock, dnsResponse, 1500, 0)) < 0) {
    if ((recvCount = recv(sock, dnsResponse, 1500, 0)) < 0) {
      if ((recvCount = recv(sock, dnsResponse, 1500, 0)) < 0) {
        printf("CATASTROPHIC ERROR!\n");
        return 0;
      }
    }
  }
  return recvCount;
}


x86 assembly (clang, -O0):
<createDNSResponse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x17d0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11ff <createDNSResponse+0x5f>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x493e0,-0x38(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    1248 <createDNSResponse+0xa8>
lea    0xdd6(%rip),%rdi
call   1090 <perror@plt>
lea    -0x17b0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x5dc,%esi
call   10b0 <construct_query@plt>
mov    %eax,-0x17b4(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x17b8(%rbp)
mov    $0x35,%edi
call   1050 <htons@plt>
mov    %eax,-0x17bc(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x17c0(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x17b0(%rbp),%rsi
mov    -0x17b4(%rbp),%edx
lea    -0x17c0(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x17c4(%rbp)
cmpl   $0x0,-0x17c4(%rbp)
jge    12d8 <createDNSResponse+0x138>
lea    0xd56(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x17c8(%rbp)
cmp    $0x0,%eax
jge    1360 <createDNSResponse+0x1c0>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x17c8(%rbp)
cmp    $0x0,%eax
jge    135b <createDNSResponse+0x1bb>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x17c8(%rbp)
cmp    $0x0,%eax
jge    1356 <createDNSResponse+0x1b6>
lea    0xce6(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1369 <createDNSResponse+0x1c9>
jmp    135b <createDNSResponse+0x1bb>
jmp    1360 <createDNSResponse+0x1c0>
mov    -0x17c8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x17d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDNSResponse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1798,%rsp
mov    %rdx,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1080 <inet_addr@plt>
mov    %eax,%r15d
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12e4 <createDNSResponse+0x144>
mov    %eax,%ebp
movq   $0x0,0x18(%rsp)
movl   $0x493e0,0x10(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1050 <setsockopt@plt>
test   %rax,%rax
js     12d3 <createDNSResponse+0x133>
lea    0x20(%rsp),%r13
mov    %r13,%rdi
mov    $0x5dc,%esi
mov    %r12,%rdx
call   10b0 <construct_query@plt>
mov    %eax,%ebx
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x35,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r15d,(%rsp)
mov    %rsp,%r8
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
js     12ed <createDNSResponse+0x14d>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    12c1 <createDNSResponse+0x121>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    12c1 <createDNSResponse+0x121>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    12c1 <createDNSResponse+0x121>
lea    0xd71(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x1798,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3f(%rip),%rdi
call   1090 <perror@plt>
jmp    1223 <createDNSResponse+0x83>
lea    0xd15(%rip),%rdi
jmp    12f4 <createDNSResponse+0x154>
lea    0xd2b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [25 x i8] c"Creating socket failed: \00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.1 = [6 x i8] c"Error\00"
@.str.2 = [12 x i8] c"Send failed\00"
@.str.3 = [21 x i8] c"CATASTROPHIC ERROR!\0A\00"
define i32 @createDNSResponse(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca [1500 x i32]
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%16 = load i8*, i8** %6
%17 = call i32 @inet_addr(i8* %16)
store i32 %17, i32* %8
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %3
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 300000, i32* %28
%29 = load i32, i32* %9
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_RCVTIMEO
%32 = call i64 @setsockopt(i32 %29, i32 %30, i32 %31, %struct.timeval* %10, i32 16)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %26
%37 = getelementptr inbounds [1500 x i32], [1500 x i32]* %11, i64 0, i64 0
%38 = load i8*, i8** %5
%39 = call i32 @construct_query(i32* %37, i32 1500, i8* %38)
store i32 %39, i32* %12
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %40, i32* %41
%42 = call i32 @htons(i32 53)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %42, i32* %43
%44 = load i32, i32* %8
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
store i32 %44, i32* %46
%47 = load i32, i32* %9
%48 = getelementptr inbounds [1500 x i32], [1500 x i32]* %11, i64 0, i64 0
%49 = load i32, i32* %12
%50 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%51 = call i32 @sendto(i32 %47, i32* %48, i32 %49, i32 0, %struct.sockaddr* %50, i32 12)
store i32 %51, i32* %14
%52 = load i32, i32* %14
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %36
%55 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %36
%58 = load i32, i32* %9
%59 = load i8*, i8** %7
%60 = call i32 @recv(i32 %58, i8* %59, i32 1500, i32 0)
store i32 %60, i32* %15
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %76
62: ; preds = %57
%63 = load i32, i32* %9
%64 = load i8*, i8** %7
%65 = call i32 @recv(i32 %63, i8* %64, i32 1500, i32 0)
store i32 %65, i32* %15
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %62
%68 = load i32, i32* %9
%69 = load i8*, i8** %7
%70 = call i32 @recv(i32 %68, i8* %69, i32 1500, i32 0)
store i32 %70, i32* %15
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %78
74: ; preds = %67
br label %75
75: ; preds = %74, %62
br label %76
76: ; preds = %75, %57
%77 = load i32, i32* %15
store i32 %77, i32* %4
br label %78
78: ; preds = %76, %72
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [25 x i8] c"Creating socket failed: \00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.1 = [6 x i8] c"Error\00"
@.str.2 = [12 x i8] c"Send failed\00"
@str = [20 x i8] c"CATASTROPHIC ERROR!\00"
define i32 @createDNSResponse(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.timeval
%5 = alloca [1500 x i32]
%6 = alloca %struct.sockaddr_in
%7 = tail call i32 @inet_addr(i8* %1)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = bitcast %struct.timeval* %4 to i8*
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 300000, i32* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_RCVTIMEO
%21 = call i64 @setsockopt(i32 %10, i32 %19, i32 %20, %struct.timeval* %4, i32 16)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = bitcast [1500 x i32]* %5 to i8*
%27 = getelementptr inbounds [1500 x i32], [1500 x i32]* %5, i64 0, i64 0
%28 = call i32 @construct_query(i32* %27, i32 1500, i8* %0)
%29 = bitcast %struct.sockaddr_in* %6 to i8*
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %30, i32* %31
%32 = call i32 @htons(i32 53)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %7, i32* %34
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = call i32 @sendto(i32 %10, i32* %27, i32 %28, i32 0, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %25
%42 = call i32 @recv(i32 %10, i8* %2, i32 1500, i32 0)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = call i32 @recv(i32 %10, i8* %2, i32 1500, i32 0)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = call i32 @recv(i32 %10, i8* %2, i32 1500, i32 0)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %41, %47, %44, %50
%53 = phi i32 [ 0, %50 ], [ %48, %47 ], [ %45, %44 ], [ %42, %41 ]
ret i32 %53
}