#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ send_buf;
typedef int /*<<< orphan*/ recv_buf;
typedef int /*<<< orphan*/ dest_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
unsigned short atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(int argc, char *argv[]) {
  int sockfd = 0;
  struct sockaddr_in dest_addr;
  unsigned short port = 8000;
  char *server_ip = "10.220.2.189";
  if (argc > 1) {
    server_ip = argv[1];
  }
  if (argc > 2) {
    port = atoi(argv[2]);
  }
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0) {
    perror("socket");
    exit(-1);
  }
  bzero(&dest_addr, sizeof(dest_addr));
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(port);
  inet_pton(AF_INET, server_ip, &dest_addr.sin_addr);
  printf("ready send data to UDP server %s:%d\n", server_ip, port);
  while (1) {
    char send_buf[512] = "";
    char recv_buf[512] = "";
    fgets(send_buf, sizeof(send_buf), stdin);
    send_buf[strlen(send_buf) - 1] = '\0';
    sendto(sockfd, send_buf, strlen(send_buf), 0, (struct sockaddr *)&dest_addr,
           sizeof(dest_addr));
    recvfrom(sockfd, recv_buf, sizeof(recv_buf), 0, NULL, NULL);
    printf("%s\n", recv_buf);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movw   $0x1f40,-0x22(%rbp)
lea    0xe03(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1217 <main+0x47>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1232 <main+0x62>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %ax,-0x22(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126e <main+0x9e>
lea    0xdae(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movzwl -0x22(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <inet_pton@plt>
mov    -0x30(%rbp),%rsi
movzwl -0x22(%rbp),%edx
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x230(%rbp),%rdi
mov    0x2cec(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1080 <fgets@plt>
lea    -0x230(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x440(%rbp)
cltq
movb   $0x0,-0x230(%rbp,%rax,1)
mov    -0x14(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x230(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   10a0 <recvfrom@plt>
lea    -0x430(%rbp),%rsi
lea    0xcab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12c0 <main+0xf0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    $0x1f40,%bx
cmp    $0x2,%edi
jl     11fd <main+0x2d>
mov    0x8(%rsi),%r15
je     1204 <main+0x34>
mov    0x10(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
jmp    1204 <main+0x34>
lea    0xdfc(%rip),%r15
mov    0x2ddd(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1318 <main+0x148>
mov    %eax,%r12d
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
movzwl %bx,%ebx
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1090 <inet_pton@plt>
lea    0xdb1(%rip),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x210(%rsp),%rbx
lea    0x10(%rsp),%rbp
mov    0x2d5d(%rip),%r13
lea    0xdaf(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    $0x200,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   10a0 <recvfrom@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1290 <main+0xc0>
lea    0xcee(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"10.220.2.189\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [37 x i8] c"ready send data to UDP server %s:%d\0A\00"
@stdin = global i32 0
@.str.3 = [4 x i8] c"%s\0A\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 i16
%9 = alloca i8*
%10 = alloca [512 x i8]
%11 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i16 8000, i16* %8
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %9
br label %18
18: ; preds = %14, %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call zeroext i16 @atoi(i8* %24)
store i16 %25, i16* %8
br label %26
26: ; preds = %21, %18
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %26
%36 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %37, i32* %38
%39 = load i16, i16* %8
%40 = call i32 @htons(i16 zeroext %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %40, i32* %41
%42 = load i32, i32* @AF_INET
%43 = load i8*, i8** %9
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%45 = call i32 @inet_pton(i32 %42, i8* %43, i32* %44)
%46 = load i8*, i8** %9
%47 = load i16, i16* %8
%48 = zext i16 %47 to i32
%49 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %46, i32 %48)
br label %50
50: ; preds = %35, %50
%51 = bitcast [512 x i8]* %10 to i8*
%52 = bitcast [512 x i8]* %11 to i8*
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%54 = load i32, i32* @stdin
%55 = call i32 @fgets(i8* %53, i32 512, i32 %54)
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%57 = call i32 @strlen(i8* %56)
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %59
store i8 0, i8* %60
%61 = load i32, i32* %6
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%66 = call i32 @sendto(i32 %61, i8* %62, i32 %64, i32 0, %struct.sockaddr* %65, i32 12)
%67 = load i32, i32* %6
%68 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%69 = call i32 @recvfrom(i32 %67, i8* %68, i32 512, i32 0, i32* null, i32* null)
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%71 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %70)
br label %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"10.220.2.189\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [37 x i8] c"ready send data to UDP server %s:%d\0A\00"
@stdin = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %16
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call zeroext i16 @atoi(i8* %14)
br label %16
16: ; preds = %2, %12, %8
%17 = phi i8* [ %10, %12 ], [ %10, %8 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %2 ]
%18 = phi i16 [ %15, %12 ], [ 8000, %8 ], [ 8000, %2 ]
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %16
%27 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %28, i32* %29
%30 = tail call i32 @htons(i16 zeroext %18)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = call i32 @inet_pton(i32 %32, i8* %17, i32* %33)
%35 = zext i16 %18 to i32
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %17, i32 %35)
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %40
40: ; preds = %40, %26
%41 = load i32, i32* @stdin
%42 = call i32 @fgets(i8* %37, i32 512, i32 %41)
%43 = call i32 @strlen(i8* %37)
%44 = add nsw i32 %43, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %45
store i8 0, i8* %46
%47 = call i32 @strlen(i8* %37)
%48 = call i32 @sendto(i32 %21, i8* %37, i32 %47, i32 0, %struct.sockaddr* %39, i32 12)
%49 = call i32 @recvfrom(i32 %21, i8* %38, i32 512, i32 0, i32* null, i32* null)
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %40
}