#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr_in *, int *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr_in *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sock, length, n;
  struct sockaddr_in server, from;
  struct hostent *hp;
  char buffer[256];
  if (argc != 3) {
    printf("\n\tCLIENT: Uso correto: servidor porta\n\n");
    exit(1);
  }
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    error("\n\tCLIENT:error: socket\n\t");
  server.sin_family = AF_INET;
  hp = gethostbyname(argv[1]);
  if (hp == 0)
    error("\n\tCLIENT:-error: host desconhecido");
  bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
  server.sin_port = htons(atoi(argv[2]));
  length = sizeof(struct sockaddr_in);
  do {
    printf("\n\tCLIENT: Digite a menssagem - 0 pra sair: ");
    bzero(buffer, 256);
    fgets(buffer, 255, stdin);
    n = sendto(sock, buffer, strlen(buffer), 0, &server, length);
    if (n < 0)
      error("\n\tCLIENT: erro: Send to - erro ao enviar");
    n = recvfrom(sock, buffer, 256, 0, &from, &length);
    if (n < 0)
      error("\n\tCLIENT: error: received from - erro ao receber\n\t");
    write(1, "CLIENT: ACK:  ", 12);
    write(1, buffer, n);
  } while ((n >= 0) || (buffer == '0'));
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1219 <main+0x39>
lea    0xdf6(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124b <main+0x6b>
lea    0xde2(%rip),%rdi
call   10c0 <error@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    127f <main+0x9f>
lea    0xdc7(%rip),%rdi
call   10c0 <error@plt>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   10b0 <bcopy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
movl   $0xc,-0x18(%rbp)
lea    0xda5(%rip),%rdi
call   1060 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
lea    -0x140(%rbp),%rdi
mov    0x2cfd(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1080 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x140(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%r9d
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
call   1070 <sendto@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1349 <main+0x169>
lea    0xd4c(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x38(%rbp),%r8
lea    -0x18(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1380 <main+0x1a0>
lea    0xd3e(%rip),%rdi
call   10c0 <error@plt>
mov    $0x1,%edi
lea    0xd60(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
lea    -0x140(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    $0x1,%al
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x14d(%rbp)
jge    13d4 <main+0x1f4>
lea    -0x140(%rbp),%rax
mov    $0x30,%ecx
cmp    %rcx,%rax
sete   %al
mov    %al,-0x14d(%rbp)
mov    -0x14d(%rbp),%al
test   $0x1,%al
jne    12b8 <main+0xd8>
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x3,%edi
jne    1372 <main+0x182>
mov    %rsi,%r14
mov    0x2dd4(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1238 <main+0x48>
lea    0xdcd(%rip),%rdi
call   10d0 <error@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%r14),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    125b <main+0x6b>
lea    0xdc3(%rip),%rdi
call   10d0 <error@plt>
mov    0x8(%rbx),%rsi
lea    0x14(%rsp),%rdi
mov    (%rbx),%edx
call   10c0 <memmove@plt>
mov    0x10(%r14),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r13
lea    0xc(%rsp),%r12
lea    0xe27(%rip),%r14
nopl   (%rax)
lea    0xd95(%rip),%rdi
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0xc(%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1080 <sendto@plt>
test   %eax,%eax
jns    12ff <main+0x10f>
lea    0xd6e(%rip),%rdi
call   10d0 <error@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r12,%r9
call   10b0 <recvfrom@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1329 <main+0x139>
lea    0xd6d(%rip),%rdi
call   10d0 <error@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
cmp    $0x30,%rbx
je     12a0 <main+0xb0>
test   %r15d,%r15d
jns    12a0 <main+0xb0>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
@.str = [40 x i8] c"\0A\09CLIENT: Uso correto: servidor porta\0A\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [25 x i8] c"\0A\09CLIENT:error: socket\0A\09\00"
@.str.2 = [35 x i8] c"\0A\09CLIENT:-error: host desconhecido\00"
@.str.3 = [44 x i8] c"\0A\09CLIENT: Digite a menssagem - 0 pra sair: \00"
@stdin = global i32 0
@.str.4 = [41 x i8] c"\0A\09CLIENT: erro: Send to - erro ao enviar\00"
@.str.5 = [51 x i8] c"\0A\09CLIENT: error: received from - erro ao receber\0A\09\00"
@.str.6 = [15 x i8] c"CLIENT: ACK: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %11
%33 = load %struct.hostent*, %struct.hostent** %11
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %26
%38 = load %struct.hostent*, %struct.hostent** %11
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i8*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%43 = bitcast i32* %42 to i8*
%44 = load %struct.hostent*, %struct.hostent** %11
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @bcopy(i8* %41, i8* %43, i32 %46)
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 2
%50 = load i8*, i8** %49
%51 = call i32 @atoi(i8* %50)
%52 = call i32 @htons(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %52, i32* %53
store i32 12, i32* %7
br label %54
54: ; preds = %90, %37
%55 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%57 = call i32 @bzero(i8* %56, i32 256)
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%59 = load i32, i32* @stdin
%60 = call i32 @fgets(i8* %58, i32 255, i32 %59)
%61 = load i32, i32* %6
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = load i32, i32* %7
%66 = call i32 @sendto(i32 %61, i8* %62, i32 %64, i32 0, %struct.sockaddr_in* %9, i32 %65)
store i32 %66, i32* %8
%67 = load i32, i32* %8
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %54
%70 = call i32 @error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %71
71: ; preds = %69, %54
%72 = load i32, i32* %6
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%74 = call i32 @recvfrom(i32 %72, i8* %73, i32 256, i32 0, %struct.sockaddr_in* %10, i32* %7)
store i32 %74, i32* %8
%75 = load i32, i32* %8
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %71
%78 = call i32 @error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0))
br label %79
79: ; preds = %77, %71
%80 = call i32 @write(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 12)
%81 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%82 = load i32, i32* %8
%83 = call i32 @write(i32 1, i8* %81, i32 %82)
br label %84
84: ; preds = %79
%85 = load i32, i32* %8
%86 = icmp sge i32 %85, 0
br i1 %86, label %90, label %87
87: ; preds = %84
%88 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%89 = icmp eq i8* %88, inttoptr (i64 48 to i8*)
br label %90
90: ; preds = %87, %84
%91 = phi i1 [ true, %84 ], [ %89, %87 ]
br i1 %91, label %54, label %92
92: ; preds = %90
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [25 x i8] c"\0A\09CLIENT:error: socket\0A\09\00"
@.str.2 = [35 x i8] c"\0A\09CLIENT:-error: host desconhecido\00"
@.str.3 = [44 x i8] c"\0A\09CLIENT: Digite a menssagem - 0 pra sair: \00"
@stdin = global i32 0
@.str.4 = [41 x i8] c"\0A\09CLIENT: erro: Send to - erro ao enviar\00"
@.str.5 = [51 x i8] c"\0A\09CLIENT: error: received from - erro ao receber\0A\09\00"
@.str.6 = [15 x i8] c"CLIENT: ACK: \00"
@str = [39 x i8] c"\0A\09CLIENT: Uso correto: servidor porta\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [256 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 3
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call %struct.hostent* @gethostbyname(i8* %26)
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = tail call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %22
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 1
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i8*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%36 = bitcast i32* %35 to i8*
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = tail call i32 @atoi(i8* %40)
%42 = tail call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %42, i32* %43
store i32 12, i32* %3
%44 = icmp eq i8* %10, inttoptr (i64 48 to i8*)
br label %45
45: ; preds = %61, %31
%46 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @bzero(i8* %10, i32 256)
%48 = load i32, i32* @stdin
%49 = call i32 @fgets(i8* %10, i32 255, i32 %48)
%50 = call i32 @strlen(i8* %10)
%51 = load i32, i32* %3
%52 = call i32 @sendto(i32 %18, i8* %10, i32 %50, i32 0, %struct.sockaddr_in* %4, i32 %51)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 @error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %45
%57 = call i32 @recvfrom(i32 %18, i8* %10, i32 256, i32 0, %struct.sockaddr_in* %5, i32* %3)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = call i32 @write(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 12)
%63 = call i32 @write(i32 1, i8* %10, i32 %57)
%64 = icmp sgt i32 %57, -1
%65 = or i1 %44, %64
br i1 %65, label %45, label %66
66: ; preds = %61
ret i32 0
}