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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int main() {
  struct sockaddr_in server;
  int socketfd = 0;
  char *addr = "127.0.0.1";
  char buf[256] = {0};
  char msg[256] = {0};
  int sock_len = sizeof(struct sockaddr);
  bzero(&server, sizeof(server));
  server.sin_family = PF_INET;
  server.sin_addr.s_addr = inet_addr(addr);
  server.sin_port = htons(1994);
  socketfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (socketfd < 0) {
    printf("create socket error ... ");
    return -1;
  }
  printf("udp server start [%d][0.0.0.0][%d] -------> \n", socketfd, 1994);
  while (1) {
    memset(buf, 0, 256);
    fgets(buf, 256, stdin);
    int send = sendto(socketfd, buf, strlen(buf), 0, (struct sockaddr *)&server,
                      sizeof(struct sockaddr));
    if (send < 0) {
      perror("sendto error ....");
      break;
    } else {
      printf("send to %s(port=%d) len %d:%s\n", addr, 1994, send, buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movl   $0x4,-0x224(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0xc(%rbp)
mov    $0x7ca,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1267 <main+0xc7>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <main+0x1a1>
mov    -0x14(%rbp),%esi
lea    0xdb2(%rip),%rdi
mov    $0x7ca,%edx
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x234(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x230(%rbp)
lea    -0x120(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x234(%rbp),%edi
mov    -0x230(%rbp),%rsi
mov    %eax,%edx
lea    -0x10(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    1311 <main+0x171>
lea    0xd4a(%rip),%rdi
call   10a0 <perror@plt>
jmp    133a <main+0x19a>
mov    -0x20(%rbp),%rsi
mov    -0x228(%rbp),%ecx
lea    -0x120(%rbp),%r8
lea    0xd3a(%rip),%rdi
mov    $0x7ca,%edx
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127d <main+0xdd>
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    $0x118,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    %rsp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xdc0(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x7ca,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    1298 <main+0xf8>
lea    0xd90(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xd82(%rip),%rdi
mov    %eax,%esi
mov    $0x7ca,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rbx
mov    0x2d25(%rip),%r13
mov    %rsp,%r14
lea    0xd9e(%rip),%r15
lea    0xd34(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x4,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
js     132c <main+0x18c>
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x7ca,%edx
mov    %eax,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12d0 <main+0x130>
lea    0xd1e(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
jmp    1286 <main+0xe6>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [25 x i8] c"create socket error ... \00"
@.str.2 = [46 x i8] c"udp server start [%d][0.0.0.0][%d] -------> \0A\00"
@stdin = global i32 0
@.str.3 = [18 x i8] c"sendto error ....\00"
@.str.4 = [31 x i8] c"send to %s(port=%d) len %d:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %4
%9 = bitcast [256 x i8]* %5 to i8*
%10 = bitcast [256 x i8]* %6 to i8*
store i32 4, i32* %7
%11 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%12 = load i32, i32* @PF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i8*, i8** %4
%15 = call i32 @inet_addr(i8* %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 1994)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_DGRAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %53
27: ; preds = %0
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 1994)
br label %30
30: ; preds = %27, %51
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 256)
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%34 = load i32, i32* @stdin
%35 = call i32 @fgets(i8* %33, i32 256, i32 %34)
%36 = load i32, i32* %3
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%41 = call i32 @sendto(i32 %36, i8* %37, i32 %39, i32 0, %struct.sockaddr* %40, i32 4)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %30
%45 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %52
46: ; preds = %30
%47 = load i8*, i8** %4
%48 = load i32, i32* %8
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %47, i32 1994, i32 %48, i8* %49)
br label %51
51: ; preds = %46
br label %30
52: ; preds = %44
store i32 0, i32* %1
br label %53
53: ; preds = %52, %25
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [25 x i8] c"create socket error ... \00"
@.str.2 = [46 x i8] c"udp server start [%d][0.0.0.0][%d] -------> \0A\00"
@stdin = global i32 0
@.str.3 = [18 x i8] c"sendto error ....\00"
@.str.4 = [31 x i8] c"send to %s(port=%d) len %d:%s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [256 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%6 = load i32, i32* @PF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 1994)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %32
18: ; preds = %0
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 1994)
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %21
21: ; preds = %30, %18
%22 = call i32 @memset(i8* %4, i32 0, i32 256)
%23 = load i32, i32* @stdin
%24 = call i32 @fgets(i8* %4, i32 256, i32 %23)
%25 = call i32 @strlen(i8* %4)
%26 = call i32 @sendto(i32 %14, i8* %4, i32 %25, i32 0, %struct.sockaddr* %20, i32 4)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %32
30: ; preds = %21
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 1994, i32 %26, i8* %4)
br label %21
32: ; preds = %28, %16
%33 = phi i32 [ -1, %16 ], [ 0, %28 ]
ret i32 %33
}