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

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ close(int);
int get_udp_socket(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ sleep(int);
void *udp_server(void *arg) {
  int fd, recv_len;
  char buf[1024], src_ip[INET6_ADDRSTRLEN];
  struct sockaddr_in6 src_addr;
  socklen_t src_addr_len = sizeof(src_addr);
  (void)arg;
  while (1) {
    if ((fd = get_udp_socket(7000)) == -1) {
      printf("Failed to initialise UDP socket on port %d\n", 7000);
      return NULL;
    }
    if ((recv_len = recvfrom(fd, buf, 1024, 0, (struct sockaddr *)&src_addr,
                             &src_addr_len)) != -1) {
      inet_ntop(AF_INET6, &src_addr.sin6_addr, src_ip, sizeof(src_ip));
      printf("UDP request from %s\n", src_ip);
      sendto(fd, buf, recv_len, 0, (struct sockaddr *)&src_addr, src_addr_len);
    }
    close(fd);
    sleep(5);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x440(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x418(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x438(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x420(%rbp)
movl   $0x4,-0x42c(%rbp)
jmp    11ca <udp_server+0x5a>
mov    $0x1b58,%edi
call   1040 <get_udp_socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1204 <udp_server+0x94>
lea    0xe19(%rip),%rdi
mov    $0x1b58,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x418(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
lea    -0x428(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x42c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     1290 <udp_server+0x120>
mov    -0x438(%rbp),%rdx
mov    -0x440(%rbp),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%edi
lea    -0x428(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x438(%rbp),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    -0x428(%rbp),%r8
mov    -0x42c(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
jmp    11c5 <udp_server+0x55>

x86 assembly (clang, -O2):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsp,-0x38(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x2c(%rbp)
mov    $0x1b58,%edi
call   1040 <get_udp_socket@plt>
cmp    $0xffffffff,%eax
je     125a <udp_server+0xea>
mov    %eax,%r13d
lea    -0x450(%rbp),%r15
lea    -0x48(%rbp),%r12
jmp    11f4 <udp_server+0x84>
mov    %r13d,%edi
call   1070 <close@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    $0x1b58,%edi
call   1040 <get_udp_socket@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     125a <udp_server+0xea>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r12,%r8
lea    -0x2c(%rbp),%r9
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     11d0 <udp_server+0x60>
mov    %eax,%r14d
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    -0x40(%rbp),%rcx
call   1030 <inet_ntop@plt>
lea    0xdf8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x2c(%rbp),%r9d
mov    %r13d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1060 <sendto@plt>
jmp    11d0 <udp_server+0x60>
lea    0xd9f(%rip),%rdi
mov    $0x1b58,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x38(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [44 x i8] c"Failed to initialise UDP socket on port %d\0A\00"
@AF_INET6 = global i32 0
@.str.1 = [21 x i8] c"UDP request from %s\0A\00"
define i8* @udp_server(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.sockaddr_in6
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i32, i32* @INET6_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
store i32 4, i32* %9
%14 = load i8*, i8** %2
br label %15
15: ; preds = %40, %1
br label %16
16: ; preds = %15
%17 = call i32 @get_udp_socket(i32 7000)
store i32 %17, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 7000)
%21 = load i8*, i8** %6
ret i8* null
22: ; preds = %16
%23 = load i32, i32* %3
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = bitcast %struct.sockaddr_in6* %8 to %struct.sockaddr*
%26 = call i32 @recvfrom(i32 %23, i8* %24, i32 1024, i32 0, %struct.sockaddr* %25, i32* %9)
store i32 %26, i32* %4
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %40
28: ; preds = %22
%29 = load i32, i32* @AF_INET6
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 0
%31 = trunc i64 %11 to i32
%32 = call i32 @inet_ntop(i32 %29, i32* %30, i8* %13, i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%34 = load i32, i32* %3
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = load i32, i32* %4
%37 = bitcast %struct.sockaddr_in6* %8 to %struct.sockaddr*
%38 = load i32, i32* %9
%39 = call i32 @sendto(i32 %34, i8* %35, i32 %36, i32 0, %struct.sockaddr* %37, i32 %38)
br label %40
40: ; preds = %28, %22
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = call i32 @sleep(i32 5)
br label %15
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [44 x i8] c"Failed to initialise UDP socket on port %d\0A\00"
@AF_INET6 = global i32 0
@.str.1 = [21 x i8] c"UDP request from %s\0A\00"
define noalias i8* @udp_server(i8* nocapture readnone %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = load i32, i32* @INET6_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = bitcast %struct.sockaddr_in6* %3 to i8*
%11 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%12 = call i32 @get_udp_socket(i32 7000)
%13 = icmp eq i32 %12, -1
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%16 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
br label %19
17: ; preds = %29, %1
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 7000)
ret i8* null
19: ; preds = %14, %29
%20 = phi i32 [ %12, %14 ], [ %32, %29 ]
%21 = call i32 @recvfrom(i32 %20, i8* %5, i32 1024, i32 0, %struct.sockaddr* %15, i32* %4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = load i32, i32* @AF_INET6
%25 = call i32 @inet_ntop(i32 %24, i32* %16, i8* %9, i32 %6)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%27 = load i32, i32* %4
%28 = call i32 @sendto(i32 %20, i8* %5, i32 %21, i32 0, %struct.sockaddr* %15, i32 %27)
br label %29
29: ; preds = %23, %19
%30 = call i32 @close(i32 %20)
%31 = call i32 @sleep(i32 5)
%32 = call i32 @get_udp_socket(i32 7000)
%33 = icmp eq i32 %32, -1
br i1 %33, label %17, label %19
}