#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int uint32_t;
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/, int /*<<< orphan*/);
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main(int argc, char *argv[]) {
  int sock;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(2000);
  addr.sin_addr.s_addr = 0;
  int result;
  result = bind(sock, (const struct sockaddr *)&addr, sizeof(addr));
  if (result)
    perror("bind");
  result = listen(sock, 1);
  if (result)
    perror("listen");
  int fd = accept(sock, 0, 0);
  uint8_t buf[10240];
  for (uint32_t i = 0; i < 100; i++) {
    ssize_t n;
    n = read(fd, buf, 1024);
    if (n <= 0) {
      printf("read: %s %d\n", strerror(errno), errno);
      break;
    }
  }
  printf("did read all buffers\n");
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1220 <main+0x80>
lea    0xde5(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1246 <main+0xa6>
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10a0 <accept@plt>
mov    %eax,-0x30(%rbp)
movl   $0x0,-0xa034(%rbp)
cmpl   $0x64,-0xa034(%rbp)
jge    12db <main+0x13b>
mov    -0x30(%rbp),%edi
lea    -0xa030(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0xa040(%rbp)
cmpq   $0x0,-0xa040(%rbp)
jg     12c2 <main+0x122>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12db <main+0x13b>
jmp    12c7 <main+0x127>
mov    -0xa034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa034(%rbp)
jmp    125f <main+0xbf>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0xa040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa018,%rsp
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %eax,%eax
jne    12b0 <main+0x100>
mov    %r14d,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %eax,%eax
jne    12c1 <main+0x111>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
mov    $0x64,%ebx
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    126b <main+0xbb>
add    $0xffffffff,%ebx
jne    1250 <main+0xa0>
jmp    128c <main+0xdc>
mov    0x2d5e(%rip),%rbx
mov    (%rbx),%edi
call   10c0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd8a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd86(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xa018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd49(%rip),%rdi
call   10a0 <perror@plt>
jmp    1214 <main+0x64>
lea    0xd3d(%rip),%rdi
call   10a0 <perror@plt>
jmp    1229 <main+0x79>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [13 x i8] c"read: %s %d\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"did read all buffers\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 i32
%9 = alloca i32
%10 = alloca [10240 x i32]
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 2000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i64 0, i64* %21
%22 = load i32, i32* %6
%23 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%24 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 16)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %2
%30 = load i32, i32* %6
%31 = call i32 @listen(i32 %30, i32 1)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = load i32, i32* %6
%38 = call i32 @accept(i32 %37, i32 0, i32 0)
store i32 %38, i32* %9
store i32 0, i32* %11
br label %39
39: ; preds = %54, %36
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 100
br i1 %41, label %42, label %57
42: ; preds = %39
%43 = load i32, i32* %9
%44 = getelementptr inbounds [10240 x i32], [10240 x i32]* %10, i64 0, i64 0
%45 = call i64 @read(i32 %43, i32* %44, i32 1024)
store i64 %45, i64* %12
%46 = load i64, i64* %12
%47 = icmp sle i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = load i32, i32* @errno
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %50, i32 %51)
br label %57
53: ; preds = %42
br label %54
54: ; preds = %53
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %39
57: ; preds = %48, %39
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [13 x i8] c"read: %s %d\0A\00"
@errno = global i32 0
@str = [21 x i8] c"did read all buffers\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [10240 x i32]
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 2000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 0, i64* %13
%14 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%15 = call i32 @bind(i32 %7, %struct.sockaddr* %14, i32 16)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = call i32 @listen(i32 %7, i32 1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = call i32 @accept(i32 %7, i32 0, i32 0)
%26 = bitcast [10240 x i32]* %4 to i8*
%27 = getelementptr inbounds [10240 x i32], [10240 x i32]* %4, i64 0, i64 0
br label %31
28: ; preds = %31
%29 = add nuw nsw i32 %32, 1
%30 = icmp eq i32 %29, 100
br i1 %30, label %40, label %31
31: ; preds = %24, %28
%32 = phi i32 [ 0, %24 ], [ %29, %28 ]
%33 = call i64 @read(i32 %25, i32* %27, i32 1024)
%34 = icmp slt i64 %33, 1
br i1 %34, label %35, label %28
35: ; preds = %31
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = load i32, i32* @errno
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %38)
br label %40
40: ; preds = %28, %35
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%42 = call i32 @close(i32 %7)
ret i32 0
}