#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 */
struct sockaddr_in {
  int sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ recv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
char *inet_ntop(int /*<<< orphan*/, void *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int udp_send(int sockfd, struct sockaddr_in *send_addr, int len) {
  int ret = 0;
  int count = 0;
  char buffer[1024];
  while (count < 100) {
    memset(buffer, 0, 1024);
    snprintf(buffer, 1024, "%d", count);
    printf("we will send msg %d\n", count);
    printf("we want to recv msg %d\n", count + 1);
    ret = sendto(sockfd, buffer, strlen(buffer), 0,
                 (struct sockaddr *)send_addr, len);
    if (ret == -1) {
      perror("[udp_send] sendto error");
      return -1;
    }
    memset(buffer, 0, 1024);
    struct sockaddr_in recv_addr;
    int length = sizeof(recv_addr);
    ret = recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr *)&recv_addr,
                   (socklen_t *)&length);
    if (ret < 0) {
      perror("[udp_send] recvfrom error");
      return -1;
    }
    char temp[1024] = {0};
    const char *ptemp =
        inet_ntop(AF_INET, (void *)&(recv_addr.sin_addr), temp, 1024);
    printf("we1 have recv %s from %s:%d\n\n", buffer, ptemp,
           recv_addr.sin_port);
    count++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<udp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    133b <udp_send+0x1bb>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x400,%esi
lea    0xe2a(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x1c(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x844(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x844(%rbp),%edi
mov    -0x840(%rbp),%rsi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
xor    %ecx,%ecx
call   1080 <sendto@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1266 <udp_send+0xe6>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <udp_send+0x1c2>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
movl   $0x8,-0x42c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x428(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x42c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12cc <udp_send+0x14c>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <udp_send+0x1c2>
lea    -0x830(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    -0x428(%rbp),%rsi
add    $0x4,%rsi
lea    -0x830(%rbp),%rdx
mov    $0x400,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x838(%rbp)
lea    -0x420(%rbp),%rsi
mov    -0x838(%rbp),%rdx
mov    -0x428(%rbp),%ecx
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a3 <udp_send+0x23>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %edx,0x8(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%r13d
mov    $0x1,%ebx
lea    0x420(%rsp),%r15
lea    0x20(%rsp),%r12
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    -0x1(%rbx),%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
mov    $0x400,%esi
lea    0xe1f(%rip),%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
lea    0xe14(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe19(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    0x18(%rsp),%r8
mov    0x8(%rsp),%r9d
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
je     12cc <udp_send+0x14c>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
movl   $0x8,0xc(%rsp)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
call   1090 <recvfrom@plt>
test   %eax,%eax
js     12d5 <udp_send+0x155>
mov    $0x400,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rsi
mov    %r12,%rdx
mov    $0x400,%ecx
call   1030 <inet_ntop@plt>
mov    0x10(%rsp),%ecx
lea    0xdc4(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebx
cmp    $0x65,%ebx
jne    11c0 <udp_send+0x40>
mov    %r14d,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5d(%rip),%rdi
jmp    12dc <udp_send+0x15c>
lea    0xd6c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12b7 <udp_send+0x137>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%d\00"
@.str.1 = [21 x i8] c"we will send msg %d\0A\00"
@.str.2 = [24 x i8] c"we want to recv msg %d\0A\00"
@.str.3 = [24 x i8] c"[udp_send] sendto error\00"
@.str.4 = [26 x i8] c"[udp_send] recvfrom error\00"
@AF_INET = global i32 0
@.str.5 = [30 x i8] c"we1 have recv %s from %s:%d\0A\0A\00"
define i32 @udp_send(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca [1024 x i8]
%14 = alloca i8*
store i32 %0, i32* %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %15
15: ; preds = %52, %3
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 100
br i1 %17, label %18, label %66
18: ; preds = %15
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 1024)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%22 = load i32, i32* %9
%23 = call i32 @snprintf(i8* %21, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %9
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %5
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%34 = bitcast %struct.sockaddr_in* %33 to %struct.sockaddr*
%35 = load i32, i32* %7
%36 = call i32 @sendto(i32 %29, i8* %30, i32 %32, i32 0, %struct.sockaddr* %34, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %18
%40 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %67
41: ; preds = %18
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%43 = call i32 @memset(i8* %42, i32 0, i32 1024)
store i32 8, i32* %12
%44 = load i32, i32* %5
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%47 = call i32 @recvfrom(i32 %44, i8* %45, i32 1024, i32 0, %struct.sockaddr* %46, i32* %12)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %67
52: ; preds = %41
%53 = bitcast [1024 x i8]* %13 to i8*
%54 = load i32, i32* @AF_INET
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%56 = bitcast i32* %55 to i8*
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%58 = call i8* @inet_ntop(i32 %54, i8* %56, i8* %57, i32 1024)
store i8* %58, i8** %14
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = load i8*, i8** %14
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %59, i8* %60, i32 %62)
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %15
66: ; preds = %15
store i32 0, i32* %4
br label %67
67: ; preds = %66, %50, %39
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%d\00"
@.str.1 = [21 x i8] c"we will send msg %d\0A\00"
@.str.2 = [24 x i8] c"we want to recv msg %d\0A\00"
@.str.3 = [24 x i8] c"[udp_send] sendto error\00"
@.str.4 = [26 x i8] c"[udp_send] recvfrom error\00"
@AF_INET = global i32 0
@.str.5 = [30 x i8] c"we1 have recv %s from %s:%d\0A\0A\00"
define i32 @udp_send(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%15 = bitcast i32* %14 to i8*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
br label %17
17: ; preds = %3, %35
%18 = phi i32 [ 0, %3 ], [ %22, %35 ]
%19 = call i32 @memset(i8* %8, i32 0, i32 1024)
%20 = call i32 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%22 = add nuw nsw i32 %18, 1
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = call i32 @strlen(i8* %8)
%25 = call i32 @sendto(i32 %0, i8* %8, i32 %24, i32 0, %struct.sockaddr* %9, i32 %2)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %41
29: ; preds = %17
%30 = call i32 @memset(i8* %8, i32 0, i32 1024)
store i32 8, i32* %6
%31 = call i32 @recvfrom(i32 %0, i8* %8, i32 1024, i32 0, %struct.sockaddr* %12, i32* %6)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %41
35: ; preds = %29
%36 = load i32, i32* @AF_INET
%37 = call i8* @inet_ntop(i32 %36, i8* %15, i8* %13, i32 1024)
%38 = load i32, i32* %16
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %8, i8* %37, i32 %38)
%40 = icmp eq i32 %22, 100
br i1 %40, label %41, label %17
41: ; preds = %35, %33, %27
%42 = phi i32 [ -1, %27 ], [ -1, %33 ], [ 0, %35 ]
ret i32 %42
}