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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr const *, int);
int htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int do_run() {
  int server_sockfd, client_sockfd;
  int server_len, client_len;
  struct sockaddr_in server_address;
  struct sockaddr_in client_address;
  fd_set readfds, testfds;
  int result;
  server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  server_address.sin_port = htons(9876);
  server_len = sizeof(server_address);
  bind(server_sockfd, (const struct sockaddr *)&server_address, server_len);
  listen(server_sockfd, 5);
  FD_ZERO(&readfds);
  FD_SET(server_sockfd, &readfds);
  while (1) {
    testfds = readfds;
    printf("server waitting......\n");
    result = select(FD_SETSIZE, &testfds, NULL, NULL, NULL);
    if (result < 1)
      perror("select error : ");
    for (int fd = 0; fd < FD_SETSIZE; fd++) {
      if (!FD_ISSET(fd, &testfds))
        continue;
      if (fd == server_sockfd) {
        client_len = sizeof(struct sockaddr_in);
        client_sockfd = accept(server_sockfd,
                               (struct sockaddr *)&client_address, &client_len);
        FD_SET(client_sockfd, &readfds);
        printf("adding client on fd[%d] with ip=%x port=%d\n", client_sockfd,
               client_address.sin_addr.s_addr, ntohs(client_address.sin_port));
      } else {
        char ch = '?';
        read(fd, &ch, 1);
        printf("client[fd=%d ip=%x port=%d] : %c\n", client_sockfd,
               client_address.sin_addr.s_addr, ntohs(client_address.sin_port),
               ch);
      }
    }
    sleep(6);
  }
}


x86 assembly (clang, -O0):
<do_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x2694,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
movl   $0xc,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10c0 <bind@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x3c(%rbp)
jge    12c0 <do_run+0xd0>
lea    0xd5c(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%eax
jge    13ab <do_run+0x1bb>
mov    -0x40(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    12f3 <do_run+0x103>
jmp    139d <do_run+0x1ad>
mov    -0x40(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    1354 <do_run+0x164>
movl   $0xc,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%edx
mov    %eax,%ecx
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1398 <do_run+0x1a8>
movb   $0x3f,-0x41(%rbp)
mov    -0x40(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%edx
mov    %eax,%ecx
movsbl -0x41(%rbp),%r8d
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    139d <do_run+0x1ad>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12c7 <do_run+0xd7>
mov    $0x6,%edi
call   1100 <sleep@plt>
jmp    1276 <do_run+0x86>

x86 assembly (clang, -O2):
<do_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2dd3(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%eax
mov    %eax,0x30(%rsp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x2c(%rsp)
mov    $0x2694,%edi
call   1050 <htons@plt>
mov    %eax,0x28(%rsp)
lea    0x28(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    %r12d,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1100 <FD_SET@plt>
mov    0x2d38(%rip),%rbx
lea    0x10(%rsp),%r14
jmp    12aa <do_run+0xaa>
nopw   0x0(%rax,%rax,1)
mov    $0x6,%edi
call   1110 <sleep@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
jg     12df <do_run+0xdf>
lea    0xd26(%rip),%rdi
call   10e0 <perror@plt>
cmpl   $0x0,(%rbx)
jle    12a0 <do_run+0xa0>
xor    %r15d,%r15d
jmp    133c <do_run+0x13c>
nopl   0x0(%rax)
movb   $0x3f,0xb(%rsp)
mov    %r15d,%edi
lea    0xb(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    0x18(%rsp),%edi
mov    0x1c(%rsp),%ebp
call   10c0 <ntohs@plt>
movsbl 0xb(%rsp),%r8d
lea    0xd1b(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%r15d
cmp    (%rbx),%r15d
jge    12a0 <do_run+0xa0>
mov    %r15d,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     132f <do_run+0x12f>
cmp    %r15d,%r12d
jne    12f0 <do_run+0xf0>
movl   $0xc,0x14(%rsp)
mov    %r12d,%edi
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   10f0 <accept@plt>
mov    %eax,%r13d
mov    %eax,%edi
lea    0xc(%rsp),%rsi
call   1100 <FD_SET@plt>
mov    0x18(%rsp),%edi
mov    %r14,%rbp
mov    0x1c(%rsp),%r14d
call   10c0 <ntohs@plt>
lea    0xc7f(%rip),%rdi
mov    %r13d,%esi
mov    %r14d,%edx
mov    %rbp,%r14
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    132f <do_run+0x12f>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [23 x i8] c"server waitting......\0A\00"
@FD_SETSIZE = global i32 0
@.str.1 = [16 x i8] c"select error : \00"
@.str.2 = [44 x i8] c"adding client on fd[%d] with ip=%x port=%d\0A\00"
@.str.3 = [34 x i8] c"client[fd=%d ip=%x port=%d] : %c\0A\00"
define i32 @do_run() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %1
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = call i32 @htons(i32 9876)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %21, i32* %22
store i32 12, i32* %3
%23 = load i32, i32* %1
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = load i32, i32* %3
%26 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 %25)
%27 = load i32, i32* %1
%28 = call i32 @listen(i32 %27, i32 5)
%29 = call i32 @FD_ZERO(i32* %7)
%30 = load i32, i32* %1
%31 = call i32 @FD_SET(i32 %30, i32* %7)
br label %32
32: ; preds = %0, %86
%33 = load i32, i32* %7
store i32 %33, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%35 = load i32, i32* @FD_SETSIZE
%36 = call i32 @select(i32 %35, i32* %8, i32* null, i32* null, i32* null)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
store i32 0, i32* %10
br label %42
42: ; preds = %83, %41
%43 = load i32, i32* %10
%44 = load i32, i32* @FD_SETSIZE
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %86
46: ; preds = %42
%47 = load i32, i32* %10
%48 = call i32 @FD_ISSET(i32 %47, i32* %8)
%49 = icmp ne i32 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %46
br label %83
51: ; preds = %46
%52 = load i32, i32* %10
%53 = load i32, i32* %1
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %69
55: ; preds = %51
store i32 12, i32* %4
%56 = load i32, i32* %1
%57 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%58 = call i32 @accept(i32 %56, %struct.sockaddr* %57, i32* %4)
store i32 %58, i32* %2
%59 = load i32, i32* %2
%60 = call i32 @FD_SET(i32 %59, i32* %7)
%61 = load i32, i32* %2
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @ntohs(i32 %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %61, i32 %64, i32 %67)
br label %82
69: ; preds = %51
store i8 63, i8* %11
%70 = load i32, i32* %10
%71 = call i32 @read(i32 %70, i8* %11, i32 1)
%72 = load i32, i32* %2
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%74 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @ntohs(i32 %77)
%79 = load i8, i8* %11
%80 = sext i8 %79 to i32
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 %75, i32 %78, i32 %80)
br label %82
82: ; preds = %69, %55
br label %83
83: ; preds = %82, %50
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 1
store i32 %85, i32* %10
br label %42
86: ; preds = %42
%87 = call i32 @sleep(i32 6)
br label %32
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [16 x i8] c"select error : \00"
@.str.2 = [44 x i8] c"adding client on fd[%d] with ip=%x port=%d\0A\00"
@.str.3 = [34 x i8] c"client[fd=%d ip=%x port=%d] : %c\0A\00"
@str = [22 x i8] c"server waitting......\00"
define i32 @do_run() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = bitcast i32* %1 to i8*
%8 = bitcast %struct.sockaddr_in* %2 to i8*
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 9876)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %14, %struct.sockaddr* %22, i32 12)
%24 = call i32 @listen(i32 %14, i32 5)
%25 = call i32 @FD_ZERO(i32* %4)
%26 = call i32 @FD_SET(i32 %14, i32* %4)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %30
30: ; preds = %42, %0
%31 = phi i32 [ undef, %0 ], [ %43, %42 ]
%32 = load i32, i32* %4
store i32 %32, i32* %5
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%34 = load i32, i32* @FD_SETSIZE
%35 = call i32 @select(i32 %34, i32* %5, i32* null, i32* null, i32* null)
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %30
%40 = load i32, i32* @FD_SETSIZE
%41 = icmp sgt i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %67, %39
%43 = phi i32 [ %31, %39 ], [ %68, %67 ]
%44 = call i32 @sleep(i32 6)
br label %30
45: ; preds = %39, %67
%46 = phi i32 [ %69, %67 ], [ 0, %39 ]
%47 = phi i32 [ %68, %67 ], [ %31, %39 ]
%48 = call i32 @FD_ISSET(i32 %46, i32* %5)
%49 = icmp eq i32 %48, 0
br i1 %49, label %67, label %50
50: ; preds = %45
%51 = icmp eq i32 %46, %14
br i1 %51, label %52, label %59
52: ; preds = %50
store i32 12, i32* %1
%53 = call i32 @accept(i32 %14, %struct.sockaddr* %29, i32* %1)
%54 = call i32 @FD_SET(i32 %53, i32* %4)
%55 = load i32, i32* %27
%56 = load i32, i32* %28
%57 = call i32 @ntohs(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %53, i32 %55, i32 %57)
br label %67
59: ; preds = %50
store i8 63, i8* %6
%60 = call i32 @read(i32 %46, i8* %6, i32 1)
%61 = load i32, i32* %27
%62 = load i32, i32* %28
%63 = call i32 @ntohs(i32 %62)
%64 = load i8, i8* %6
%65 = sext i8 %64 to i32
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 %61, i32 %63, i32 %65)
br label %67
67: ; preds = %52, %59, %45
%68 = phi i32 [ %53, %52 ], [ %47, %59 ], [ %47, %45 ]
%69 = add nuw nsw i32 %46, 1
%70 = load i32, i32* @FD_SETSIZE
%71 = icmp slt i32 %69, %70
br i1 %71, label %45, label %42
}