#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*/ addr1;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohl(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sock1, sock2;
  struct sockaddr_in addr1, addr2;
  unsigned short port = 5353;
  int size;
  printf("server started!\n");
  sock1 = socket(AF_INET, SOCK_STREAM, 0);
  if (sock1 < 0) {
    printf("sock failed!\n");
    return -1;
  }
  memset(&addr1, 0, sizeof(addr1));
  addr1.sin_family = AF_INET;
  addr1.sin_addr.s_addr = htonl(INADDR_ANY);
  addr1.sin_port = htons(port);
  if (bind(sock1, (struct sockaddr *)(&addr1), sizeof(struct sockaddr)) < 0) {
    printf("bind failed!\n");
  }
  if (listen(sock1, 5) < 0) {
    printf("listen failed!\n");
  }
  while (1) {
    size = sizeof(struct sockaddr_in);
    sock2 = accept(sock1, (struct sockaddr *)(&addr2), &size);
    if (sock2 < 0) {
      printf("accept failed!\n");
      break;
    }
    printf("accpet success from %#x:%#x", ntohl(addr2.sin_addr.s_addr),
           ntohs(addr2.sin_port));
    if (write(sock2, "hello\n", 8) < 0) {
      printf("write error!\n");
    }
    close(sock2);
  }
  close(sock1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movw   $0x14e9,-0x3a(%rbp)
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    122a <main+0x6a>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <main+0x19b>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
movzwl -0x3a(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    128c <main+0xcc>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12b1 <main+0xf1>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b6 <main+0xf6>
movl   $0xc,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12ed <main+0x12d>
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    134c <main+0x18c>
mov    -0x34(%rbp),%edi
call   10d0 <ntohl@plt>
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd4c(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    133f <main+0x17f>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    12b6 <main+0xf6>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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    0xe3e(%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>
test   %eax,%eax
js     1343 <main+0x173>
mov    %eax,%ebx
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x1c(%rsp)
mov    $0x14e9,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    1269 <main+0x99>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jns    1286 <main+0xb6>
lea    0xdce(%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>
test   %eax,%eax
js     132c <main+0x15c>
mov    %eax,%ebp
lea    0xd6c(%rip),%r15
lea    0x8(%rsp),%r13
lea    0x4(%rsp),%r12
jmp    12e2 <main+0x112>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1090 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     132c <main+0x15c>
mov    0xc(%rsp),%edi
call   10e0 <ntohl@plt>
mov    %eax,%r14d
mov    0x8(%rsp),%edi
call   10b0 <ntohs@plt>
lea    0xd02(%rip),%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    12c0 <main+0xf0>
lea    0xd0e(%rip),%rdi
call   1030 <puts@plt>
jmp    12c0 <main+0xf0>
lea    0xd0d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    1354 <main+0x184>
lea    0xd21(%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 = [17 x i8] c"server started!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [14 x i8] c"sock failed!\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [14 x i8] c"bind failed!\0A\00"
@.str.3 = [16 x i8] c"listen failed!\0A\00"
@.str.4 = [16 x i8] c"accept failed!\0A\00"
@.str.5 = [28 x i8] c"accpet success from %#x:%#x\00"
@.str.6 = [7 x i8] c"hello\0A\00"
@.str.7 = [14 x i8] c"write error!\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 %struct.sockaddr_in
%10 = alloca i16
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i16 5353, i16* %10
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
20: ; preds = %2
%21 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i16, i16* %10
%29 = call i32 @htons(i16 zeroext %28)
%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 i64 @bind(i32 %31, %struct.sockaddr* %32, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %20
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %20
%38 = load i32, i32* %6
%39 = call i64 @listen(i32 %38, i32 5)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
br label %44
44: ; preds = %43, %66
store i32 12, i32* %11
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %11)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %69
52: ; preds = %44
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohl(i32 %55)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @ntohs(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %56, i32 %59)
%61 = load i32, i32* %7
%62 = call i64 @write(i32 %61, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 8)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %52
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %64, %52
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
br label %44
69: ; preds = %50
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
store i32 0, i32* %3
br label %72
72: ; preds = %69, %18
%73 = load i32, i32* %3
ret i32 %73
}

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