#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*/ to;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int recv(int, void *, size_t, int /*<<< orphan*/);
int send(int, void *, size_t, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_request(const char *host_ip, int port, void *request,
                size_t request_len, void *response_buffer, size_t buffer_len) {
  int s;
  if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    return -1;
  struct sockaddr_in to;
  memset(&to, 0, sizeof(to));
  to.sin_addr.s_addr = inet_addr(host_ip);
  to.sin_family = AF_INET;
  to.sin_port = htons(port);
  if (connect(s, (struct sockaddr *)&to, sizeof(to)) == -1)
    return -1;
  if (send(s, request, request_len, 0) == -1)
    return -1;
  int res_len;
  if ((res_len = recv(s, response_buffer, buffer_len, 0)) == -1)
    return -1;
  close(s);
  return res_len;
}


x86 assembly (clang, -O0):
<udp_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11d7 <udp_request+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <udp_request+0x10f>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x40(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1230 <udp_request+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <udp_request+0x10f>
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1257 <udp_request+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <udp_request+0x10f>
mov    -0x3c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
jne    1281 <udp_request+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <udp_request+0x10f>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r15
mov    %r8,%rbx
mov    %rcx,0x20(%rsp)
mov    %rdx,%r13
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1263 <udp_request+0xe3>
mov    %eax,%ebp
mov    %rbx,0x10(%rsp)
mov    %r15,0x18(%rsp)
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1268 <udp_request+0xe8>
mov    %ebp,%edi
mov    %r13,%rsi
mov    0x20(%rsp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1268 <udp_request+0xe8>
mov    %ebp,%edi
mov    0x10(%rsp),%rsi
mov    0x18(%rsp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1268 <udp_request+0xe8>
mov    %eax,%r14d
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%ebx
jmp    1268 <udp_request+0xe8>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AF_INET = global i32 0
define i32 @udp_request(i8* %0, i32 %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca %struct.sockaddr_in
%16 = alloca i32
store i8* %0, i8** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @IPPROTO_UDP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %14
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %6
store i32 -1, i32* %7
br label %57
23: ; preds = %6
%24 = call i32 @memset(%struct.sockaddr_in* %15, i32 0, i32 12)
%25 = load i8*, i8** %8
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* %9
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %14
%35 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%36 = call i32 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %23
store i32 -1, i32* %7
br label %57
39: ; preds = %23
%40 = load i32, i32* %14
%41 = load i8*, i8** %10
%42 = load i64, i64* %11
%43 = call i32 @send(i32 %40, i8* %41, i64 %42, i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 -1, i32* %7
br label %57
46: ; preds = %39
%47 = load i32, i32* %14
%48 = load i8*, i8** %12
%49 = load i64, i64* %13
%50 = call i32 @recv(i32 %47, i8* %48, i64 %49, i32 0)
store i32 %50, i32* %16
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %46
store i32 -1, i32* %7
br label %57
53: ; preds = %46
%54 = load i32, i32* %14
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %16
store i32 %56, i32* %7
br label %57
57: ; preds = %53, %52, %45, %38, %22
%58 = load i32, i32* %7
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AF_INET = global i32 0
define i32 @udp_request(i8* %0, i32 %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca %struct.sockaddr_in
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %35, label %13
13: ; preds = %6
%14 = bitcast %struct.sockaddr_in* %7 to i8*
%15 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%16 = call i32 @inet_addr(i8* %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%23 = call i32 @connect(i32 %11, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %33, label %25
25: ; preds = %13
%26 = call i32 @send(i32 %11, i8* %2, i64 %3, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %33, label %28
28: ; preds = %25
%29 = call i32 @recv(i32 %11, i8* %4, i64 %5, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @close(i32 %11)
br label %33
33: ; preds = %31, %28, %25, %13
%34 = phi i32 [ -1, %13 ], [ -1, %25 ], [ %29, %31 ], [ -1, %28 ]
br label %35
35: ; preds = %6, %33
%36 = phi i32 [ %34, %33 ], [ -1, %6 ]
ret i32 %36
}