#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*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ handle_connection(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sfd, cfd;
  int n;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t clilen;
  sfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sfd < 0) {
    printf("Socket could not be created!\n");
    exit(1);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(5000);
  n = bind(sfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (n != 0) {
    printf("Could not bind port!\n");
    perror("bind");
    exit(1);
  }
  listen(sfd, 10);
  printf("Start listening...\n");
  clilen = sizeof(cliaddr);
  for (;;) {
    cfd = accept(sfd, (struct sockaddr *)&cliaddr, &clilen);
    printf("incoming connection (fd = %d)!\n", cfd);
    handle_connection(cfd);
    close(cfd);
    printf("connection closed (fd = %d)\n", cfd);
  }
  close(sfd);
  return 0;
}


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)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1214 <main+0x54>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x1388,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     128e <main+0xce>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdb5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <handle_connection@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%esi
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b0 <main+0xf0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e03(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12c8 <main+0xf8>
mov    %eax,%r13d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x1388,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
jne    12de <main+0x10e>
mov    %r13d,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd7f(%rip),%r12
lea    0xd98(%rip),%rbx
nopl   (%rax)
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <handle_connection@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1290 <main+0xc0>
lea    0xd9b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd70(%rip),%rdi
call   1030 <puts@plt>
lea    0xd0f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"Socket could not be created!\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Could not bind port!\0A\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [20 x i8] c"Start listening...\0A\00"
@.str.4 = [32 x i8] c"incoming connection (fd = %d)!\0A\00"
@.str.5 = [29 x i8] c"connection closed (fd = %d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
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* %9, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 5000)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %20
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %20
%40 = load i32, i32* %6
%41 = call i32 @listen(i32 %40, i32 10)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 12, i32* %11
br label %43
43: ; preds = %43, %39
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %11)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %7
%50 = call i32 @handle_connection(i32 %49)
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %7
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %53)
br label %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.4 = [32 x i8] c"incoming connection (fd = %d)!\0A\00"
@.str.5 = [29 x i8] c"connection closed (fd = %d)\0A\00"
@str = [19 x i8] c"Start listening...\00"
@str.6 = [21 x i8] c"Could not bind port!\00"
@str.7 = [29 x i8] c"Socket could not be created!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 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
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.7, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 5000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %11, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %16
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %16
%33 = call i32 @listen(i32 %11, i32 10)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
store i32 12, i32* %5
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %36
36: ; preds = %36, %32
%37 = call i32 @accept(i32 %11, %struct.sockaddr* %35, i32* %5)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = call i32 @handle_connection(i32 %37)
%40 = call i32 @close(i32 %37)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %37)
br label %36
}