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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ WNOHANG;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void main() {
  int sockfd, new_fd;
  struct sockaddr_in sa;
  struct sockaddr_in their_addr;
  int sin_size;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  sa.sin_family = AF_INET;
  sa.sin_port = htons(7770);
  sa.sin_addr.s_addr = INADDR_ANY;
  bzero(&(sa.sin_zero), 8);
  if (bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen");
    exit(1);
  }
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    if (new_fd == -1) {
      perror("accept");
      continue;
    }
    if (fork() == 0) {
      if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
        perror("send");
      close(new_fd);
      exit(0);
    }
    close(new_fd);
    while (waitpid(-1, NULL, WNOHANG) > 0)
      ;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    1203 <main+0x43>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x1e5a,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1266 <main+0xa6>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1292 <main+0xd2>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1297 <main+0xd7>
movl   $0x10,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12cc <main+0x10c>
lea    0xd51(%rip),%rdi
call   1090 <perror@plt>
jmp    1297 <main+0xd7>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    1317 <main+0x157>
mov    -0x8(%rbp),%edi
lea    0xd33(%rip),%rsi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
jne    1308 <main+0x148>
lea    0xd26(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <waitpid@plt>
cmp    $0x0,%rax
jle    1345 <main+0x185>
jmp    131f <main+0x15f>
jmp    1297 <main+0xd7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e13(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11f5 <main+0x35>
lea    0xe0d(%rip),%rdi
jmp    1262 <main+0xa2>
mov    %eax,%r13d
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    $0x1e5a,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1249 <main+0x89>
lea    0xdc0(%rip),%rdi
jmp    1262 <main+0xa2>
mov    %r13d,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1271 <main+0xb1>
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd91(%rip),%r12
mov    0x2d57(%rip),%rbx
jmp    12a0 <main+0xe0>
mov    %r12,%rdi
call   1090 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0x10,0x4(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
je     128b <main+0xcb>
mov    %eax,%ebp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     12e5 <main+0x125>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rbx),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1080 <waitpid@plt>
test   %rax,%rax
jg     12d0 <main+0x110>
jmp    12a0 <main+0xe0>
lea    0xd2e(%rip),%rsi
mov    %ebp,%edi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
jne    130b <main+0x14b>
lea    0xd23(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [15 x i8] c"Hello, world!\0A\00"
@.str.5 = [5 x i8] c"send\00"
@WNOHANG = global i32 0
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 3
store i32 %14, i32* %15
%16 = call i32 @htons(i32 7770)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%22 = call i32 @bzero(i32* %21, i32 8)
%23 = load i32, i32* %1
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 16)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %13
%31 = load i32, i32* %1
%32 = call i32 @listen(i32 %31, i32 10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
br label %38
38: ; preds = %37, %44, %67
store i32 16, i32* %5
%39 = load i32, i32* %1
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i32* %5)
store i32 %41, i32* %2
%42 = load i32, i32* %2
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %38
46: ; preds = %38
%47 = call i64 (...) @fork()
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %46
%50 = load i32, i32* %2
%51 = call i32 @send(i32 %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 14, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = load i32, i32* %2
%57 = call i32 @close(i32 %56)
%58 = call i32 @exit(i32 0)
unreachable
59: ; preds = %46
%60 = load i32, i32* %2
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %66, %59
%63 = load i32, i32* @WNOHANG
%64 = call i64 @waitpid(i32 -1, i32* null, i32 %63)
%65 = icmp sgt i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %62
br label %62
67: ; preds = %62
br label %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [15 x i8] c"Hello, world!\0A\00"
@.str.5 = [5 x i8] c"send\00"
@WNOHANG = global i32 0
define void @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 = 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 %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 7770)
%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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%22 = call i32 @bzero(i32* %21, i32 8)
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @bind(i32 %9, %struct.sockaddr* %23, i32 16)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %14
%30 = call i32 @listen(i32 %9, i32 10)
%31 = icmp eq i32 %30, -1
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %42, %32
store i32 16, i32* %3
%38 = call i32 @accept(i32 %9, %struct.sockaddr* %33, i32* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %56, %40
br label %37
43: ; preds = %37
%44 = call i64 (...) @fork()
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = call i32 @send(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 14, i32 0)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = call i32 @close(i32 %38)
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %43
%55 = call i32 @close(i32 %38)
br label %56
56: ; preds = %56, %54
%57 = load i32, i32* @WNOHANG
%58 = call i64 @waitpid(i32 -1, i32* null, i32 %57)
%59 = icmp sgt i64 %58, 0
br i1 %59, label %56, label %42
}