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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ emul_tft_draw();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
size_t recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
void *event_thread(void *arg) {
  printf("Event thread listening on UDP port %d\n", (5006));
  struct sockaddr_in si_me;
  size_t recv_len;
  char buf[(512)];
  struct sockaddr_in client_sock;
  socklen_t slen = sizeof(client_sock);
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    printf("Error: socket\n");
    exit(EXIT_FAILURE);
  }
  memset((char *)&si_me, 0, sizeof(si_me));
  si_me.sin_family = AF_INET;
  si_me.sin_port = htons((5006));
  si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&si_me, sizeof(si_me)) == -1) {
    printf("Error: could not bind to port %d\n", (5005));
  }
  while (1) {
    if ((recv_len = recvfrom(sock, buf, (512), 0,
                             (struct sockaddr *)&client_sock, &slen)) == -1) {
      printf("Error: recvfrom()\n");
    }
    if (buf[recv_len - 1] == '\n') {
      buf[recv_len - 1] = 0;
      recv_len--;
    }
    printf("[Evt] Received %lu bytes from %s:%d [%s]\n", recv_len,
           inet_ntoa(client_sock.sin_addr), ntohs(client_sock.sin_port), buf);
    if (strcmp("draw", buf) == 0) {
      printf("Drawing UI\n");
      emul_tft_draw();
      printf("---\n");
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<event_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe2a(%rip),%rdi
mov    $0x138e,%esi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xc,-0x234(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x238(%rbp)
cmp    $0xffffffff,%eax
jne    1237 <event_thread+0x77>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x138e,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x238(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a1 <event_thread+0xe1>
lea    0xda1(%rip),%rdi
mov    $0x138d,%esi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12a6 <event_thread+0xe6>
mov    -0x238(%rbp),%edi
lea    -0x220(%rbp),%rsi
lea    -0x230(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x234(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    12e9 <event_thread+0x129>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    131e <event_thread+0x15e>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x220(%rbp,%rax,1)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x248(%rbp)
mov    -0x22c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,-0x23c(%rbp)
mov    -0x230(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x248(%rbp),%rsi
mov    -0x23c(%rbp),%edx
mov    %eax,%ecx
lea    -0x220(%rbp),%r8
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x220(%rbp),%rsi
lea    0xd1e(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    13a9 <event_thread+0x1e9>
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <emul_tft_draw@plt>
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12a6 <event_thread+0xe6>

x86 assembly (clang, -O2):
<event_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
lea    0xe18(%rip),%rdi
mov    $0x138e,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movl   $0xc,0xc(%rsp)
mov    0x2de5(%rip),%r14
mov    (%r14),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1367 <event_thread+0x197>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x138e,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    128a <event_thread+0xba>
lea    0xda9(%rip),%rdi
mov    $0x138d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x30(%rsp),%rbx
lea    0xdb3(%rip),%r13
lea    0xdd6(%rip),%r12
jmp    12da <event_thread+0x10a>
nop
mov    0x24(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %eax,%r15d
mov    0x20(%rsp),%edi
call   10c0 <ntohs@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
mov    %eax,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1310 <event_thread+0x140>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
call   10b0 <recvfrom@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
je     1340 <event_thread+0x170>
cmpb   $0xa,0x2f(%rsp,%r14,1)
jne    12a0 <event_thread+0xd0>
jmp    1358 <event_thread+0x188>
nopl   0x0(%rax,%rax,1)
lea    0xd61(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <emul_tft_draw@plt>
lea    0xd59(%rip),%rdi
call   1040 <puts@plt>
jmp    12da <event_thread+0x10a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xd40(%rip),%rdi
call   1040 <puts@plt>
cmpb   $0xa,0x2f(%rsp,%r14,1)
jne    12a0 <event_thread+0xd0>
add    $0xffffffffffffffff,%r14
movb   $0x0,0x30(%rsp,%r14,1)
jmp    12a0 <event_thread+0xd0>
lea    0xd2b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [39 x i8] c"Event thread listening on UDP port %d\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [15 x i8] c"Error: socket\0A\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [34 x i8] c"Error: could not bind to port %d\0A\00"
@.str.3 = [19 x i8] c"Error: recvfrom()\0A\00"
@.str.4 = [42 x i8] c"[Evt] Received %lu bytes from %s:%d [%s]\0A\00"
@.str.5 = [5 x i8] c"draw\00"
@.str.6 = [12 x i8] c"Drawing UI\0A\00"
@.str.7 = [5 x i8] c"---\0A\00"
define i8* @event_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = alloca [512 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 5006)
store i32 12, i32* %7
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = bitcast %struct.sockaddr_in* %3 to i8*
%21 = call i32 @memset(i8* %20, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 5006)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* %8
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 5005)
br label %36
36: ; preds = %34, %19
br label %37
37: ; preds = %36, %76
%38 = load i32, i32* %8
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%41 = call i64 @recvfrom(i32 %38, i8* %39, i32 512, i32 0, %struct.sockaddr* %40, i32* %7)
store i64 %41, i64* %4
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = load i64, i64* %4
%47 = sub i64 %46, 1
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 10
br i1 %51, label %52, label %58
52: ; preds = %45
%53 = load i64, i64* %4
%54 = sub i64 %53, 1
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %54
store i8 0, i8* %55
%56 = load i64, i64* %4
%57 = add i64 %56, -1
store i64 %57, i64* %4
br label %58
58: ; preds = %52, %45
%59 = load i64, i64* %4
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%61 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @inet_ntoa(i32 %62)
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @ntohs(i32 %65)
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i64 %59, i32 %63, i32 %66, i8* %67)
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%70 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %69)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %58
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%74 = call i32 (...) @emul_tft_draw()
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %76
76: ; preds = %72, %58
br label %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [39 x i8] c"Event thread listening on UDP port %d\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [34 x i8] c"Error: could not bind to port %d\0A\00"
@.str.4 = [42 x i8] c"[Evt] Received %lu bytes from %s:%d [%s]\0A\00"
@.str.5 = [5 x i8] c"draw\00"
@str = [11 x i8] c"Drawing UI\00"
@str.8 = [4 x i8] c"---\00"
@str.9 = [18 x i8] c"Error: recvfrom()\00"
@str.10 = [14 x i8] c"Error: socket\00"
define noalias i8* @event_thread(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [512 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 5006)
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @IPPROTO_UDP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = call i32 @memset(i8* %7, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 5006)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %14, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 5005)
br label %34
34: ; preds = %32, %20
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %38
38: ; preds = %62, %34
%39 = call i64 @recvfrom(i32 %14, i8* %8, i32 512, i32 0, %struct.sockaddr* %35, i32* %5)
%40 = icmp eq i64 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = add i64 %39, -1
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 10
br i1 %47, label %48, label %49
48: ; preds = %43
store i8 0, i8* %45
br label %49
49: ; preds = %48, %43
%50 = phi i64 [ %44, %48 ], [ %39, %43 ]
%51 = load i32, i32* %36
%52 = call i32 @inet_ntoa(i32 %51)
%53 = load i32, i32* %37
%54 = call i32 @ntohs(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i64 %50, i32 %52, i32 %54, i8* %8)
%56 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %49
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%60 = call i32 (...) @emul_tft_draw()
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.8, i64 0, i64 0))
br label %62
62: ; preds = %58, %49
br label %38
}