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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ WNOHANG;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ socketConnection(int, struct sockaddr_in);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main(void) {
  printf("This is dayserve\n");
  int listenfd;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if (listenfd < 0) {
    perror("socket()");
    exit(1);
  }
  struct sockaddr_in servAddr;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_port = htons(49999);
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(listenfd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    perror("bind()");
    exit(1);
  }
  listen(listenfd, 1);
  while (1) {
    int connectfd;
    int length = sizeof(struct sockaddr_in);
    struct sockaddr_in clientAddr;
    connectfd = accept(listenfd, (struct sockaddr *)&clientAddr, &length);
    if (connectfd < 0) {
      perror("accept()");
      exit(1);
    }
    pid_t childID;
    if (!(childID = fork()))
      socketConnection(connectfd, clientAddr);
    close(connectfd);
    waitpid(childID, NULL, WNOHANG);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1237 <main+0x57>
lea    0xdea(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0xc34f,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12a2 <main+0xc2>
lea    0xd88(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12e9 <main+0x109>
lea    0xd48(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jne    1319 <main+0x139>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   10a0 <socketConnection@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x34(%rbp),%edi
mov    0x2cb5(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <waitpid@plt>
jmp    12af <main+0xcf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de7(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1224 <main+0x44>
lea    0xde1(%rip),%rdi
jmp    12b2 <main+0xd2>
mov    %eax,%r12d
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    $0xc34f,%edi
call   1040 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x18(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    1280 <main+0xa0>
lea    0xd8b(%rip),%rdi
jmp    12b2 <main+0xd2>
mov    %r12d,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r12d,%edi
call   10d0 <accept@plt>
test   %eax,%eax
jns    12c1 <main+0xe1>
lea    0xd5e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%ebp
mov    0x2d16(%rip),%r13
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
jmp    1310 <main+0x130>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   10b0 <waitpid@plt>
movl   $0xc,0x4(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12ab <main+0xcb>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    12e0 <main+0x100>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%edx
mov    %ebp,%edi
call   10a0 <socketConnection@plt>
jmp    12e0 <main+0x100>

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

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