#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);
char *get_time();
int get_udp_socket(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ printf(char *, 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);
int strlen(char *);
void *udp_server(void *arg) {
  int fd, dt_len;
  char buf[1], *daytime, 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(1300)) == -1) {
      return NULL;
    }
    if (recvfrom(fd, buf, 1, 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);
      daytime = get_time();
      dt_len = strlen(daytime);
      sendto(fd, daytime, dt_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    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x3c(%rbp)
jmp    11d8 <udp_server+0x48>
mov    $0x514,%edi
call   1040 <get_udp_socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <udp_server+0x6c>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x1,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   10a0 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     127c <udp_server+0xec>
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rax
mov    0x2db1(%rip),%rcx
mov    (%rcx),%edi
lea    -0x38(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x48(%rbp),%rsi
lea    0xdb9(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1090 <get_time@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    -0x38(%rbp),%r8
mov    -0x3c(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
jmp    11d3 <udp_server+0x43>

x86 assembly (clang, -O2):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x38(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x30(%rbp)
mov    $0x514,%edi
call   1040 <get_udp_socket@plt>
cmp    $0xffffffff,%eax
je     1286 <udp_server+0xf6>
mov    %eax,%r15d
lea    -0x40(%rbp),%r12
jmp    1214 <udp_server+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    $0x514,%edi
call   1040 <get_udp_socket@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     1286 <udp_server+0xf6>
mov    %r15d,%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %r12,%r8
lea    -0x30(%rbp),%r9
call   10a0 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     11f0 <udp_server+0x60>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xdaf(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <get_time@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%r9d
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1070 <sendto@plt>
jmp    11f0 <udp_server+0x60>
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
@AF_INET6 = global i32 0
@.str = [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 [1 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.sockaddr_in6
%10 = alloca i32
store i8* %0, i8** %2
%11 = load i32, i32* @INET6_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
store i32 4, i32* %10
%15 = load i8*, i8** %2
br label %16
16: ; preds = %43, %1
br label %17
17: ; preds = %16
%18 = call i32 @get_udp_socket(i32 1300)
store i32 %18, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i8*, i8** %7
ret i8* null
22: ; preds = %17
%23 = load i32, i32* %3
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%25 = bitcast %struct.sockaddr_in6* %9 to %struct.sockaddr*
%26 = call i32 @recvfrom(i32 %23, i8* %24, i32 1, i32 0, %struct.sockaddr* %25, i32* %10)
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %43
28: ; preds = %22
%29 = load i32, i32* @AF_INET6
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 0
%31 = trunc i64 %12 to i32
%32 = call i32 @inet_ntop(i32 %29, i32* %30, i8* %14, i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
%34 = call i8* (...) @get_time()
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %4
%37 = load i32, i32* %3
%38 = load i8*, i8** %6
%39 = load i32, i32* %4
%40 = bitcast %struct.sockaddr_in6* %9 to %struct.sockaddr*
%41 = load i32, i32* %10
%42 = call i32 @sendto(i32 %37, i8* %38, i32 %39, i32 0, %struct.sockaddr* %40, i32 %41)
br label %43
43: ; preds = %28, %22
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = call i32 @sleep(i32 5)
br label %16
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i32 0
@.str = [21 x i8] c"UDP request from %s\0A\00"
define noalias i8* @udp_server(i8* nocapture readnone %0) {
%2 = alloca [1 x i8]
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = getelementptr inbounds [1 x i8], [1 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 1300)
%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 %18
17: ; preds = %30, %1
ret i8* null
18: ; preds = %14, %30
%19 = phi i32 [ %12, %14 ], [ %33, %30 ]
%20 = call i32 @recvfrom(i32 %19, i8* %5, i32 1, i32 0, %struct.sockaddr* %15, i32* %4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %30, label %22
22: ; preds = %18
%23 = load i32, i32* @AF_INET6
%24 = call i32 @inet_ntop(i32 %23, i32* %16, i8* %9, i32 %6)
%25 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %9)
%26 = call i8* (...) @get_time()
%27 = call i32 @strlen(i8* %26)
%28 = load i32, i32* %4
%29 = call i32 @sendto(i32 %19, i8* %26, i32 %27, i32 0, %struct.sockaddr* %15, i32 %28)
br label %30
30: ; preds = %22, %18
%31 = call i32 @close(i32 %19)
%32 = call i32 @sleep(i32 5)
%33 = call i32 @get_udp_socket(i32 1300)
%34 = icmp eq i32 %33, -1
br i1 %34, label %17, label %18
}