#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 */
struct TYPE_2__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int STDIN_FILENO;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gets(char *);
void *htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int servfd, clifd;
  int cnt = 0;
  fd_set read_set, ready_set;
  struct sockaddr_in servaddr, cliaddr;
  if ((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("create socket error!\n");
    exit(1);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(20000);
  servaddr.sin_addr.s_addr = htons(INADDR_ANY);
  if (bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    printf("bind to port %d failure!\n", 20000);
    exit(1);
  }
  if (listen(servfd, 10) < 0) {
    printf("call listen failure!\n");
    exit(1);
  }
  FD_ZERO(&read_set);
  FD_SET(STDIN_FILENO, &read_set);
  FD_SET(servfd, &read_set);
  while (1) {
    ready_set = read_set;
    int ret;
    ret = select(servfd + 1, &ready_set, NULL, NULL, NULL);
    if (ret > 0) {
      if (FD_ISSET(STDIN_FILENO, &ready_set)) {
        char buf[32];
        gets(buf);
        printf("input str is: %s\n", buf);
      }
      if (FD_ISSET(servfd, &ready_set)) {
        printf("listened something ...\n");
      }
    } else {
      printf("end...\n");
      break;
    }
  }
  close(servfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    122d <main+0x5d>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x40(%rbp),%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x4e20,%edi
call   1040 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x18,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    129f <main+0xcf>
lea    0xd8d(%rip),%rdi
mov    $0x4e20,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    12ce <main+0xfe>
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jle    1382 <main+0x1b2>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1359 <main+0x189>
lea    -0x80(%rbp),%rdi
call   1070 <gets@plt>
lea    -0x80(%rbp),%rsi
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     137d <main+0x1ad>
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1395 <main+0x1c5>
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    139a <main+0x1ca>
jmp    12f5 <main+0x125>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0x2df3(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1364 <main+0x184>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x18,%esi
call   10d0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x4e20,%edi
call   1050 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x18,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     136d <main+0x18d>
mov    %r14d,%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
test   %rax,%rax
js     138a <main+0x1aa>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
mov    0x2d37(%rip),%r15
mov    (%r15),%edi
mov    %rbx,%rsi
call   10f0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   10f0 <FD_SET@plt>
mov    %r14d,%ebp
add    $0x1,%ebp
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
jle    133f <main+0x15f>
mov    %rsp,%rbx
lea    0x20(%rsp),%r12
lea    0xd42(%rip),%r13
jmp    12fc <main+0x11c>
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
jle    133f <main+0x15f>
mov    (%r15),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1321 <main+0x141>
mov    %r12,%rdi
call   1080 <gets@plt>
mov    %r13,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12e0 <main+0x100>
lea    0xcfb(%rip),%rdi
call   1030 <puts@plt>
jmp    12e0 <main+0x100>
lea    0xce6(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf4(%rip),%rdi
jmp    1391 <main+0x1b1>
lea    0xc8c(%rip),%rdi
mov    $0x4e20,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xcb9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"create socket error!\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [26 x i8] c"bind to port %d failure!\0A\00"
@.str.2 = [22 x i8] c"call listen failure!\0A\00"
@STDIN_FILENO = global i32 0
@.str.3 = [18 x i8] c"input str is: %s\0A\00"
@.str.4 = [24 x i8] c"listened something ...\0A\00"
@.str.5 = [8 x i8] c"end...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = call i32 @bzero(%struct.sockaddr_in* %11, i32 24)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i8* @htons(i32 20000)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i8* %26, i8** %27
%28 = load i32, i32* @INADDR_ANY
%29 = call i8* @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i8* %29, i8** %31
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 24)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %22
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 20000)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %22
%40 = load i32, i32* %6
%41 = call i64 @listen(i32 %40, i32 10)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = call i32 @FD_ZERO(i32* %9)
%48 = load i32, i32* @STDIN_FILENO
%49 = call i32 @FD_SET(i32 %48, i32* %9)
%50 = load i32, i32* %6
%51 = call i32 @FD_SET(i32 %50, i32* %9)
br label %52
52: ; preds = %46, %77
%53 = load i32, i32* %9
store i32 %53, i32* %10
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
%56 = call i32 @select(i32 %55, i32* %10, i32* null, i32* null, i32* null)
store i32 %56, i32* %13
%57 = load i32, i32* %13
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %75
59: ; preds = %52
%60 = load i32, i32* @STDIN_FILENO
%61 = call i64 @FD_ISSET(i32 %60, i32* %10)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %59
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%65 = call i32 @gets(i8* %64)
%66 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %66)
br label %68
68: ; preds = %63, %59
%69 = load i32, i32* %6
%70 = call i64 @FD_ISSET(i32 %69, i32* %10)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %74
74: ; preds = %72, %68
br label %77
75: ; preds = %52
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %78
77: ; preds = %74
br label %52
78: ; preds = %75
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [26 x i8] c"bind to port %d failure!\0A\00"
@STDIN_FILENO = global i32 0
@.str.3 = [18 x i8] c"input str is: %s\0A\00"
@str = [7 x i8] c"end...\00"
@str.6 = [23 x i8] c"listened something ...\00"
@str.7 = [21 x i8] c"call listen failure!\00"
@str.8 = [21 x i8] c"create socket error!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca [32 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 @bzero(%struct.sockaddr_in* %5, i32 24)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i8* @htons(i32 20000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i8* %21, i8** %22
%23 = load i32, i32* @INADDR_ANY
%24 = tail call i8* @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i8* %24, i8** %25
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i64 @bind(i32 %12, %struct.sockaddr* %26, i32 24)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 20000)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %17
%33 = call i64 @listen(i32 %12, i32 10)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %32
%39 = call i32 @FD_ZERO(i32* %3)
%40 = load i32, i32* @STDIN_FILENO
%41 = call i32 @FD_SET(i32 %40, i32* %3)
%42 = call i32 @FD_SET(i32 %12, i32* %3)
%43 = add nuw nsw i32 %12, 1
%44 = load i32, i32* %3
store i32 %44, i32* %4
%45 = call i32 @select(i32 %43, i32* %4, i32* null, i32* null, i32* null)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %65
47: ; preds = %38
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
br label %49
49: ; preds = %47, %61
%50 = load i32, i32* @STDIN_FILENO
%51 = call i64 @FD_ISSET(i32 %50, i32* %4)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = call i32 @gets(i8* %48)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %56
56: ; preds = %53, %49
%57 = call i64 @FD_ISSET(i32 %12, i32* %4)
%58 = icmp eq i64 %57, 0
br i1 %58, label %61, label %59
59: ; preds = %56
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.6, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = load i32, i32* %3
store i32 %62, i32* %4
%63 = call i32 @select(i32 %43, i32* %4, i32* null, i32* null, i32* null)
%64 = icmp sgt i32 %63, 0
br i1 %64, label %49, label %65
65: ; preds = %61, %38
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%67 = call i32 @close(i32 %12)
ret i32 0
}