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

/* Variables and functions */
int atoi(char *);
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 *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int udp_recv(int sockfd, struct sockaddr_in *send_addr, int len) {
  int ret = 0;
  int seq = 0;
  char buffer[1024];
  struct sockaddr_in src_addr;
  while (1) {
    memset(buffer, 0, 1024);
    socklen_t length = sizeof(src_addr);
    ret = recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr *)&src_addr,
                   &length);
    if (ret < 0) {
      perror("[udp_recv] recvfrom error");
      return -1;
    }
    printf("server: recv msg %s\n", buffer);
    seq = atoi(buffer);
    printf("server: send back msg %d\n", seq + 1);
    memset(buffer, 0, 1024);
    snprintf(buffer, 1024, "%d", seq + 1);
    ret = sendto(sockfd, buffer, strlen(buffer), 0,
                 (struct sockaddr *)send_addr, (socklen_t)len);
    if (ret < 0) {
      perror("[udp_recv] sendto error");
      return -1;
    }
    printf("server: send back msg %d ok\n\n", seq + 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<udp_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movl   $0x4,-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   1080 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1209 <udp_recv+0x89>
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <udp_recv+0x171>
lea    -0x420(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    $0x400,%esi
lea    0xddc(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d8 <udp_recv+0x158>
lea    0xd85(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <udp_recv+0x171>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a3 <udp_recv+0x23>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edx,%r14d
mov    %rsi,%r13
mov    %edi,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
lea    0xe29(%rip),%rdi
test   %eax,%eax
js     12c0 <udp_recv+0x140>
lea    0x10(%rsp),%rbx
lea    0x4(%rsp),%r15
nopl   0x0(%rax)
lea    0xe23(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
mov    %eax,%r12d
add    $0x1,%r12d
lea    0xe18(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xe0a(%rip),%rdx
mov    %r12d,%ecx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r14d,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
js     12b9 <udp_recv+0x139>
lea    0xdf6(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
mov    %r15,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
jns    11f0 <udp_recv+0x70>
lea    0xd49(%rip),%rdi
jmp    12c0 <udp_recv+0x140>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"[udp_recv] recvfrom error\00"
@.str.1 = [21 x i8] c"server: recv msg %s\0A\00"
@.str.2 = [26 x i8] c"server: send back msg %d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [24 x i8] c"[udp_recv] sendto error\00"
@.str.5 = [30 x i8] c"server: send back msg %d ok\0A\0A\00"
define i32 @udp_recv(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
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 %13
13: ; preds = %3, %50
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 1024)
store i32 4, i32* %12
%16 = load i32, i32* %5
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%18 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%19 = call i32 @recvfrom(i32 %16, i8* %17, i32 1024, i32 0, %struct.sockaddr* %18, i32* %12)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %54
24: ; preds = %13
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 1024)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
%37 = call i32 @snprintf(i8* %34, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %5
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%43 = bitcast %struct.sockaddr_in* %42 to %struct.sockaddr*
%44 = load i32, i32* %7
%45 = call i32 @sendto(i32 %38, i8* %39, i32 %41, i32 0, %struct.sockaddr* %43, i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %24
%49 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %54
50: ; preds = %24
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %52)
br label %13
54: ; preds = %48, %22
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"[udp_recv] recvfrom error\00"
@.str.1 = [21 x i8] c"server: recv msg %s\0A\00"
@.str.2 = [26 x i8] c"server: send back msg %d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [24 x i8] c"[udp_recv] sendto error\00"
@.str.5 = [30 x i8] c"server: send back msg %d ok\0A\0A\00"
define i32 @udp_recv(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = call i32 @memset(i8* %7, i32 0, i32 1024)
%10 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%11 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%12 = call i32 @recvfrom(i32 %0, i8* %7, i32 1024, i32 0, %struct.sockaddr* %11, i32* %6)
%13 = icmp slt i32 %12, 0
br i1 %13, label %31, label %14
14: ; preds = %3
%15 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %16
16: ; preds = %14, %26
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%18 = call i32 @atoi(i8* %7)
%19 = add nsw i32 %18, 1
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 @memset(i8* %7, i32 0, i32 1024)
%22 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%23 = call i32 @strlen(i8* %7)
%24 = call i32 @sendto(i32 %0, i8* %7, i32 %23, i32 0, %struct.sockaddr* %15, i32 %2)
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %16
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%28 = call i32 @memset(i8* %7, i32 0, i32 1024)
store i32 4, i32* %6
%29 = call i32 @recvfrom(i32 %0, i8* %7, i32 1024, i32 0, %struct.sockaddr* %11, i32* %6)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %16
31: ; preds = %16, %26, %3
%32 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), %26 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), %16 ]
%33 = call i32 @perror(i8* %32)
ret i32 -1
}