#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ handle_connection(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void serve() {
  int opt = +1;
  int serv = -1;
  int conn = -1;
  int conn_addr_size = 0;
  struct sockaddr_in serv_addr = {0};
  struct sockaddr_in conn_addr = {0};
  serv = socket(AF_INET, SOCK_STREAM, 0);
  if (serv == -1)
    do {
      printf("ERROR: "
             "failed to create socket"
             ": %s.\n",
             strerror(errno));
      goto CLEANUP;
    } while (0);
  setsockopt(serv, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(8000);
  serv_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
  if (bind(serv, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))
    do {
      printf("ERROR: "
             "failed to bind to %s:%d"
             ": %s.\n",
             "0.0.0.0", 8000, strerror(errno));
      goto CLEANUP;
    } while (0);
  if (listen(serv, 5) != 0)
    do {
      printf("ERROR: "
             "failed to listen"
             ": %s.\n",
             strerror(errno));
      goto CLEANUP;
    } while (0);
  printf("Listening on %s:%d"
         ".\n",
         "0.0.0.0", 8000);
  while (1) {
    conn = accept(serv, (struct sockaddr *)&conn_addr, &conn_addr_size);
    if (conn == -1)
      do {
        printf("ERROR: "
               "failed to accept connection"
               ": %s.\n",
               strerror(errno));
        goto CLEANUP;
      } while (0);
    printf("Connection accepted"
           ".\n");
    handle_connection(conn);
  }
CLEANUP:
  if (conn != -1)
    close(conn);
  if (serv != -1)
    close(serv);
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x1,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1253 <serve+0x93>
jmp    122f <serve+0x6f>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13aa <serve+0x1ea>
mov    -0x8(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
lea    -0x4(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    0xd8e(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
je     12ef <serve+0x12f>
jmp    12bf <serve+0xff>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rcx
lea    0xd56(%rip),%rdi
lea    0xd47(%rip),%rsi
mov    $0x1f40,%edx
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13aa <serve+0x1ea>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
je     132f <serve+0x16f>
jmp    130b <serve+0x14b>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13aa <serve+0x1ea>
lea    0xd3a(%rip),%rdi
lea    0xce8(%rip),%rsi
mov    $0x1f40,%edx
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    138f <serve+0x1cf>
jmp    136b <serve+0x1ab>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13aa <serve+0x1ea>
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <handle_connection@plt>
jmp    1349 <serve+0x189>
cmpl   $0xffffffff,-0xc(%rbp)
je     13bc <serve+0x1fc>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     13ce <serve+0x20e>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0x1,0xc(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x1c(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2ded(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a0 <serve+0xe0>
mov    %eax,%ebx
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xdd4(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %rax,%rax
je     12cb <serve+0x10b>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xda8(%rip),%rdi
lea    0xd99(%rip),%rsi
mov    $0x1f40,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1382 <serve+0x1c2>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd4b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %rax,%rax
je     12f6 <serve+0x136>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd61(%rip),%rdi
jmp    1378 <serve+0x1b8>
lea    0xd73(%rip),%rdi
lea    0xd21(%rip),%rsi
mov    $0x1f40,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     1363 <serve+0x1a3>
mov    %eax,%ebp
lea    0xd7f(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x8(%rsp),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1090 <handle_connection@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1340 <serve+0x180>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd0d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [37 x i8] c"ERROR: failed to create socket: %s.\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [8 x i8] c"0.0.0.0\00"
@.str.2 = [37 x i8] c"ERROR: failed to bind to %s:%d: %s.\0A\00"
@.str.3 = [30 x i8] c"ERROR: failed to listen: %s.\0A\00"
@.str.4 = [21 x i8] c"Listening on %s:%d.\0A\00"
@.str.5 = [41 x i8] c"ERROR: failed to accept connection: %s.\0A\00"
@.str.6 = [22 x i8] c"Connection accepted.\0A\00"
define void @serve() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
store i32 1, i32* %1
store i32 -1, i32* %2
store i32 -1, i32* %3
store i32 0, i32* %4
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast %struct.sockaddr_in* %6 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 eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %0
br label %15
15: ; preds = %14
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %70
19: ; No predecessors!
br label %20
20: ; preds = %19, %0
%21 = load i32, i32* %2
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %1, i32 4)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %25, i32* %26
%27 = call i32 @htons(i32 8000)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %2
%33 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 16)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %20
br label %37
37: ; preds = %36
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 8000, i8* %39)
br label %70
41: ; No predecessors!
br label %42
42: ; preds = %41, %20
%43 = load i32, i32* %2
%44 = call i64 @listen(i32 %43, i32 5)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %42
br label %47
47: ; preds = %46
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %70
51: ; No predecessors!
br label %52
52: ; preds = %51, %42
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 8000)
br label %54
54: ; preds = %52, %66
%55 = load i32, i32* %2
%56 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%57 = call i32 @accept(i32 %55, %struct.sockaddr* %56, i32* %4)
store i32 %57, i32* %3
%58 = load i32, i32* %3
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %66
60: ; preds = %54
br label %61
61: ; preds = %60
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %63)
br label %70
65: ; No predecessors!
br label %66
66: ; preds = %65, %54
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%68 = load i32, i32* %3
%69 = call i32 @handle_connection(i32 %68)
br label %54
70: ; preds = %61, %47, %37, %15
%71 = load i32, i32* %3
%72 = icmp ne i32 %71, -1
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32, i32* %3
%75 = call i32 @close(i32 %74)
br label %76
76: ; preds = %73, %70
%77 = load i32, i32* %2
%78 = icmp ne i32 %77, -1
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i32, i32* %2
%81 = call i32 @close(i32 %80)
br label %82
82: ; preds = %79, %76
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [37 x i8] c"ERROR: failed to create socket: %s.\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [8 x i8] c"0.0.0.0\00"
@.str.2 = [37 x i8] c"ERROR: failed to bind to %s:%d: %s.\0A\00"
@.str.3 = [30 x i8] c"ERROR: failed to listen: %s.\0A\00"
@.str.4 = [21 x i8] c"Listening on %s:%d.\0A\00"
@.str.5 = [41 x i8] c"ERROR: failed to accept connection: %s.\0A\00"
@str = [21 x i8] c"Connection accepted.\00"
define void @serve() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%6 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 0, i32* %8
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %59
18: ; preds = %0
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %12, i32 %19, i32 %20, i32* %1, i32 4)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 8000)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %12, %struct.sockaddr* %28, i32 16)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %18
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 8000, i8* %33)
br label %57
35: ; preds = %18
%36 = call i64 @listen(i32 %12, i32 5)
%37 = icmp eq i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %57
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 8000)
%44 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%45 = call i32 @accept(i32 %12, %struct.sockaddr* %44, i32* %2)
%46 = icmp eq i32 %45, -1
br i1 %46, label %53, label %47
47: ; preds = %42, %47
%48 = phi i32 [ %51, %47 ], [ %45, %42 ]
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%50 = call i32 @handle_connection(i32 %48)
%51 = call i32 @accept(i32 %12, %struct.sockaddr* %44, i32* %2)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %47
53: ; preds = %47, %42
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %53, %38, %31
%58 = call i32 @close(i32 %12)
br label %59
59: ; preds = %14, %57
ret void
}