#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 int socklen_t;
typedef int /*<<< orphan*/ recv_buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char toupper(char);
int main(int argc, char *argv[]) {
  struct sockaddr_in serv_addr;
  char recv_buf[1024];
  int port, i, ret, sock_fd;
  socklen_t addr_len;
  if (argc != 2) {
    printf("Usage: %s portnumber\n", argv[0]);
    return -1;
  }
  port = atoi(argv[1]);
  memset(&serv_addr, 0, sizeof(struct sockaddr_in));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    goto err;
  }
  if ((bind(sock_fd, (struct sockaddr *)&serv_addr,
            sizeof(struct sockaddr_in))) < 0) {
    perror("bind");
    goto err;
  }
  while (1) {
    addr_len = sizeof(struct sockaddr_in);
    if ((ret = recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0,
                        (struct sockaddr *)&serv_addr, &addr_len)) < 0) {
      perror("recvfrom");
      goto err;
    }
    printf("Receive message from client: %s\n", recv_buf);
    for (i = 0; i < ret; i++) {
      recv_buf[i] = toupper(recv_buf[i]);
    }
    if ((sendto(sock_fd, recv_buf, ret, 0, (struct sockaddr *)&serv_addr,
                sizeof(struct sockaddr_in))) < 0) {
      perror("sendto");
      goto err;
    }
  }
  close(sock_fd);
  return 0;
err:
  close(sock_fd);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1202 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <main+0x208>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x424(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x424(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x430(%rbp)
cmp    $0x0,%eax
jge    1289 <main+0xc9>
lea    0xd97(%rip),%rdi
call   10c0 <perror@plt>
jmp    13b6 <main+0x1f6>
mov    -0x430(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12b8 <main+0xf8>
lea    0xd6f(%rip),%rdi
call   10c0 <perror@plt>
jmp    13b6 <main+0x1f6>
jmp    12bd <main+0xfd>
movl   $0xc,-0x434(%rbp)
mov    -0x430(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x434(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x42c(%rbp)
cmp    $0x0,%eax
jge    130b <main+0x14b>
lea    0xd21(%rip),%rdi
call   10c0 <perror@plt>
jmp    13b6 <main+0x1f6>
lea    -0x420(%rbp),%rsi
lea    0xd12(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x42c(%rbp),%eax
jge    1372 <main+0x1b2>
movslq -0x428(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%edi
ff
call   1030 <toupper@plt>
mov    %al,%cl
movslq -0x428(%rbp),%rax
mov    %cl,-0x420(%rbp,%rax,1)
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    1328 <main+0x168>
mov    -0x430(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x42c(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    $0x0,%rax
jge    13b1 <main+0x1f1>
lea    0xca5(%rip),%rdi
call   10c0 <perror@plt>
jmp    13b6 <main+0x1f6>
jmp    12bd <main+0xfd>
mov    -0x430(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
je     11fc <main+0x3c>
mov    (%rsi),%rsi
lea    0xe20(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dcb(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1265 <main+0xa5>
lea    0xdb6(%rip),%rdi
jmp    131b <main+0x15b>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    1285 <main+0xc5>
lea    0xd9d(%rip),%rdi
jmp    131b <main+0x15b>
lea    0x10(%rsp),%rbx
mov    %rsp,%r15
jmp    12ad <main+0xed>
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
test   %rax,%rax
js     130b <main+0x14b>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   10a0 <recvfrom@plt>
test   %eax,%eax
js     1314 <main+0x154>
mov    %eax,%r14d
lea    0xd4f(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
test   %r14d,%r14d
je     1290 <main+0xd0>
mov    %r14d,%r12d
xor    %r13d,%r13d
nop
movsbl 0x10(%rsp,%r13,1),%edi
call   1030 <toupper@plt>
mov    %al,0x10(%rsp,%r13,1)
add    $0x1,%r13
cmp    %r13,%r12
jne    12f0 <main+0x130>
jmp    1290 <main+0xd0>
lea    0xd3a(%rip),%rdi
jmp    131b <main+0x15b>
lea    0xd07(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    11e5 <main+0x25>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Usage: %s portnumber\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [9 x i8] c"recvfrom\00"
@.str.4 = [33 x i8] c"Receive message from client: %s\0A\00"
@.str.5 = [7 x i8] c"sendto\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %89
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %8
%25 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* %8
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_DGRAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %11
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %20
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %86
41: ; preds = %20
%42 = load i32, i32* %11
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i64 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %86
48: ; preds = %41
br label %49
49: ; preds = %48, %85
store i32 12, i32* %12
%50 = load i32, i32* %11
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%53 = call i32 @recvfrom(i32 %50, i8* %51, i32 1024, i32 0, %struct.sockaddr* %52, i32* %12)
store i32 %53, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %86
57: ; preds = %49
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%59 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %58)
store i32 0, i32* %9
br label %60
60: ; preds = %73, %57
%61 = load i32, i32* %9
%62 = load i32, i32* %10
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %76
64: ; preds = %60
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %66
%68 = load i8, i8* %67
%69 = call signext i8 @toupper(i8 signext %68)
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %71
store i8 %69, i8* %72
br label %73
73: ; preds = %64
%74 = load i32, i32* %9
%75 = add nsw i32 %74, 1
store i32 %75, i32* %9
br label %60
76: ; preds = %60
%77 = load i32, i32* %11
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%79 = load i32, i32* %10
%80 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%81 = call i64 @sendto(i32 %77, i8* %78, i32 %79, i32 0, %struct.sockaddr* %80, i32 12)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %76
%84 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %86
85: ; preds = %76
br label %49
86: ; preds = %83, %55, %46, %39
%87 = load i32, i32* %11
%88 = call i32 @close(i32 %87)
store i32 -1, i32* %3
br label %89
89: ; preds = %86, %15
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Usage: %s portnumber\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [9 x i8] c"recvfrom\00"
@.str.4 = [33 x i8] c"Receive message from client: %s\0A\00"
@.str.5 = [7 x i8] c"sendto\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %55
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %16)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_DGRAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %51, label %29
29: ; preds = %13
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @bind(i32 %27, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %51, label %33
33: ; preds = %29, %48
store i32 12, i32* %5
%34 = call i32 @recvfrom(i32 %27, i8* %7, i32 1024, i32 0, %struct.sockaddr* %30, i32* %5)
%35 = icmp slt i32 %34, 0
br i1 %35, label %51, label %36
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%38 = icmp eq i32 %34, 0
br i1 %38, label %48, label %39
39: ; preds = %36
%40 = zext i32 %34 to i64
br label %41
41: ; preds = %39, %41
%42 = phi i64 [ 0, %39 ], [ %46, %41 ]
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = call signext i8 @toupper(i8 signext %44)
store i8 %45, i8* %43
%46 = add nuw nsw i64 %42, 1
%47 = icmp eq i64 %46, %40
br i1 %47, label %48, label %41
48: ; preds = %41, %36
%49 = call i64 @sendto(i32 %27, i8* %7, i32 %34, i32 0, %struct.sockaddr* %30, i32 12)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %33
51: ; preds = %48, %33, %29, %13
%52 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %29 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %33 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %48 ]
%53 = call i32 @perror(i8* %52)
%54 = call i32 @close(i32 %27)
br label %55
55: ; preds = %51, %10
ret i32 -1
}