#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int listen(int, int);
int ntohl(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main() {
  int sfp, nfp;
  struct sockaddr_in s_add, c_add;
  int sin_size;
  unsigned short portnum = 0x8888;
  printf("server starting.\n");
  sfp = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == sfp) {
    printf("socket fail ! \r\n");
    return -1;
  }
  printf("socket ok !\r\n");
  bzero(&s_add, sizeof(struct sockaddr_in));
  s_add.sin_family = AF_INET;
  s_add.sin_addr.s_addr = htonl(INADDR_ANY);
  s_add.sin_port = htons(portnum);
  if (-1 == bind(sfp, (struct sockaddr *)(&s_add), sizeof(struct sockaddr))) {
    printf("bind fail !\r\n");
    return -1;
  }
  printf("bind ok !\r\n");
  if (-1 == listen(sfp, 5)) {
    printf("listen fail !\r\n");
    return -1;
  }
  printf("listen ok\r\n");
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
    if (-1 == nfp) {
      printf("accept fail !\r\n");
      return -1;
    }
    printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",
           ntohl(c_add.sin_addr.s_addr), ntohs(c_add.sin_port));
    if (-1 == write(nfp, "hello,welcome to my server \r\n", 32)) {
      printf("write fail!\r\n");
      return -1;
    }
    printf("write ok!\r\n");
    close(nfp);
  }
  close(sfp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movw   $0x8888,-0x2e(%rbp)
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1227 <main+0x67>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b8 <main+0x1f8>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x14(%rbp)
movzwl -0x2e(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12a6 <main+0xe6>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b8 <main+0x1f8>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12ea <main+0x12a>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b8 <main+0x1f8>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    133a <main+0x17a>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b8 <main+0x1f8>
mov    -0x24(%rbp),%edi
call   10d0 <ntohl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xd43(%rip),%rsi
mov    $0x20,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    139d <main+0x1dd>
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b8 <main+0x1f8>
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    12f8 <main+0x138>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df7(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1344 <main+0x174>
mov    %eax,%ebx
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x1c(%rsp)
mov    $0x8888,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     134d <main+0x17d>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     1356 <main+0x186>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     133b <main+0x16b>
mov    %eax,%ebp
lea    0xd72(%rip),%r15
lea    0x8(%rsp),%r13
lea    0x4(%rsp),%r14
xchg   %ax,%ax
mov    0xc(%rsp),%edi
call   10e0 <ntohl@plt>
mov    %eax,%r12d
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xd14(%rip),%rdi
mov    %r12d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x20,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     135f <main+0x18f>
lea    0xd75(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12d0 <main+0x100>
lea    0xd5e(%rip),%rdi
jmp    1366 <main+0x196>
lea    0xd80(%rip),%rdi
jmp    1366 <main+0x196>
lea    0xd6a(%rip),%rdi
jmp    1366 <main+0x196>
lea    0xd52(%rip),%rdi
jmp    1366 <main+0x196>
lea    0xd2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"server starting.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"socket fail ! \0D\0A\00"
@.str.2 = [14 x i8] c"socket ok !\0D\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [14 x i8] c"bind fail !\0D\0A\00"
@.str.4 = [12 x i8] c"bind ok !\0D\0A\00"
@.str.5 = [16 x i8] c"listen fail !\0D\0A\00"
@.str.6 = [12 x i8] c"listen ok\0D\0A\00"
@.str.7 = [16 x i8] c"accept fail !\0D\0A\00"
@.str.8 = [54 x i8] c"accept ok!\0D\0AServer start get connect from %#x : %#x\0D\0A\00"
@.str.9 = [30 x i8] c"hello,welcome to my server \0D\0A\00"
@.str.10 = [14 x i8] c"write fail!\0D\0A\00"
@.str.11 = [12 x i8] c"write ok!\0D\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i16
store i32 0, i32* %1
store i16 -30584, i16* %7
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %69
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 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* %4, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i16, i16* %7
%26 = call i32 @htons(i16 zeroext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %34
32: ; preds = %16
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %69
34: ; preds = %16
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %2
%37 = call i32 @listen(i32 %36, i32 5)
%38 = icmp eq i32 -1, %37
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %69
41: ; preds = %34
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %65
store i32 12, i32* %6
%44 = load i32, i32* %2
%45 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %6)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = icmp eq i32 -1, %47
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %69
51: ; preds = %43
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @ntohl(i32 %54)
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @ntohs(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), i32 %55, i32 %58)
%60 = load i32, i32* %3
%61 = call i32 @write(i32 %60, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i32 32)
%62 = icmp eq i32 -1, %61
br i1 %62, label %63, label %65
63: ; preds = %51
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0))
store i32 -1, i32* %1
br label %69
65: ; preds = %51
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
%67 = load i32, i32* %3
%68 = call i32 @close(i32 %67)
br label %43
69: ; preds = %63, %49, %39, %32, %14
%70 = load i32, i32* %1
ret i32 %70
}

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
@.str.8 = [54 x i8] c"accept ok!\0D\0AServer start get connect from %#x : %#x\0D\0A\00"
@.str.9 = [30 x i8] c"hello,welcome to my server \0D\0A\00"
@str = [17 x i8] c"server starting.\00"
@str.12 = [13 x i8] c"socket ok !\0D\00"
@str.13 = [11 x i8] c"bind ok !\0D\00"
@str.14 = [11 x i8] c"listen ok\0D\00"
@str.15 = [11 x i8] c"write ok!\0D\00"
@str.16 = [13 x i8] c"write fail!\0D\00"
@str.17 = [15 x i8] c"accept fail !\0D\00"
@str.18 = [15 x i8] c"listen fail !\0D\00"
@str.19 = [13 x i8] c"bind fail !\0D\00"
@str.20 = [16 x i8] c"socket fail ! \0D\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.20, i64 0, i64 0))
br label %61
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.12, i64 0, i64 0))
%16 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = tail call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i16 zeroext -30584)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i32 @bind(i32 %10, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.19, i64 0, i64 0))
br label %61
29: ; preds = %14
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.13, i64 0, i64 0))
%31 = call i32 @listen(i32 %10, i32 5)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.18, i64 0, i64 0))
br label %61
35: ; preds = %29
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.14, i64 0, i64 0))
%37 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
store i32 12, i32* %3
%38 = call i32 @accept(i32 %10, %struct.sockaddr* %37, i32* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %45
43: ; preds = %56, %35
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.17, i64 0, i64 0))
br label %61
45: ; preds = %40, %56
%46 = phi i32 [ %38, %40 ], [ %59, %56 ]
%47 = load i32, i32* %41
%48 = call i32 @ntohl(i32 %47)
%49 = load i32, i32* %42
%50 = call i32 @ntohs(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), i32 %48, i32 %50)
%52 = call i32 @write(i32 %46, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i32 32)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.16, i64 0, i64 0))
br label %61
56: ; preds = %45
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.15, i64 0, i64 0))
%58 = call i32 @close(i32 %46)
store i32 12, i32* %3
%59 = call i32 @accept(i32 %10, %struct.sockaddr* %37, i32* %3)
%60 = icmp eq i32 %59, -1
br i1 %60, label %43, label %45
61: ; preds = %54, %43, %33, %27, %12
ret i32 -1
}