#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*/ servidor;
typedef int /*<<< orphan*/ cliente;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in servidor, cliente;
  int s, i;
  socklen_t slen_s = sizeof(servidor);
  socklen_t slen_c = sizeof(cliente);
  char buf[512];
  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    printf("ERRO NA CRICAO DO SOCKET");
    return 1;
  }
  memset((char *)&servidor, 0, sizeof(servidor));
  servidor.sin_family = AF_INET;
  servidor.sin_port = htons(4444);
  printf("Setando porta para: %d\n", 4444);
  servidor.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(s, (struct sockaddr *)&servidor, sizeof(servidor)) == -1) {
    printf("ERRO NO BINDING");
    return 1;
  }
  for (i = 0; i < 10; i++) {
    if (recvfrom(s, buf, 512, 0, (struct sockaddr *)&cliente, &slen_c) == -1) {
      printf("ERRO AO RECEBER DADO");
      return 1;
    }
    printf("Pacote recebido de %s:%d\nDado: %s\n", inet_ntoa(cliente.sin_addr),
           ntohs(cliente.sin_port), buf);
  }
  sendto(s, "Obrigado!", 10, 0, (struct sockaddr *)&cliente, slen_c);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x2c(%rbp)
movl   $0xc,-0x30(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <main+0x66>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1a1>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x115c,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
lea    0xde3(%rip),%rdi
mov    $0x115c,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1287 <main+0xe7>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1a1>
movl   $0x0,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
jge    131c <main+0x17c>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x30(%rbp),%r9
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    12d9 <main+0x139>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1a1>
mov    -0x1c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x238(%rbp)
mov    -0x20(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x238(%rbp),%rsi
mov    %eax,%edx
lea    -0x230(%rbp),%rcx
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    128e <main+0xee>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%r8
mov    -0x30(%rbp),%r9d
lea    0xd4b(%rip),%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1080 <sendto@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2e28(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e2 <main+0x142>
mov    %eax,%r13d
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    $0x115c,%edi
call   1040 <htons@plt>
mov    %eax,0x20(%rsp)
lea    0xe03(%rip),%rdi
mov    $0x115c,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x24(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12eb <main+0x14b>
mov    $0xa,%r12d
lea    0x30(%rsp),%rbx
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     12f4 <main+0x154>
mov    0x14(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xdad(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%r12d
jne    1270 <main+0xd0>
mov    0xc(%rsp),%r9d
lea    0xdaf(%rip),%rsi
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    %r13d,%edi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1080 <sendto@plt>
jmp    1307 <main+0x167>
lea    0xd17(%rip),%rdi
jmp    12fb <main+0x15b>
lea    0xd3f(%rip),%rdi
jmp    12fb <main+0x15b>
lea    0xd46(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x238,%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 = [25 x i8] c"ERRO NA CRICAO DO SOCKET\00"
@.str.1 = [24 x i8] c"Setando porta para: %d\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [16 x i8] c"ERRO NO BINDING\00"
@.str.3 = [21 x i8] c"ERRO AO RECEBER DADO\00"
@.str.4 = [35 x i8] c"Pacote recebido de %s:%d\0ADado: %s\0A\00"
@.str.5 = [10 x i8] c"Obrigado!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
store i32 0, i32* %1
store i32 12, i32* %6
store i32 12, i32* %7
%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* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %64
16: ; preds = %0
%17 = bitcast %struct.sockaddr_in* %2 to i8*
%18 = call i32 @memset(i8* %17, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 4444)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %21, i32* %22
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 4444)
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %4
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %16
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %64
34: ; preds = %16
store i32 0, i32* %5
br label %35
35: ; preds = %56, %34
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 10
br i1 %37, label %38, label %59
38: ; preds = %35
%39 = load i32, i32* %4
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i32 @recvfrom(i32 %39, i8* %40, i32 512, i32 0, %struct.sockaddr* %41, i32* %7)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %64
46: ; preds = %38
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @ntohs(i32 %52)
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %50, i32 %53, i8* %54)
br label %56
56: ; preds = %46
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %35
59: ; preds = %35
%60 = load i32, i32* %4
%61 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%62 = load i32, i32* %7
%63 = call i32 @sendto(i32 %60, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 10, i32 0, %struct.sockaddr* %61, i32 %62)
store i32 0, i32* %1
br label %64
64: ; preds = %59, %44, %32, %14
%65 = load i32, i32* %1
ret i32 %65
}

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 = [25 x i8] c"ERRO NA CRICAO DO SOCKET\00"
@.str.1 = [24 x i8] c"Setando porta para: %d\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [16 x i8] c"ERRO NO BINDING\00"
@.str.3 = [21 x i8] c"ERRO AO RECEBER DADO\00"
@.str.4 = [35 x i8] c"Pacote recebido de %s:%d\0ADado: %s\0A\00"
@.str.5 = [10 x i8] c"Obrigado!\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%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 eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %52
16: ; preds = %0
%17 = call i32 @memset(i8* %5, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 4444)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 4444)
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%27 = call i32 @bind(i32 %12, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %33, label %29
29: ; preds = %16
%30 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %35
33: ; preds = %16
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %52
35: ; preds = %29, %41
%36 = phi i32 [ 0, %29 ], [ %47, %41 ]
%37 = call i32 @recvfrom(i32 %12, i8* %8, i32 512, i32 0, %struct.sockaddr* %30, i32* %3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %52
41: ; preds = %35
%42 = load i32, i32* %31
%43 = call i8* @inet_ntoa(i32 %42)
%44 = load i32, i32* %32
%45 = call i32 @ntohs(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %45, i8* %8)
%47 = add nuw nsw i32 %36, 1
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %35
49: ; preds = %41
%50 = load i32, i32* %3
%51 = call i32 @sendto(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 10, i32 0, %struct.sockaddr* %30, i32 %50)
br label %52
52: ; preds = %49, %39, %33, %14
%53 = phi i32 [ 1, %14 ], [ 1, %33 ], [ 1, %39 ], [ 0, %49 ]
ret i32 %53
}