#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int BUF_SIZE = 1024;
  int port;
  int sockfd;
  struct sockaddr_in listen_addr;
  struct sockaddr_in client_addr;
  int sin_size, recv_size;
  char buffer[BUF_SIZE + 1];
  if (argc != 2) {
    fprintf(stderr, "usage: udp_server port\n");
    exit(1);
  }
  port = atoi(argv[1]);
  if (port <= 0) {
    fprintf(stderr, "error: invalid port\n");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_port = htons(port);
  listen_addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockfd, (struct sockaddr *)&listen_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind");
    exit(1);
  }
  while (1) {
    sin_size = sizeof(struct sockaddr);
    if ((recv_size = recvfrom(sockfd, buffer, BUF_SIZE, 0,
                              (struct sockaddr *)&client_addr, &sin_size)) ==
        0) {
      close(sockfd);
      if (errno != 0) {
        perror("recvfrom");
      }
      break;
    }
    buffer[recv_size] = '\0';
    printf("Received message. Size: %d, message: %s", recv_size, buffer);
    if (sendto(sockfd, buffer, recv_size, 0, (struct sockaddr *)&client_addr,
               sizeof(struct sockaddr)) == -1) {
      perror("sendto");
      close(sockfd);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x400,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121f <main+0x6f>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1258 <main+0xa8>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1293 <main+0xe3>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12e6 <main+0x136>
lea    0xd5d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12eb <main+0x13b>
jmp    12f0 <main+0x140>
mov    -0x58(%rbp),%rsi
movl   $0x4,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jne    1346 <main+0x196>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2c95(%rip),%rax
cmpq   $0x0,(%rax)
je     1341 <main+0x191>
lea    0xcfd(%rip),%rdi
call   10a0 <perror@plt>
jmp    13a3 <main+0x1f3>
mov    -0x58(%rbp),%rdx
movslq -0x40(%rbp),%rax
movb   $0x0,(%rdx,%rax,1)
mov    -0x40(%rbp),%esi
lea    0xce6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x58(%rbp),%rsi
mov    -0x1c(%rbp),%edi
mov    -0x40(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1050 <sendto@plt>
cmp    $0xffffffff,%eax
jne    139e <main+0x1ee>
lea    0xcde(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
jmp    13a3 <main+0x1f3>
jmp    12eb <main+0x13b>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
cmp    $0x2,%edi
jne    1309 <main+0x159>
mov    %rsp,%rbp
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
test   %eax,%eax
jle    131b <main+0x16b>
mov    %eax,%ebx
mov    0x2e01(%rip),%r14
mov    (%r14),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     133a <main+0x18a>
mov    %eax,%r13d
mov    %rbp,0x18(%rsp)
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1343 <main+0x193>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xde7(%rip),%r12
nopl   0x0(%rax,%rax,1)
movl   $0x4,0x4(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
je     12cd <main+0x11d>
mov    %eax,%ebp
cltq
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x4,%r9d
call   1050 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1260 <main+0xb0>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
mov    %r13d,%edi
call   1060 <close@plt>
jmp    12e2 <main+0x132>
mov    %r13d,%edi
call   1060 <close@plt>
mov    0x2ce4(%rip),%rax
cmpq   $0x0,(%rax)
jne    12fb <main+0x14b>
mov    0x18(%rsp),%rsp
xor    %eax,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd37(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e2 <main+0x132>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xce7(%rip),%rsi
jmp    132b <main+0x17b>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xcec(%rip),%rdi
jmp    134a <main+0x19a>
lea    0xcea(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: udp_server port\0A\00"
@.str.1 = [21 x i8] c"error: invalid port\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.3 = [5 x i8] c"bind\00"
@errno = global i64 0
@.str.4 = [9 x i8] c"recvfrom\00"
@.str.5 = [40 x i8] c"Received message. Size: %d, message: %s\00"
@.str.6 = [7 x i8] c"sendto\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i8* @llvm.stacksave()
%10 = alloca [1025 x i8]
%11 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%12 = icmp eq i32 %0, 2
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = tail call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %35, i32* %36
%37 = tail call i32 @htons(i32 %20)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* @INADDR_ANY
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i32 @bind(i32 %29, %struct.sockaddr* %41, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %46, label %44
44: ; preds = %34
%45 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %49
46: ; preds = %34
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %44, %58
store i32 4, i32* %5
%50 = call i32 @recvfrom(i32 %29, i8* %11, i32 1024, i32 0, %struct.sockaddr* %45, i32* %5)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = call i32 @close(i32 %29)
%54 = load i64, i64* @errno
%55 = icmp eq i64 %54, 0
br i1 %55, label %67, label %56
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %67
58: ; preds = %49
%59 = sext i32 %50 to i64
%60 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 %59
store i8 0, i8* %60
%61 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %50, i8* %11)
%62 = call i32 @sendto(i32 %29, i8* %11, i32 %50, i32 0, %struct.sockaddr* %45, i32 4)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %49
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%66 = call i32 @close(i32 %29)
br label %67
67: ; preds = %52, %56, %64
ret i32 0
}