#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ str;
typedef int socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Bind(int, struct sockaddr *, int);
int /*<<< orphan*/ INADDR_ANY;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_DGRAM;
int Socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perr_exit(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
char toupper(char);
void udp_server() {
  struct sockaddr_in servaddr, cliaddr;
  socklen_t cliaddr_len;
  int sockfd;
  char buf[80];
  char str[INET_ADDRSTRLEN];
  int i, n;
  sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8000);
  Bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  printf("Accepting connections ...\n");
  while (1) {
    cliaddr_len = sizeof(cliaddr);
    n = recvfrom(sockfd, buf, 80, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
    if (n == -1)
      perr_exit("recvfrom error");
    printf("received from %s at PORT %d\n",
           inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
           ntohs(cliaddr.sin_port));
    for (i = 0; i < n; i++)
      buf[i] = toupper(buf[i]);
    n = sendto(sockfd, buf, n, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
    if (n == -1)
      perr_exit("sendto error");
  }
}


x86 assembly (clang, -O0):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xa8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <Socket@plt>
mov    %eax,-0x28(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <Bind@plt>
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    127f <udp_server+0xbf>
movl   $0xc,-0x24(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x80(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x50,%edx
xor    %ecx,%ecx
lea    -0x24(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0xffffffff,-0x98(%rbp)
jne    12c0 <udp_server+0x100>
lea    0xd60(%rip),%rdi
call   10e0 <perr_exit@plt>
mov    -0xa0(%rbp),%rdx
mov    -0xa8(%rbp),%rax
mov    0x2d13(%rip),%rcx
mov    (%rcx),%edi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0xb0(%rbp)
mov    -0x20(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x94(%rbp)
mov    -0x94(%rbp),%eax
cmp    -0x98(%rbp),%eax
jge    135a <udp_server+0x19a>
movslq -0x94(%rbp),%rax
movsbl -0x80(%rbp,%rax,1),%edi
call   1040 <toupper@plt>
mov    %al,%cl
movslq -0x94(%rbp),%rax
mov    %cl,-0x80(%rbp,%rax,1)
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
jmp    1316 <udp_server+0x156>
mov    -0x28(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x98(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0xffffffff,-0x98(%rbp)
jne    1397 <udp_server+0x1d7>
lea    0xcb5(%rip),%rdi
call   10e0 <perr_exit@plt>
jmp    127a <udp_server+0xba>

x86 assembly (clang, -O2):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rax,-0x50(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    0x2ddb(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <Socket@plt>
mov    %eax,%r12d
lea    -0x40(%rbp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x1f40,%edi
call   1060 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10c0 <Bind@plt>
lea    0xdcc(%rip),%rdi
call   1050 <puts@plt>
lea    -0xb0(%rbp),%r15
lea    -0x60(%rbp),%rbx
mov    %r12d,-0x2c(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movl   $0xc,-0x30(%rbp)
mov    %r12d,%edi
mov    %r15,%r12
mov    %r15,%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
mov    %rbx,%r13
mov    %rbx,%r8
lea    -0x30(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    12ca <udp_server+0xfa>
lea    0xd3b(%rip),%rdi
call   10f0 <perr_exit@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    -0x5c(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    %rax,%r15
mov    -0x60(%rbp),%edi
call   10b0 <ntohs@plt>
lea    0xd19(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
test   %r14d,%r14d
jle    132d <udp_server+0x15d>
mov    %r14d,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl -0xb0(%rbp,%rbx,1),%edi
ff
call   1040 <toupper@plt>
mov    %al,-0xb0(%rbp,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1310 <udp_server+0x140>
mov    -0x2c(%rbp),%edi
mov    %r12,%r15
mov    %edi,%r12d
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %r13,%rbx
mov    %r13,%r8
mov    $0xc,%r9d
call   1090 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1290 <udp_server+0xc0>
lea    0xccd(%rip),%rdi
call   10f0 <perr_exit@plt>
jmp    1290 <udp_server+0xc0>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [27 x i8] c"Accepting connections ...\0A\00"
@.str.1 = [15 x i8] c"recvfrom error\00"
@.str.2 = [29 x i8] c"received from %s at PORT %d\0A\00"
@.str.3 = [13 x i8] c"sendto error\00"
define void @udp_server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = load i32, i32* @INET_ADDRSTRLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @Socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %4
%17 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 8000)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%28 = call i32 @Bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %75, %0
br label %31
31: ; preds = %30
store i32 12, i32* %3
%32 = load i32, i32* %4
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%34 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%35 = call i32 @recvfrom(i32 %32, i8* %33, i32 80, i32 0, %struct.sockaddr* %34, i32* %3)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @perr_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %31
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%43 = trunc i64 %11 to i32
%44 = call i8* @inet_ntop(i32 %41, %struct.TYPE_2__* %42, i8* %13, i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %44, i32 %47)
store i32 0, i32* %8
br label %49
49: ; preds = %62, %40
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %65
53: ; preds = %49
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = call signext i8 @toupper(i8 signext %57)
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 %60
store i8 %58, i8* %61
br label %62
62: ; preds = %53
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
br label %49
65: ; preds = %49
%66 = load i32, i32* %4
%67 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%68 = load i32, i32* %9
%69 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%70 = call i32 @sendto(i32 %66, i8* %67, i32 %68, i32 0, %struct.sockaddr* %69, i32 12)
store i32 %70, i32* %9
%71 = load i32, i32* %9
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %65
%74 = call i32 @perr_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %75
75: ; preds = %73, %65
br label %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [15 x i8] c"recvfrom error\00"
@.str.2 = [29 x i8] c"received from %s at PORT %d\0A\00"
@.str.3 = [13 x i8] c"sendto error\00"
@str = [26 x i8] c"Accepting connections ...\00"
define void @udp_server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @INET_ADDRSTRLEN
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = tail call i32 @Socket(i32 %12, i32 %13, i32 0)
%15 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 8000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @Bind(i32 %14, %struct.sockaddr* %23, i32 12)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %29
29: ; preds = %55, %0
store i32 12, i32* %3
%30 = call i32 @recvfrom(i32 %14, i8* %8, i32 80, i32 0, %struct.sockaddr* %26, i32* %3)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perr_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* @AF_INET
%36 = call i8* @inet_ntop(i32 %35, %struct.TYPE_2__* %27, i8* %11, i32 %9)
%37 = load i32, i32* %28
%38 = call i32 @ntohs(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %36, i32 %38)
%40 = icmp sgt i32 %30, 0
br i1 %40, label %41, label %50
41: ; preds = %34
%42 = zext i32 %30 to i64
br label %43
43: ; preds = %41, %43
%44 = phi i64 [ 0, %41 ], [ %48, %43 ]
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = call signext i8 @toupper(i8 signext %46)
store i8 %47, i8* %45
%48 = add nuw nsw i64 %44, 1
%49 = icmp eq i64 %48, %42
br i1 %49, label %50, label %43
50: ; preds = %43, %34
%51 = call i32 @sendto(i32 %14, i8* %8, i32 %30, i32 0, %struct.sockaddr* %26, i32 12)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @perr_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
br label %29
}