#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*/ server;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
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*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void serve(int port, void (*handlecon)(int, void *), void *arg) {
  int sockfd, confd;
  socklen_t len;
  struct sockaddr_in server, client;
  int x;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("error: socket");
    return;
  }
  x = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&x, sizeof x);
  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(port);
  if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("error: bind");
    return;
  }
  listen(sockfd, 5);
  len = sizeof(client);
  while (confd = accept(sockfd, (struct sockaddr *)&client, &len), confd >= 0)
    handlecon(confd, arg);
  perror("error: accept");
  return;
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ca <serve+0x4a>
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
jmp    12a3 <serve+0x123>
movl   $0x1,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1254 <serve+0xd4>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
jmp    12a3 <serve+0x123>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
movl   $0xc,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1297 <serve+0x117>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   *%rax
jmp    1268 <serve+0xe8>
lea    0xd7c(%rip),%rdi
call   1080 <perror@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%r15d
mov    0x2e42(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
jns    11cf <serve+0x4f>
lea    0xe44(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>
mov    %eax,%r13d
movl   $0x1,0x4(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1251 <serve+0xd1>
lea    0xdbf(%rip),%rdi
jmp    12b1 <serve+0x131>
mov    %r13d,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
movl   $0xc,(%rsp)
lea    0x18(%rsp),%rsi
mov    %rsp,%rdx
mov    %r13d,%edi
call   1090 <accept@plt>
test   %eax,%eax
js     12aa <serve+0x12a>
lea    0x18(%rsp),%rbx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%edi
mov    %r14,%rsi
call   *%r12
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <accept@plt>
test   %eax,%eax
jns    1290 <serve+0x110>
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [14 x i8] c"error: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [12 x i8] c"error: bind\00"
@.str.2 = [14 x i8] c"error: accept\00"
define void @serve(i32 %0, void (i32, i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca void (i32, i8*)*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 %0, i32* %4
store void (i32, i8*)* %1, void (i32, i8*)** %5
store i8* %2, i8** %6
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %56
20: ; preds = %3
store i32 1, i32* %12
%21 = load i32, i32* %7
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = bitcast i32* %12 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 4)
%26 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %4
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %7
%36 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %20
%40 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %56
41: ; preds = %20
%42 = load i32, i32* %7
%43 = call i32 @listen(i32 %42, i32 5)
store i32 12, i32* %9
br label %44
44: ; preds = %50, %41
%45 = load i32, i32* %7
%46 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %9)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp sge i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load void (i32, i8*)*, void (i32, i8*)** %5
%52 = load i32, i32* %8
%53 = load i8*, i8** %6
br label %44
54: ; preds = %44
%55 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %39, %18
ret void
}

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
@.str = [14 x i8] c"error: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [12 x i8] c"error: bind\00"
@.str.2 = [14 x i8] c"error: accept\00"
define void @serve(i32 %0, void (i32, i8*)* nocapture %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %45
18: ; preds = %3
store i32 1, i32* %7
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %14, i32 %19, i32 %20, i8* %11, i32 4)
%22 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = call i32 @htons(i32 %0)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%30 = call i64 @bind(i32 %14, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %45
34: ; preds = %18
%35 = call i32 @listen(i32 %14, i32 5)
store i32 12, i32* %4
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @accept(i32 %14, %struct.sockaddr* %36, i32* %4)
%38 = icmp sgt i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %34, %39
%40 = phi i32 [ %41, %39 ], [ %37, %34 ]
%41 = call i32 @accept(i32 %14, %struct.sockaddr* %36, i32* %4)
%42 = icmp sgt i32 %41, -1
br i1 %42, label %39, label %43
43: ; preds = %39, %34
%44 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %32, %16
ret void
}