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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main(int argc, char **argv) {
  int listenfd, connfd;
  struct sockaddr_in servaddr;
  char buff[4096];
  int n;
  if (-1 == (listenfd = socket(AF_INET, SOCK_STREAM, 0))) {
    printf("Create socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8888);
  if (-1 == bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) {
    printf("Bind socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  if (-1 == listen(listenfd, 10)) {
    printf("Listen socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  printf("===== Waiting for client's request ========\n");
  while (1) {
    if (-1 == (connfd = accept(listenfd, (struct sockaddr *)NULL, NULL))) {
      printf("accept socket error : %s(errno: %d)", strerror(errno), errno);
      continue;
    }
    n = recv(connfd, buff, 4096, 0);
    buff[n] = '\0';
    printf("Recv msg from client : %s\n", buff);
    close(connfd);
  }
  close(listenfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1233 <main+0x73>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12bc <main+0xfc>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1307 <main+0x147>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cd8(%rip),%rax
mov    (%rax),%edx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1363 <main+0x1a3>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c7a(%rip),%rax
mov    (%rax),%edx
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1315 <main+0x155>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1034(%rbp)
movslq -0x1034(%rbp),%rax
movb   $0x0,-0x1030(%rbp,%rax,1)
lea    -0x1030(%rbp),%rsi
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    1315 <main+0x155>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    0x2e00(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x12b>
mov    %eax,%r12d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     1314 <main+0x144>
mov    %r12d,%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
je     132d <main+0x15d>
lea    0xe34(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d47(%rip),%r13
lea    0xde2(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xdfa(%rip),%r15
jmp    12ce <main+0xfe>
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0xd0>
mov    0x0(%r13),%edi
call   10e0 <strerror@plt>
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12ce <main+0xfe>
mov    0x2cc6(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xcee(%rip),%rdi
jmp    1344 <main+0x174>
mov    0x2cad(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xcf9(%rip),%rdi
jmp    1344 <main+0x174>
mov    0x2c94(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd02(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

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
@.str = [36 x i8] c"Create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [34 x i8] c"Bind socket error: %s(errno: %d)\0A\00"
@.str.2 = [36 x i8] c"Listen socket error: %s(errno: %d)\0A\00"
@.str.3 = [45 x i8] c"===== Waiting for client's request ========\0A\00"
@.str.4 = [36 x i8] c"accept socket error : %s(errno: %d)\00"
@.str.5 = [27 x i8] c"Recv msg from client : %s\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 %struct.sockaddr_in
%9 = alloca [4096 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = icmp eq i32 -1, %13
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 8888)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = load i32, i32* @errno
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %37, i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %21
%42 = load i32, i32* %6
%43 = call i32 @listen(i32 %42, i32 10)
%44 = icmp eq i32 -1, %43
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = load i32, i32* @errno
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %48)
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %41
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %57, %62
%54 = load i32, i32* %6
%55 = call i32 @accept(i32 %54, %struct.sockaddr* null, i32* null)
store i32 %55, i32* %7
%56 = icmp eq i32 -1, %55
br i1 %56, label %57, label %62
57: ; preds = %53
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = load i32, i32* @errno
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %59, i32 %60)
br label %53
62: ; preds = %53
%63 = load i32, i32* %7
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%65 = call i32 @recv(i32 %63, i8* %64, i32 4096, i32 0)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %67
store i8 0, i8* %68
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %69)
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
br label %53
}

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
@.str = [36 x i8] c"Create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [34 x i8] c"Bind socket error: %s(errno: %d)\0A\00"
@.str.2 = [36 x i8] c"Listen socket error: %s(errno: %d)\0A\00"
@.str.4 = [36 x i8] c"accept socket error : %s(errno: %d)\00"
@.str.5 = [27 x i8] c"Recv msg from client : %s\0A\00"
@str = [44 x i8] c"===== Waiting for client's request ========\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = load i32, i32* @errno
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = call i32 @htons(i32 8888)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %9, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %17
%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 ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %17
%36 = call i32 @listen(i32 %9, i32 10)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %41)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %35
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %54, %44
%47 = call i32 @accept(i32 %9, %struct.sockaddr* null, i32* null)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = load i32, i32* @errno
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %51, i32 %52)
br label %54
54: ; preds = %49, %55
br label %46
55: ; preds = %46
%56 = call i32 @recv(i32 %47, i8* %6, i32 4096, i32 0)
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %57
store i8 0, i8* %58
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%60 = call i32 @close(i32 %47)
br label %54
}