#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*/);
int serve1(int port) {
  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 -1;
  }
  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 -1;
  }
  listen(sockfd, 5);
  len = sizeof(client);
  while (confd = accept(sockfd, (struct sockaddr *)&client, &len), confd >= 0)
    return confd;
  perror("error: accept");
  return -1;
}


x86 assembly (clang, -O0):
<serve1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c9 <serve1+0x49>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <serve1+0x129>
movl   $0x1,-0x34(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    125a <serve1+0xda>
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <serve1+0x129>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
movl   $0xc,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1296 <serve1+0x116>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a9 <serve1+0x129>
lea    0xd7d(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve1>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
mov    0x2e4c(%rip),%r15
mov    (%r15),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1256 <serve1+0xd6>
mov    %eax,%ebx
movl   $0x1,(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     125f <serve1+0xdf>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1090 <accept@plt>
test   %eax,%eax
js     1268 <serve1+0xe8>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda3(%rip),%rdi
jmp    126f <serve1+0xef>
lea    0xda8(%rip),%rdi
jmp    126f <serve1+0xef>
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    124b <serve1+0xcb>

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

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