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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_port = htons(8000);
  inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);
  int ret = bind(sock, (struct sockaddr *)&server, sizeof(server));
  if (ret != 0) {
    printf("bind error\n");
    return 0;
  }
  char buf[10];
  while (1) {
    struct sockaddr_in client;
    int len;
    ret = recvfrom(sock, buf, 10, 0, (struct sockaddr *)&client, &len);
    if (0 == ret) {
      continue;
    }
    printf("buf = %s\n", buf);
    sendto(sock, buf, 10, 0, (struct sockaddr *)&client, sizeof(client));
    printf("ret = %d\n", ret);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xe38(%rip),%rsi
call   1060 <inet_pton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1201 <main+0x91>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret
jmp    1206 <main+0x96>
mov    -0x8(%rbp),%edi
lea    -0x26(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0xa,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    1234 <main+0xc4>
jmp    1206 <main+0x96>
lea    -0x26(%rbp),%rsi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x26(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0xa,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1050 <sendto@plt>
mov    -0x1c(%rbp),%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1206 <main+0x96>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e53(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%edi
lea    0xe37(%rip),%rsi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdx
call   1070 <inet_pton@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
je     1206 <main+0x86>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1e(%rsp),%r14
lea    0x28(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0xdf8(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
je     1220 <main+0xa0>
mov    %eax,%r13d
lea    0xdc5(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %rbp,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <main+0xa0>

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 = [10 x i8] c"127.0.0.1\00"
@.str.1 = [12 x i8] c"bind error\0A\00"
@.str.2 = [10 x i8] c"buf = %s\0A\00"
@.str.3 = [10 x i8] c"ret = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [10 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 8000)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%17 = call i32 @inet_pton(i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %16)
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 12)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
25: ; preds = %0
br label %26
26: ; preds = %25, %33, %34
%27 = load i32, i32* %2
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%29 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%30 = call i32 @recvfrom(i32 %27, i8* %28, i32 10, i32 0, %struct.sockaddr* %29, i32* %7)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 0, %31
br i1 %32, label %33, label %34
33: ; preds = %26
br label %26
34: ; preds = %26
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %2
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%39 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%40 = call i32 @sendto(i32 %37, i8* %38, i32 10, i32 0, %struct.sockaddr* %39, i32 12)
%41 = load i32, i32* %4
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %41)
br label %26
}

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 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"buf = %s\0A\00"
@.str.3 = [10 x i8] c"ret = %d\0A\00"
@str = [11 x i8] c"bind error\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [10 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.sockaddr_in* %1 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 8000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%15 = call i32 @inet_pton(i32 %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %14)
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @bind(i32 %7, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %0
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
ret i32 0
21: ; preds = %0
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%23 = bitcast %struct.sockaddr_in* %3 to i8*
%24 = bitcast i32* %4 to i8*
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %26
26: ; preds = %33, %21
%27 = call i32 @recvfrom(i32 %7, i8* %22, i32 10, i32 0, %struct.sockaddr* %25, i32* %4)
%28 = icmp eq i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%31 = call i32 @sendto(i32 %7, i8* %22, i32 10, i32 0, %struct.sockaddr* %25, i32 12)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %33
33: ; preds = %26, %29
br label %26
}