#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 socklen_t;
typedef int /*<<< orphan*/ listen_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int listen_fd, real_fd;
  struct sockaddr_in listen_addr, client_addr;
  socklen_t len = sizeof(struct sockaddr_in);
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd == -1) {
    perror("socket failed");
    return -1;
  }
  bzero(&listen_addr, sizeof(listen_addr));
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  listen_addr.sin_port = htons(9527);
  bind(listen_fd, (struct sockaddr *)&listen_addr, len);
  listen(listen_fd, 50);
  while (1) {
    real_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &len);
    if (real_fd == -1) {
      perror("accpet failed");
      return -1;
    }
    if (fork() == 0) {
      close(listen_fd);
      char pcContent[1024];
      read(real_fd, pcContent, 1024);
      printf("connected\n");
      close(real_fd);
      exit(0);
    }
    close(real_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc,-0x3c(%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)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122e <main+0x5e>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130c <main+0x13c>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x2537,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1090 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x32,%esi
call   1080 <listen@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12b7 <main+0xe7>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130c <main+0x13c>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%rax
jne    12ff <main+0x12f>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xd31(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    1282 <main+0xb2>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x438,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2dfc(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0x40>
lea    0xdf5(%rip),%rdi
jmp    12bf <main+0xef>
mov    %eax,%r15d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x2537,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %r15d,%edi
mov    $0x32,%esi
call   1080 <listen@plt>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     12b8 <main+0xe8>
mov    %eax,%ebp
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%rbx
nopl   0x0(%rax)
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     12d7 <main+0x107>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1290 <main+0xc0>
lea    0xd4f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1060 <close@plt>
lea    0x30(%rsp),%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xd25(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@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 = [14 x i8] c"socket failed\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"accpet failed\00"
@.str.2 = [11 x i8] c"connected\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 i32
%11 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %10
%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* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
19: ; preds = %2
%20 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 9527)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = load i32, i32* %10
%32 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @listen(i32 %33, i32 50)
br label %35
35: ; preds = %19, %56
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%38 = call i32 @accept(i32 %36, %struct.sockaddr* %37, i32* %10)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
43: ; preds = %35
%44 = call i64 (...) @fork()
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %7
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 1024)
%52 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %43
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
br label %35
59: ; preds = %41, %17
%60 = load i32, i32* %3
ret i32 %60
}

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 = [14 x i8] c"socket failed\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"accpet failed\00"
@str = [10 x i8] c"connected\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%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 eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %48
16: ; preds = %2
%17 = call i32 @bzero(%struct.sockaddr_in* %3, 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 = tail 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 = tail call i32 @htons(i32 9527)
%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 i32 @bind(i32 %12, %struct.sockaddr* %25, i32 12)
%27 = call i32 @listen(i32 %12, i32 50)
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i32 @accept(i32 %12, %struct.sockaddr* %28, i32* %5)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %44, %16
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %48
33: ; preds = %16, %44
%34 = phi i32 [ %46, %44 ], [ %29, %16 ]
%35 = call i64 (...) @fork()
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = call i32 @close(i32 %12)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %34, i8* %39, i32 1024)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%42 = call i32 @close(i32 %34)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %33
%45 = call i32 @close(i32 %34)
%46 = call i32 @accept(i32 %12, %struct.sockaddr* %28, i32* %5)
%47 = icmp eq i32 %46, -1
br i1 %47, label %31, label %33
48: ; preds = %31, %14
ret i32 -1
}