#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 */
typedef int /*<<< orphan*/ tcp_server;
typedef int /*<<< orphan*/ tcp_client;
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;

/* 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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(void) {
  int test_sock, in_sock;
  socklen_t len_client;
  char msg[] = "Hello, OpenWRT!!!\n";
  struct sockaddr_in tcp_server, tcp_client;
  test_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (test_sock < 0) {
    printf("Socket didn't open :(\n");
  } else {
    printf("Socket opened :)\n");
  }
  tcp_server.sin_family = AF_INET;
  tcp_server.sin_addr.s_addr = htonl(INADDR_ANY);
  tcp_server.sin_port = htons(17231);
  if (bind(test_sock, (struct sockaddr *)&tcp_server, sizeof(tcp_server)) < 0) {
    printf("Error binding socket\n");
  } else {
    printf("Socket bound :)\n");
  }
  printf("Waiting for client to connect...\n");
  if (listen(test_sock, 0) == -1) {
    printf("Unable to listen :(\n");
  }
  len_client = sizeof(tcp_client);
  in_sock = accept(test_sock, (struct sockaddr *)&tcp_client, &len_client);
  if (in_sock < 0) {
    printf("Unable to accept connection :(\n");
  }
  send(in_sock, msg, strlen(msg), 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe54(%rip),%ax
mov    %ax,-0x20(%rbp)
mov    0xe4c(%rip),%al
mov    %al,-0x1e(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1200 <main+0x70>
lea    0xe1d(%rip),%rdi
call   1060 <printf@plt>
jmp    120c <main+0x7c>
lea    0xe23(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x434f,%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1262 <main+0xd2>
lea    0xde4(%rip),%rdi
call   1060 <printf@plt>
jmp    126e <main+0xde>
lea    0xde9(%rip),%rdi
call   1060 <printf@plt>
lea    0xdee(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1299 <main+0x109>
lea    0xdf1(%rip),%rdi
call   1060 <printf@plt>
movl   $0xc,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12c9 <main+0x139>
lea    0xdd6(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
movaps 0xe62(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movl   $0xa2121,0x2f(%rsp)
mov    0x2e36(%rip),%r14
mov    (%r14),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
lea    0xecd(%rip),%rax
lea    0xe3c(%rip),%rdi
cmovs  %rax,%rdi
call   1030 <puts@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x434f,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
lea    0xe66(%rip),%rax
lea    0xdfb(%rip),%rdi
cmovs  %rax,%rdi
call   1030 <puts@plt>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0xc8>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,0xc(%rsp)
lea    0x38(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1283 <main+0xf3>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.msg = [19 x i8] c"Hello, OpenWRT!!!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Socket didn't open :(\0A\00"
@.str.1 = [18 x i8] c"Socket opened :)\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [22 x i8] c"Error binding socket\0A\00"
@.str.3 = [17 x i8] c"Socket bound :)\0A\00"
@.str.4 = [34 x i8] c"Waiting for client to connect...\0A\00"
@.str.5 = [21 x i8] c"Unable to listen :(\0A\00"
@.str.6 = [32 x i8] c"Unable to accept connection :(\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [19 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%8 = bitcast [19 x i8]* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %18
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @htons(i32 17231)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %18
%32 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %35
33: ; preds = %18
%34 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
%36 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = call i32 @listen(i32 %37, i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
store i32 12, i32* %4
%43 = load i32, i32* %2
%44 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %4)
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
%51 = load i32, i32* %3
%52 = getelementptr inbounds [19 x i8], [19 x i8]* %5, i64 0, i64 0
%53 = getelementptr inbounds [19 x i8], [19 x i8]* %5, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @send(i32 %51, i8* %52, i32 %54, i32 0)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.msg = [19 x i8] c"Hello, OpenWRT!!!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@str = [17 x i8] c"Socket opened :)\00"
@str.7 = [16 x i8] c"Socket bound :)\00"
@str.8 = [33 x i8] c"Waiting for client to connect...\00"
@str.9 = [31 x i8] c"Unable to accept connection :(\00"
@str.10 = [20 x i8] c"Unable to listen :(\00"
@str.11 = [21 x i8] c"Error binding socket\00"
@str.12 = [22 x i8] c"Socket didn't open :(\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [19 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
%6 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
%13 = select i1 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @str.12, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0)
%14 = tail call i32 @puts(i8* dereferenceable(1) %13)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 17231)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %11, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @str.11, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0)
%26 = call i32 @puts(i8* dereferenceable(1) %25)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
%28 = call i32 @listen(i32 %11, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %0
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
br label %32
32: ; preds = %30, %0
store i32 12, i32* %1
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @accept(i32 %11, %struct.sockaddr* %33, i32* %1)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.9, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = call i32 @strlen(i8* %6)
%40 = call i32 @send(i32 %34, i8* %6, i32 %39, i32 0)
ret i32 0
}