#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_DGRAM;
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ htons(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_DGRAM, 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");
  uint8_t buf[10240];
  for (uint32_t i = 0; i < 100; i++) {
    ssize_t n;
    n = read(sock, 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    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e2c(%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   1070 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1200 <main+0x80>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0xa034(%rbp)
cmpl   $0x64,-0xa034(%rbp)
jge    1286 <main+0x106>
mov    -0x14(%rbp),%edi
lea    -0xa030(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0xa040(%rbp)
cmpq   $0x0,-0xa040(%rbp)
jg     126d <main+0xed>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1286 <main+0x106>
jmp    1272 <main+0xf2>
mov    -0xa034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa034(%rbp)
jmp    120a <main+0x8a>
lea    0xd85(%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   %r14
push   %rbx
sub    $0xa010,%rsp
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%r14d
mov    0x2e2a(%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   1080 <bind@plt>
test   %eax,%eax
jne    125f <main+0xcf>
mov    $0x64,%ebp
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    121c <main+0x8c>
add    $0xffffffff,%ebp
jne    1200 <main+0x70>
jmp    123d <main+0xad>
mov    0x2dad(%rip),%rbx
mov    (%rbx),%edi
call   10a0 <strerror@plt>
mov    (%rbx),%edx
lea    0xdd2(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xa010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9a(%rip),%rdi
call   1090 <perror@plt>
jmp    11ee <main+0x5e>

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