#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*/ uint8_t;
typedef int /*<<< orphan*/ timeout;
struct timeval {
  int tv_sec;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ close(int);
scalar_t__ getaddrinfo(char *, char *, int /*<<< orphan*/, struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ recvfrom(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ sendto(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                  int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
ssize_t send_request(char *server_addr, uint8_t *request, uint8_t *response) {
  struct addrinfo *addr;
  if (getaddrinfo(server_addr, "123", 0, &addr)) {
    perror("send_request/getaddrinfo");
  }
  int sock = socket(addr->ai_family, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    perror("send_request/socket");
  }
  struct timeval timeout = {.tv_sec = 3000};
  setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  if (sendto(sock, request, 48, 0, addr->ai_addr, addr->ai_addrlen) < 0) {
    perror("send_request/sendto");
  }
  ssize_t size = 0;
  if ((size = recvfrom(sock, response, 48, 0, addr->ai_addr,
                       &addr->ai_addrlen)) < 0) {
    perror("send_request/recvfrom");
  }
  close(sock);
  return size;
}


x86 assembly (clang, -O0):
<send_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe71(%rip),%rsi
xor    %edx,%edx
lea    -0x20(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11b0 <send_request+0x40>
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e7 <send_request+0x77>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
mov    0xe6f(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x20(%rbp),%rax
mov    (%rax),%r9d
mov    $0x30,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
cmp    $0x0,%rax
jge    124c <send_request+0xdc>
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x20(%rbp),%r9
mov    $0x30,%edx
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    128a <send_request+0x11a>
lea    0xdc0(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_request>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0xe7b(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    1240 <send_request+0xd0>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1251 <send_request+0xe1>
movl   $0xbb8,(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%r9d
mov    0x4(%rax),%r8d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x30,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
test   %rax,%rax
js     1262 <send_request+0xf2>
mov    0x8(%rsp),%r9
mov    0x4(%r9),%r8d
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x30,%edx
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1270 <send_request+0x100>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
jmp    119a <send_request+0x2a>
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
jmp    11c3 <send_request+0x53>
lea    0xdc8(%rip),%rdi
call   1070 <perror@plt>
jmp    120e <send_request+0x9e>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
jmp    122e <send_request+0xbe>

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.addrinfo = type { i32, i32, i32 }
@.str = [4 x i8] c"123\00"
@.str.1 = [25 x i8] c"send_request/getaddrinfo\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.2 = [20 x i8] c"send_request/socket\00"
@__const.send_request.timeout = %struct.timeval { i32 3000 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.3 = [20 x i8] c"send_request/sendto\00"
@.str.4 = [22 x i8] c"send_request/recvfrom\00"
define i32 @send_request(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%11 = load i8*, i8** %4
%12 = call i64 @getaddrinfo(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 0, %struct.addrinfo** %7)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load %struct.addrinfo*, %struct.addrinfo** %7
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 2
%19 = load i32, i32* %18
%20 = load i32, i32* @SOCK_DGRAM
%21 = load i32, i32* @IPPROTO_UDP
%22 = call i32 @socket(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %16
%28 = bitcast %struct.timeval* %9 to i8*
%29 = load i32, i32* %8
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_RCVTIMEO
%32 = call i32 @setsockopt(i32 %29, i32 %30, i32 %31, %struct.timeval* %9, i32 4)
%33 = load i32, i32* %8
%34 = load i32*, i32** %5
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %7
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @sendto(i32 %33, i32* %34, i32 48, i32 0, i32 %37, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %27
%44 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %27
store i32 0, i32* %10
%46 = load i32, i32* %8
%47 = load i32*, i32** %6
%48 = load %struct.addrinfo*, %struct.addrinfo** %7
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %7
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = call i32 @recvfrom(i32 %46, i32* %47, i32 48, i32 0, i32 %50, i32* %52)
store i32 %53, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %45
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %10
ret i32 %60
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
%struct.timeval = type { i32 }
@.str = [4 x i8] c"123\00"
@.str.1 = [25 x i8] c"send_request/getaddrinfo\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.2 = [20 x i8] c"send_request/socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.3 = [20 x i8] c"send_request/sendto\00"
@.str.4 = [22 x i8] c"send_request/recvfrom\00"
define i32 @send_request(i8* %0, i32* %1, i32* %2) {
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.timeval
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i64 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 0, %struct.addrinfo** %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = load %struct.addrinfo*, %struct.addrinfo** %4
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i64 0, i32 2
%14 = load i32, i32* %13
%15 = load i32, i32* @SOCK_DGRAM
%16 = load i32, i32* @IPPROTO_UDP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = bitcast %struct.timeval* %5 to i8*
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 3000, i32* %23
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_RCVTIMEO
%26 = call i32 @setsockopt(i32 %17, i32 %24, i32 %25, %struct.timeval* %5, i32 4)
%27 = load %struct.addrinfo*, %struct.addrinfo** %4
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @sendto(i32 %17, i32* %1, i32 48, i32 0, i32 %29, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %21
%37 = load %struct.addrinfo*, %struct.addrinfo** %4
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%41 = call i32 @recvfrom(i32 %17, i32* %2, i32 48, i32 0, i32 %39, i32* %40)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = call i32 @close(i32 %17)
ret i32 %41
}