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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int ret = -1, rsize;
  int serverSock;
  int acceptedSock;
  struct sockaddr_in Addr;
  char readBuf[100];
  socklen_t AddrSize = sizeof(Addr);
  if ((serverSock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    goto leave;
  }
  Addr.sin_family = AF_INET;
  Addr.sin_addr.s_addr = INADDR_ANY;
  Addr.sin_port = htons(3000);
  if ((ret = bind(serverSock, (struct sockaddr *)&Addr, sizeof(Addr)))) {
    perror("bind");
    goto error;
  }
  if ((ret = listen(serverSock, 1))) {
    perror("listen");
    goto error;
  }
  acceptedSock = accept(serverSock, (struct sockaddr *)&Addr, &AddrSize);
  if (acceptedSock == -1) {
    perror("accept");
    ret = -1;
    goto error;
  }
  if ((rsize = recv(acceptedSock, readBuf, 100, 0)) <= 0) {
    perror("recv");
    ret = -1;
  } else {
    printf("Read %d Bytes: '%s'\n", rsize, readBuf);
    ret = 0;
  }
  close(acceptedSock);
error:
  close(serverSock);
leave:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xc,-0x94(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11e9 <main+0x59>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
jmp    1305 <main+0x175>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0xbb8,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     123c <main+0xac>
lea    0xdd5(%rip),%rdi
call   1090 <perror@plt>
jmp    12fd <main+0x16d>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     1266 <main+0xd6>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
jmp    12fd <main+0x16d>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x94(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    129e <main+0x10e>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    12fd <main+0x16d>
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     12d8 <main+0x148>
lea    0xd53(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    12f5 <main+0x165>
mov    -0xc(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xd36(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1281 <main+0xf1>
mov    %eax,%r14d
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0xbb8,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %eax,%eax
jne    1294 <main+0x104>
mov    %r14d,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %eax,%eax
jne    12a4 <main+0x114>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x124>
mov    %eax,%ebx
lea    0x20(%rsp),%rsi
xor    %ebp,%ebp
mov    %eax,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12c7 <main+0x137>
lea    0xdc7(%rip),%rdi
lea    0x20(%rsp),%rdx
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd78(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1273 <main+0xe3>
mov    %eax,%ebp
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
jmp    126b <main+0xdb>
mov    %eax,%ebp
lea    0xd5f(%rip),%rdi
call   1090 <perror@plt>
jmp    126b <main+0xdb>
lea    0xd58(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    126b <main+0xdb>
lea    0xd4c(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1264 <main+0xd4>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@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 = [5 x i8] c"recv\00"
@.str.5 = [21 x i8] c"Read %d Bytes: '%s'\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%7 = load i32, i32* @PF_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 %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %48
13: ; preds = %0
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 3000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @bind(i32 %9, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
25: ; preds = %13
%26 = call i32 @listen(i32 %9, i32 1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %45
30: ; preds = %25
%31 = call i32 @accept(i32 %9, %struct.sockaddr* %20, i32* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %45
35: ; preds = %30
%36 = call i32 @recv(i32 %31, i8* %5, i32 100, i32 0)
%37 = icmp slt i32 %36, 1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %42
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %36, i8* %5)
br label %42
42: ; preds = %40, %38
%43 = phi i32 [ -1, %38 ], [ 0, %40 ]
%44 = call i32 @close(i32 %31)
br label %45
45: ; preds = %42, %33, %28, %23
%46 = phi i32 [ %21, %23 ], [ %26, %28 ], [ -1, %33 ], [ %43, %42 ]
%47 = call i32 @close(i32 %9)
br label %48
48: ; preds = %45, %11
%49 = phi i32 [ -1, %11 ], [ %46, %45 ]
ret i32 %49
}