#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_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  struct sockaddr_in sin;
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  char buf[2000];
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(4000);
  if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
    perror("bind");
  while (1) {
    int n = read(fd, buf, 2000);
    if (n < 1)
      break;
    write(1, buf, n);
    if (buf[n - 1] != '\n')
      write(1, "\n", 1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x800,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0xfa0,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     11ee <main+0x7e>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
jmp    11f3 <main+0x83>
mov    -0x14(%rbp),%edi
lea    -0x7f0(%rbp),%rsi
mov    $0x7d0,%edx
call   1060 <read@plt>
mov    %eax,-0x7f4(%rbp)
cmpl   $0x1,-0x7f4(%rbp)
jge    121f <main+0xaf>
jmp    126a <main+0xfa>
lea    -0x7f0(%rbp),%rsi
mov    -0x7f4(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x7f4(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x7f0(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     1265 <main+0xf5>
mov    $0x1,%edx
lea    0xda7(%rip),%rsi
mov    %edx,%edi
call   1030 <write@plt>
jmp    11f3 <main+0x83>
mov    -0x4(%rbp),%eax
add    $0x800,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x7e8,%rsp
mov    0x2e64(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    $0xfa0,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    1253 <main+0xe3>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x7d0,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1243 <main+0xd3>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xe09(%rip),%r14
jmp    1216 <main+0xa6>
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x7d0,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1243 <main+0xd3>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
cmpb   $0xa,0x10(%rsp,%rbp,1)
je     1200 <main+0x90>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1200 <main+0x90>
xor    %eax,%eax
add    $0x7e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda6(%rip),%rdi
call   1080 <perror@plt>
jmp    11d8 <main+0x68>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [2000 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* %3
%9 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %10, i32* %11
%12 = call i32 @htons(i32 4000)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i64 @bind(i32 %14, %struct.sockaddr* %15, i32 8)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %0
br label %21
21: ; preds = %20, %41
%22 = load i32, i32* %3
%23 = getelementptr inbounds [2000 x i8], [2000 x i8]* %4, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 2000)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %21
br label %42
28: ; preds = %21
%29 = getelementptr inbounds [2000 x i8], [2000 x i8]* %4, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @write(i32 1, i8* %29, i32 %30)
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [2000 x i8], [2000 x i8]* %4, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 10
br i1 %38, label %39, label %41
39: ; preds = %28
%40 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %41
41: ; preds = %39, %28
br label %21
42: ; preds = %27
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [2000 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = getelementptr inbounds [2000 x i8], [2000 x i8]* %2, i64 0, i64 0
%8 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i32 @htons(i32 4000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %11, i32* %12
%13 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%14 = call i64 @bind(i32 %6, %struct.sockaddr* %13, i32 8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = call i32 @read(i32 %6, i8* %7, i32 2000)
%20 = icmp slt i32 %19, 1
br i1 %20, label %34, label %21
21: ; preds = %18, %31
%22 = phi i32 [ %32, %31 ], [ %19, %18 ]
%23 = call i32 @write(i32 1, i8* %7, i32 %22)
%24 = add nsw i32 %22, -1
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [2000 x i8], [2000 x i8]* %2, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 10
br i1 %28, label %31, label %29
29: ; preds = %21
%30 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %31
31: ; preds = %21, %29
%32 = call i32 @read(i32 %6, i8* %7, i32 2000)
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %21
34: ; preds = %31, %18
ret i32 0
}