#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*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ trace_log(char *);
int main() {
  int sockfd, query_size = 0, flags;
  socklen_t clientLenght;
  clientLenght = (int)sizeof(struct sockaddr_in);
  char query[1000];
  struct sockaddr_in server;
  struct sockaddr_in client;
  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sockfd < 0) {
    trace_log("ERROR: socket failed!");
  }
  flags = fcntl(sockfd, F_GETFL);
  flags |= O_NONBLOCK;
  fcntl(sockfd, F_SETFL, flags);
  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(34343);
  if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    trace_log("ERROR: bind failed!");
  }
  while (query_size <= 0) {
    query_size = recvfrom(sockfd, query, 1000, 0, (struct sockaddr *)&client,
                          &clientLenght);
    printf("1");
    fflush(stdout);
  }
  printf("\nquery size: %d msg is: - %s -", query_size, query);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0xc,-0x14(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f9 <main+0x59>
lea    0xe0c(%rip),%rdi
call   1080 <trace_log@plt>
mov    -0x8(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x408(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40c(%rbp)
mov    $0x8627,%edi
call   1040 <htons@plt>
mov    %eax,-0x410(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    129f <main+0xff>
lea    0xd7c(%rip),%rdi
call   1080 <trace_log@plt>
jmp    12a4 <main+0x104>
cmpl   $0x0,-0xc(%rbp)
jg     12f3 <main+0x153>
mov    -0x8(%rbp),%edi
lea    -0x400(%rbp),%rsi
lea    -0x420(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x14(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    12a4 <main+0x104>
mov    -0xc(%rbp),%esi
lea    -0x400(%rbp),%rdx
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2e18(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11fd <main+0x4d>
lea    0xe08(%rip),%rdi
call   1090 <trace_log@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2dc9(%rip),%rcx
or     (%rcx),%eax
mov    0x2da0(%rip),%rcx
mov    (%rcx),%esi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x8627,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    1285 <main+0xd5>
lea    0xd96(%rip),%rdi
call   1090 <trace_log@plt>
lea    0x30(%rsp),%r15
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%r13
mov    0x2d15(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   10b0 <recvfrom@plt>
mov    %eax,%ebp
mov    $0x31,%edi
call   1030 <putchar@plt>
mov    (%rbx),%edi
call   10a0 <fflush@plt>
test   %ebp,%ebp
jle    12a0 <main+0xf0>
lea    0xd54(%rip),%rdi
lea    0x30(%rsp),%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [22 x i8] c"ERROR: socket failed!\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [20 x i8] c"ERROR: bind failed!\00"
@.str.2 = [2 x i8] c"1\00"
@stdout = global i32 0
@.str.3 = [31 x i8] c"\0Aquery size: %d msg is: - %s -\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1000 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
store i32 0, i32* %1
store i32 0, i32* %3
store i32 12, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @trace_log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = load i32, i32* %2
%19 = load i32, i32* @F_GETFL
%20 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* @O_NONBLOCK
%22 = load i32, i32* %4
%23 = or i32 %22, %21
store i32 %23, i32* %4
%24 = load i32, i32* %2
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* %4
%27 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %26)
%28 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = call i32 @htons(i32 34343)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* %2
%37 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%38 = call i64 @bind(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %17
%41 = call i32 @trace_log(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %17
br label %43
43: ; preds = %46, %42
%44 = load i32, i32* %3
%45 = icmp sle i32 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = load i32, i32* %2
%48 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%49 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%50 = call i32 @recvfrom(i32 %47, i8* %48, i32 1000, i32 0, %struct.sockaddr* %49, i32* %5)
store i32 %50, i32* %3
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
br label %43
54: ; preds = %43
%55 = load i32, i32* %3
%56 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %55, i8* %56)
%58 = load i32, i32* %2
%59 = call i32 @close(i32 %58)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [22 x i8] c"ERROR: socket failed!\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [20 x i8] c"ERROR: bind failed!\00"
@stdout = global i32 0
@.str.3 = [31 x i8] c"\0Aquery size: %d msg is: - %s -\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
store i32 12, i32* %1
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = tail call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @trace_log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* @F_GETFL
%18 = tail call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %17)
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %19, %18
%21 = load i32, i32* @F_SETFL
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %21, i32 %20)
%23 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = call i32 @htons(i32 34343)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @bind(i32 %12, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %16
%34 = call i32 @trace_log(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %16
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %37
37: ; preds = %35, %37
%38 = call i32 @recvfrom(i32 %12, i8* %6, i32 1000, i32 0, %struct.sockaddr* %36, i32* %1)
%39 = call i32 @putchar(i32 49)
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
%42 = icmp slt i32 %38, 1
br i1 %42, label %37, label %43
43: ; preds = %37
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %6)
%45 = call i32 @close(i32 %12)
ret i32 0
}