#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 */
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 size_t ssize_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/, int, ...);
scalar_t__ memcmp(char *, char **, size_t);
size_t recv(int, char **, size_t, int /*<<< orphan*/);
size_t sendto(int, char *, size_t, int /*<<< orphan*/, struct sockaddr const *,
              int);
int udp_ping_send(int sock[2], in_addr_t dest_ip, unsigned int port, char *buf,
                  size_t buf_len) {
  struct sockaddr_in server;
  const struct sockaddr *dest_addr = (struct sockaddr *)&server;
  char *sock_buf[buf_len];
  ssize_t r_bytes, s_bytes;
  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  server.sin_addr.s_addr = dest_ip;
  s_bytes = sendto(sock[1], buf, buf_len, 0, dest_addr, sizeof(server));
  if (s_bytes < 0) {
    ksft_print_msg("%d[%u] "
                   "sendto()"
                   ": %m"
                   "\n",
                   getpid(), 641);
    return -1;
  } else if (s_bytes != buf_len) {
    ksft_print_msg("%d[%u] "
                   "send part of the message: %zd/%zu"
                   "\n",
                   getpid(), 644, s_bytes, sizeof(server));
    return -1;
  }
  r_bytes = recv(sock[0], sock_buf, buf_len, 0);
  if (r_bytes < 0) {
    if (errno != EAGAIN)
      ksft_print_msg("%d[%u] "
                     "recv()"
                     ": %m"
                     "\n",
                     getpid(), 651);
    return -1;
  } else if (r_bytes == 0) {
    ksft_print_msg("%d[%u] "
                   "EOF on reply to ping"
                   "\n",
                   getpid(), 654);
    return -1;
  } else if (r_bytes != buf_len || memcmp(buf, sock_buf, buf_len)) {
    ksft_print_msg("%d[%u] "
                   "ping reply packet is corrupted %zd/%zu"
                   "\n",
                   getpid(), 657, r_bytes, buf_len);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<udp_ping_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x40(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jae    1225 <udp_ping_send+0xc5>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdfa(%rip),%rdi
mov    $0x281,%edx
mov    $0x0,%al
call   1080 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1390 <udp_ping_send+0x230>
mov    -0x60(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     126c <udp_ping_send+0x10c>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x60(%rbp),%rcx
lea    0xdce(%rip),%rdi
mov    $0x284,%edx
mov    $0xc,%r8d
mov    $0x0,%al
call   1080 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1390 <udp_ping_send+0x230>
jmp    1271 <udp_ping_send+0x111>
mov    -0x70(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jae    12de <udp_ping_send+0x17e>
mov    0x2d3c(%rip),%rax
mov    (%rax),%rax
mov    0x2d22(%rip),%rcx
cmp    (%rcx),%rax
je     12cb <udp_ping_send+0x16b>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd80(%rip),%rdi
mov    $0x28b,%edx
mov    $0x0,%al
call   1080 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1390 <udp_ping_send+0x230>
cmpq   $0x0,-0x58(%rbp)
jne    1318 <udp_ping_send+0x1b8>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd59(%rip),%rdi
mov    $0x28e,%edx
mov    $0x0,%al
call   1080 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1390 <udp_ping_send+0x230>
mov    -0x58(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1341 <udp_ping_send+0x1e1>
mov    -0x70(%rbp),%rsi
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     1378 <udp_ping_send+0x218>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x58(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xd16(%rip),%rdi
mov    $0x291,%edx
mov    $0x0,%al
call   1080 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1390 <udp_ping_send+0x230>
jmp    137d <udp_ping_send+0x21d>
jmp    1382 <udp_ping_send+0x222>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_ping_send>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %rcx,%r14
mov    %esi,%r13d
mov    %rdi,%r12
mov    %rsp,-0x30(%rbp)
mov    %rsp,%rbx
lea    0xf(,%r8,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    %edx,%edi
call   1050 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    %r13d,-0x40(%rbp)
mov    0x4(%r12),%edi
lea    -0x40(%rbp),%r8
mov    %r14,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    %r15,%rax
jne    121f <udp_ping_send+0xbf>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1262 <udp_ping_send+0x102>
mov    %rax,%r12
cmp    %r15,%rax
jne    1202 <udp_ping_send+0xa2>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <memcmp@plt>
test   %rax,%rax
je     1280 <udp_ping_send+0x120>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
mov    $0x291,%edx
mov    %r12,%rcx
mov    %r15,%r8
jmp    1240 <udp_ping_send+0xe0>
mov    %rax,%r13
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdd0(%rip),%rdi
mov    $0xc,%r8d
mov    %eax,%esi
mov    $0x284,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1080 <ksft_print_msg@plt>
mov    $0xffffffff,%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdba(%rip),%rdi
mov    %eax,%esi
mov    $0x28e,%edx
xor    %eax,%eax
call   1080 <ksft_print_msg@plt>
jmp    1247 <udp_ping_send+0xe7>
xor    %eax,%eax
jmp    124c <udp_ping_send+0xec>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [21 x i8] c"%d[%u] sendto(): %m\0A\00"
@.str.1 = [42 x i8] c"%d[%u] send part of the message: %zd/%zu\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [19 x i8] c"%d[%u] recv(): %m\0A\00"
@.str.3 = [29 x i8] c"%d[%u] EOF on reply to ping\0A\00"
@.str.4 = [47 x i8] c"%d[%u] ping reply packet is corrupted %zd/%zu\0A\00"
define i32 @udp_ping_send(i32* %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%19 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
store %struct.sockaddr* %19, %struct.sockaddr** %13
%20 = load i64, i64* %11
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %14
%22 = alloca i8*, i64 %20
store i64 %20, i64* %15
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* %9
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* %8
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32*, i32** %7
%32 = getelementptr inbounds i32, i32* %31, i64 1
%33 = load i32, i32* %32
%34 = load i8*, i8** %10
%35 = load i64, i64* %11
%36 = load %struct.sockaddr*, %struct.sockaddr** %13
%37 = call i64 @sendto(i32 %33, i8* %34, i64 %35, i32 0, %struct.sockaddr* %36, i32 12)
store i64 %37, i64* %17
%38 = load i64, i64* %17
%39 = icmp ult i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %5
%41 = call i32 (...) @getpid()
%42 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %41, i32 641)
store i32 -1, i32* %6
store i32 1, i32* %18
br label %91
43: ; preds = %5
%44 = load i64, i64* %17
%45 = load i64, i64* %11
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = call i32 (...) @getpid()
%49 = load i64, i64* %17
%50 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %48, i32 644, i64 %49, i64 12)
store i32 -1, i32* %6
store i32 1, i32* %18
br label %91
51: ; preds = %43
br label %52
52: ; preds = %51
%53 = load i32*, i32** %7
%54 = getelementptr inbounds i32, i32* %53, i64 0
%55 = load i32, i32* %54
%56 = load i64, i64* %11
%57 = call i64 @recv(i32 %55, i8** %22, i64 %56, i32 0)
store i64 %57, i64* %16
%58 = load i64, i64* %16
%59 = icmp ult i64 %58, 0
br i1 %59, label %60, label %68
60: ; preds = %52
%61 = load i64, i64* @errno
%62 = load i64, i64* @EAGAIN
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = call i32 (...) @getpid()
%66 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %65, i32 651)
br label %67
67: ; preds = %64, %60
store i32 -1, i32* %6
store i32 1, i32* %18
br label %91
68: ; preds = %52
%69 = load i64, i64* %16
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = call i32 (...) @getpid()
%73 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 654)
store i32 -1, i32* %6
store i32 1, i32* %18
br label %91
74: ; preds = %68
%75 = load i64, i64* %16
%76 = load i64, i64* %11
%77 = icmp ne i64 %75, %76
br i1 %77, label %83, label %78
78: ; preds = %74
%79 = load i8*, i8** %10
%80 = load i64, i64* %11
%81 = call i64 @memcmp(i8* %79, i8** %22, i64 %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %88
83: ; preds = %78, %74
%84 = call i32 (...) @getpid()
%85 = load i64, i64* %16
%86 = load i64, i64* %11
%87 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %84, i32 657, i64 %85, i64 %86)
store i32 -1, i32* %6
store i32 1, i32* %18
br label %91
88: ; preds = %78
br label %89
89: ; preds = %88
br label %90
90: ; preds = %89
store i32 0, i32* %6
store i32 1, i32* %18
br label %91
91: ; preds = %90, %83, %71, %67, %47, %40
%92 = load i8*, i8** %14
%93 = load i32, i32* %6
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str.1 = [42 x i8] c"%d[%u] send part of the message: %zd/%zu\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.3 = [29 x i8] c"%d[%u] EOF on reply to ping\0A\00"
@.str.4 = [47 x i8] c"%d[%u] ping reply packet is corrupted %zd/%zu\0A\00"
define i32 @udp_ping_send(i32* nocapture %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8*, i64 %4
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %2)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %1, i32* %15
%16 = getelementptr inbounds i32, i32* %0, i64 1
%17 = load i32, i32* %16
%18 = call i64 @sendto(i32 %17, i8* %3, i64 %4, i32 0, %struct.sockaddr* %8, i32 12)
%19 = icmp eq i64 %18, %4
br i1 %19, label %23, label %20
20: ; preds = %5
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 644, i64 %18, i64 12)
br label %38
23: ; preds = %5
%24 = load i32, i32* %0
%25 = call i64 @recv(i32 %24, i8** %10, i64 %4, i32 0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 (...) @getpid()
%29 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 654)
br label %38
30: ; preds = %23
%31 = icmp eq i64 %25, %4
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = call i64 @memcmp(i8* %3, i8** %10, i64 %4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32, %30
%36 = call i32 (...) @getpid()
%37 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %36, i32 657, i64 %25, i64 %4)
br label %38
38: ; preds = %32, %35, %27, %20
%39 = phi i32 [ -1, %20 ], [ -1, %27 ], [ -1, %35 ], [ 0, %32 ]
ret i32 %39
}