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

/* Type definitions */
struct sockaddr {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int close(int);
int /*<<< orphan*/ perror(char *);
int prepare_server_socket(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  const char *sendBuff = "Hello, world!\\r\\n";
  int connfd = 0;
  if (argc < 2) {
    perror("Please type <port>\n");
    return -1;
  }
  int listening_socket = prepare_server_socket(argv[1]);
  if (listening_socket == -1) {
    perror("Preparation has filed...\n");
  }
  while (1) {
    connfd = accept(listening_socket, (struct sockaddr *)NULL, NULL);
    if (connfd == -1) {
      perror("Accept error...\n");
      return -1;
    }
    ssize_t written = write(connfd, sendBuff, strlen(sendBuff));
    if (written == -1) {
      perror("No bytes has been written...\n");
    } else if (written > 0) {
      printf(">> Written: %d bytes <<\n", (int)written);
    }
    int close_conf = close(connfd);
    if (close_conf == -1) {
      perror("Cannot close socket...");
      return -1;
    }
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ca <main+0x4a>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <prepare_server_socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f0 <main+0x70>
lea    0xe3b(%rip),%rdi
call   1080 <perror@plt>
jmp    11f5 <main+0x75>
mov    -0x20(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1090 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1229 <main+0xa9>
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <main+0x146>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    126c <main+0xec>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
jmp    128a <main+0x10a>
cmpl   $0x0,-0x24(%rbp)
jle    1285 <main+0x105>
mov    -0x24(%rbp),%esi
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
jmp    128a <main+0x10a>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12b7 <main+0x137>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <main+0x146>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    11f5 <main+0x75>
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
push   %rax
cmp    $0x2,%edi
jge    119c <main+0x1c>
lea    0xe7b(%rip),%r14
jmp    1253 <main+0xd3>
mov    0x8(%rsi),%rdi
call   1070 <prepare_server_socket@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    11b9 <main+0x39>
lea    0xe72(%rip),%rdi
call   1080 <perror@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
lea    0xe74(%rip),%r14
cmp    $0xffffffff,%eax
je     1253 <main+0xd3>
mov    %eax,%ebp
lea    0xe22(%rip),%rbx
lea    0xe6c(%rip),%r15
lea    0xe83(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xc2>
test   %eax,%eax
jle    1217 <main+0x97>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xcc>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f0 <main+0x70>
jmp    1253 <main+0xd3>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1217 <main+0x97>
lea    0xe35(%rip),%r14
mov    %r14,%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Hello, world!\\r\\n\00"
@.str.1 = [20 x i8] c"Please type <port>\0A\00"
@.str.2 = [26 x i8] c"Preparation has filed...\0A\00"
@.str.3 = [17 x i8] c"Accept error...\0A\00"
@.str.4 = [30 x i8] c"No bytes has been written...\0A\00"
@.str.5 = [25 x i8] c">> Written: %d bytes <<\0A\00"
@.str.6 = [23 x i8] c"Cannot close socket...\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 0, i32* %7
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @prepare_server_socket(i8* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
br label %25
25: ; preds = %24, %56
%26 = load i32, i32* %8
%27 = call i32 @accept(i32 %26, %struct.sockaddr* null, i32* null)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
32: ; preds = %25
%33 = load i32, i32* %7
%34 = load i8*, i8** %6
%35 = load i8*, i8** %6
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %49
42: ; preds = %32
%43 = load i32, i32* %9
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* %9
%47 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48, %40
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
56: ; preds = %49
%57 = call i32 @sleep(i32 1)
br label %25
58: ; preds = %54, %30, %13
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Hello, world!\\r\\n\00"
@.str.1 = [20 x i8] c"Please type <port>\0A\00"
@.str.2 = [26 x i8] c"Preparation has filed...\0A\00"
@.str.3 = [17 x i8] c"Accept error...\0A\00"
@.str.4 = [30 x i8] c"No bytes has been written...\0A\00"
@.str.5 = [25 x i8] c">> Written: %d bytes <<\0A\00"
@.str.6 = [23 x i8] c"Cannot close socket...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %32, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @prepare_server_socket(i8* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = tail call i32 @accept(i32 %7, %struct.sockaddr* null, i32* null)
%13 = icmp eq i32 %12, -1
br i1 %13, label %32, label %14
14: ; preds = %11, %28
%15 = phi i32 [ %30, %28 ], [ %12, %11 ]
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @write(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %25
21: ; preds = %14
%22 = icmp sgt i32 %17, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %17)
br label %25
25: ; preds = %21, %23, %19
%26 = tail call i32 @close(i32 %15)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = tail call i32 @sleep(i32 1)
%30 = tail call i32 @accept(i32 %7, %struct.sockaddr* null, i32* null)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %14
32: ; preds = %25, %28, %11, %2
%33 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %11 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %28 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), %25 ]
%34 = tail call i32 @perror(i8* %33)
ret i32 -1
}