#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, unsigned int);
int /*<<< orphan*/ free(char *);
scalar_t__ gethostbyname(char *);
size_t getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ help();
int /*<<< orphan*/ htons(int /*<<< orphan*/);
char *inet_ntoa(struct in_addr);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sendto(int, char *, size_t, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strncasecmp(char *, char *, size_t);
int main(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in server_addr;
  struct hostent *host = NULL;
  char *send_data = NULL;
  size_t size = 0;
  ssize_t len = 0;
  if (argc < 3) {
    help();
    exit(1);
  }
  host = (struct hostent *)gethostbyname((char *)argv[1]);
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(atoi(argv[2]));
  server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(server_addr.sin_zero), 8);
  printf("Sending to %s:%d\n", inet_ntoa(server_addr.sin_addr),
         ntohs(server_addr.sin_port));
  while (1) {
    printf("Type Something (q or Q to quit):");
    if ((len = getline(&send_data, &size, stdin)) > 0) {
      len--;
      send_data[len] = '\0';
      if (strncasecmp(send_data, "q", len) == 0) {
        break;
      } else {
        fprintf(stdout, "sending %s size %u", send_data, (unsigned int)len);
        sendto(sock, send_data, len, 0, (struct sockaddr *)&server_addr,
               sizeof(struct sockaddr));
      }
      free(send_data);
      send_data = NULL;
      size = 0;
      len = 0;
    } else {
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1251 <main+0x51>
mov    $0x0,%al
call   10e0 <help@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    129d <main+0x9d>
lea    0xd72(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    $0x8,%esi
call   10d0 <bzero@plt>
mov    -0x24(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1100 <getline@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jbe    13d4 <main+0x1d4>
mov    -0x48(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xcdb(%rip),%rsi
call   10a0 <strncasecmp@plt>
cmp    $0x0,%rax
jne    1373 <main+0x173>
jmp    13de <main+0x1de>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    %eax,%ecx
lea    0xcaf(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1070 <sendto@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
jmp    13d9 <main+0x1d9>
jmp    13de <main+0x1de>
jmp    1306 <main+0x106>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
cmp    $0x2,%edi
jle    13b9 <main+0x1b9>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%r14
mov    0x2da9(%rip),%r15
mov    (%r15),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
je     13ca <main+0x1ca>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x24(%rsp)
mov    0x10(%rbp),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x8,%esi
call   10d0 <bzero@plt>
mov    0x1c(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x18(%rsp),%edi
call   10b0 <ntohs@plt>
lea    0xd5b(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd5a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1100 <getline@plt>
test   %rax,%rax
je     13a8 <main+0x1a8>
mov    %rax,%r14
lea    0xd28(%rip),%r13
lea    0x8(%rsp),%rbp
lea    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
movb   $0x0,-0x1(%rax,%r14,1)
add    $0xffffffffffffffff,%r14
mov    0x8(%rsp),%r12
mov    %r12,%rdi
lea    0xd1c(%rip),%rsi
mov    %r14,%rdx
call   10a0 <strncasecmp@plt>
test   %rax,%rax
je     13a8 <main+0x1a8>
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
lea    0xd01(%rip),%rsi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
mov    $0x4,%r9d
call   1070 <sendto@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c51(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1100 <getline@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1300 <main+0x100>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10e0 <help@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xc2f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [18 x i8] c"Sending to %s:%d\0A\00"
@.str.2 = [33 x i8] c"Type Something (q or Q to quit):\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"q\00"
@stdout = global i32 0
@.str.4 = [19 x i8] c"sending %s size %u\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.hostent* null, %struct.hostent** %8
store i8* null, i8** %9
store i64 0, i64* %10
store i64 0, i64* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (...) @help()
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @gethostbyname(i8* %20)
%22 = inttoptr i64 %21 to %struct.hostent*
store %struct.hostent* %22, %struct.hostent** %8
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_DGRAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 3
store i32 %31, i32* %32
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%40 = load %struct.hostent*, %struct.hostent** %8
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.in_addr*
%44 = bitcast %struct.in_addr* %39 to i8*
%45 = bitcast %struct.in_addr* %43 to i8*
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%47 = call i32 @bzero(i32* %46, i32 8)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%49 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i8* @inet_ntoa(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @ntohs(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %51, i32 %54)
br label %56
56: ; preds = %30, %87
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%58 = load i32, i32* @stdin
%59 = call i64 @getline(i8** %9, i64* %10, i32 %58)
store i64 %59, i64* %11
%60 = icmp ugt i64 %59, 0
br i1 %60, label %61, label %86
61: ; preds = %56
%62 = load i64, i64* %11
%63 = add i64 %62, -1
store i64 %63, i64* %11
%64 = load i8*, i8** %9
%65 = load i64, i64* %11
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8 0, i8* %66
%67 = load i8*, i8** %9
%68 = load i64, i64* %11
%69 = call i64 @strncasecmp(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i64 %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %61
br label %88
72: ; preds = %61
%73 = load i32, i32* @stdout
%74 = load i8*, i8** %9
%75 = load i64, i64* %11
%76 = trunc i64 %75 to i32
%77 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %74, i32 %76)
%78 = load i32, i32* %6
%79 = load i8*, i8** %9
%80 = load i64, i64* %11
%81 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%82 = call i32 @sendto(i32 %78, i8* %79, i64 %80, i32 0, %struct.sockaddr* %81, i32 4)
br label %83
83: ; preds = %72
%84 = load i8*, i8** %9
%85 = call i32 @free(i8* %84)
store i8* null, i8** %9
store i64 0, i64* %10
store i64 0, i64* %11
br label %87
86: ; preds = %56
br label %88
87: ; preds = %83
br label %56
88: ; preds = %86, %71
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [18 x i8] c"Sending to %s:%d\0A\00"
@.str.2 = [33 x i8] c"Type Something (q or Q to quit):\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"q\00"
@stdout = global i32 0
@.str.4 = [19 x i8] c"sending %s size %u\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%8 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 (...) @help()
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i64 @gethostbyname(i8* %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = inttoptr i64 %16 to %struct.hostent*
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %26, i32* %27
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = tail call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i64 0, i32 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i32*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%37 = load i32, i32* %35
store i32 %37, i32* %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%39 = call i32 @bzero(i32* %38, i32 8)
%40 = load i32, i32* %36
%41 = tail call i8* @inet_ntoa(i32 %40)
%42 = load i32, i32* %32
%43 = tail call i32 @ntohs(i32 %42)
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %41, i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* @stdin
%47 = call i64 @getline(i8** %4, i64* %5, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %71, label %49
49: ; preds = %24
%50 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %51
51: ; preds = %49, %59
%52 = phi i64 [ %47, %49 ], [ %69, %59 ]
%53 = add i64 %52, -1
%54 = load i8*, i8** %4
%55 = getelementptr inbounds i8, i8* %54, i64 %53
store i8 0, i8* %55
%56 = load i8*, i8** %4
%57 = call i64 @strncasecmp(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i64 %53)
%58 = icmp eq i64 %57, 0
br i1 %58, label %71, label %59
59: ; preds = %51
%60 = load i32, i32* @stdout
%61 = trunc i64 %53 to i32
%62 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32 %61)
%63 = load i8*, i8** %4
%64 = call i32 @sendto(i32 %19, i8* %63, i64 %53, i32 0, %struct.sockaddr* %50, i32 4)
%65 = load i8*, i8** %4
%66 = call i32 @free(i8* %65)
store i8* null, i8** %4
store i64 0, i64* %5
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%68 = load i32, i32* @stdin
%69 = call i64 @getline(i8** %4, i64* %5, i32 %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %51
71: ; preds = %51, %59, %24
ret i32 0
}