#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;
  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 ((ret = recv(acceptedSock, readBuf, 100, 0)) <= 0) {
    perror("recv");
    ret = -1;
  } else
    printf("Read %d Bytes: '%s'\n", ret, readBuf);
  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,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e9 <main+0x59>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
jmp    12fe <main+0x16e>
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    -0xc(%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    12f6 <main+0x166>
mov    -0xc(%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    12f6 <main+0x166>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x94(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    129e <main+0x10e>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    12f6 <main+0x166>
mov    -0x10(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jg     12d8 <main+0x148>
lea    0xd53(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    12ee <main+0x15e>
mov    -0x8(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xd36(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%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
mov    %eax,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12c7 <main+0x137>
mov    %eax,%ebp
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 %struct.sockaddr_in
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
store i32 12, i32* %7
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %59
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 3000)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %3
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
store i32 %24, i32* %2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %56
28: ; preds = %14
%29 = load i32, i32* %3
%30 = call i32 @listen(i32 %29, i32 1)
store i32 %30, i32* %2
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %56
34: ; preds = %28
%35 = load i32, i32* %3
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = call i32 @accept(i32 %35, %struct.sockaddr* %36, i32* %7)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = 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 %56
42: ; preds = %34
%43 = load i32, i32* %4
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%45 = call i32 @recv(i32 %43, i8* %44, i32 100, i32 0)
store i32 %45, i32* %2
%46 = icmp sle i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = 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 %53
49: ; preds = %42
%50 = load i32, i32* %2
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%52 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %50, i8* %51)
br label %53
53: ; preds = %49, %47
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %40, %32, %26
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %12
%60 = load i32, i32* %2
ret i32 %60
}

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 ], [ %36, %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
}