#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*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    printf("socket : %m\n");
    exit(-1);
  }
  printf("create socket success.....\n");
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8686);
  inet_aton("192.168.137.3", &addr.sin_addr);
  char buf[1024];
  int r;
  while (1) {
    r = read(0, buf, sizeof(buf) - 2);
    if (fd <= -1) {
      break;
    }
    buf[r] = '\0';
    sendto(fd, buf, r, 0, (struct sockaddr *)&addr, sizeof(addr));
    sleep(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11de <main+0x4e>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x21ee,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xe1b(%rip),%rdi
call   1080 <inet_aton@plt>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x3fe,%edx
call   1070 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jg     123b <main+0xab>
jmp    127a <main+0xea>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
lea    -0x18(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1050 <sendto@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1213 <main+0x83>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    0x2e36(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     126d <main+0xcd>
mov    %eax,%ebp
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x21ee,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe17(%rip),%rdi
mov    %rsp,%rsi
call   1090 <inet_aton@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x3fe,%edx
call   1080 <read@plt>
test   %ebp,%ebp
js     1258 <main+0xb8>
lea    0x10(%rsp),%rbx
mov    %rsp,%r14
nopl   0x0(%rax,%rax,1)
movslq %eax,%rdx
movb   $0x0,0x10(%rsp,%rdx,1)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3fe,%edx
call   1080 <read@plt>
jmp    1220 <main+0x80>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8c(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket : %m\0A\00"
@.str.1 = [28 x i8] c"create socket success.....\0A\00"
@.str.2 = [14 x i8] c"192.168.137.3\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 8686)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%21 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %20)
br label %22
22: ; preds = %14, %28
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%24 = call i32 @read(i32 0, i8* %23, i32 1022)
store i32 %24, i32* %5
%25 = load i32, i32* %2
%26 = icmp sle i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22
br label %38
28: ; preds = %22
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* %2
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* %5
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @sendto(i32 %32, i8* %33, i32 %34, i32 0, %struct.sockaddr* %35, i32 12)
%37 = call i32 @sleep(i32 1)
br label %22
38: ; preds = %27
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket : %m\0A\00"
@.str.2 = [14 x i8] c"192.168.137.3\00"
@str = [27 x i8] c"create socket success.....\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%12 = bitcast %struct.sockaddr_in* %1 to i8*
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 8686)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%18 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%20 = call i32 @read(i32 0, i8* %19, i32 1022)
%21 = icmp slt i32 %5, 0
br i1 %21, label %31, label %22
22: ; preds = %10
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %24
24: ; preds = %24, %22
%25 = phi i32 [ %20, %22 ], [ %30, %24 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i32 @sendto(i32 %5, i8* %19, i32 %25, i32 0, %struct.sockaddr* %23, i32 12)
%29 = call i32 @sleep(i32 1)
%30 = call i32 @read(i32 0, i8* %19, i32 1022)
br label %24
31: ; preds = %10
%32 = call i32 @close(i32 %5)
ret i32 0
}