#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*/ my_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECHILD;
scalar_t__ EINTR;
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);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void serve(int port) {
  int svc;
  int rqst;
  socklen_t alen;
  struct sockaddr_in my_addr;
  struct sockaddr_in client_addr;
  int sockoptval = 1;
  char hostname[128];
  gethostname(hostname, 128);
  if ((svc = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("cannot create socket");
    exit(1);
  }
  setsockopt(svc, SOL_SOCKET, SO_REUSEADDR, &sockoptval, sizeof(int));
  memset((char *)&my_addr, 0, sizeof(my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(port);
  my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(svc, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
    perror("bind failed");
    exit(1);
  }
  if (listen(svc, 5) < 0) {
    perror("listen failed");
    exit(1);
  }
  printf("server started on %s, listening on port %d\n", hostname, port);
  alen = sizeof(client_addr);
  for (;;) {
    while ((rqst = accept(svc, (struct sockaddr *)&client_addr, &alen)) < 0) {
      if ((errno != ECHILD) && (errno != EINTR) && (errno != EINTR)) {
        perror("accept failed");
        exit(1);
      }
    }
    printf("received a connection from: %s port %d\n",
           inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    shutdown(rqst, 2);
  }
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
lea    -0xc0(%rbp),%rdi
mov    $0x80,%esi
call   10f0 <gethostname@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1251 <serve+0x61>
lea    0xdbe(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2d44(%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   1090 <memset@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    12de <serve+0xee>
lea    0xd46(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    130b <serve+0x11b>
lea    0xd25(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0xc0(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xd13(%rip),%rdi
call   1070 <printf@plt>
movl   $0xc,-0x10(%rbp)
jmp    132d <serve+0x13d>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    13b2 <serve+0x1c2>
mov    0x2c70(%rip),%rax
mov    (%rax),%rax
mov    0x2c56(%rip),%rcx
cmp    (%rcx),%rax
je     13ad <serve+0x1bd>
mov    0x2c56(%rip),%rax
mov    (%rax),%rax
mov    0x2c44(%rip),%rcx
cmp    (%rcx),%rax
je     13ad <serve+0x1bd>
mov    0x2c3c(%rip),%rax
mov    (%rax),%rax
mov    0x2c2a(%rip),%rcx
cmp    (%rcx),%rax
je     13ad <serve+0x1bd>
lea    0xcbd(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
jmp    132d <serve+0x13d>
mov    -0x2c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0xc8(%rbp)
mov    -0x30(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0xc8(%rbp),%rsi
mov    %eax,%edx
lea    0xc90(%rip),%rdi
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
jmp    1328 <serve+0x138>

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %edi,%r14d
movl   $0x1,0x8(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x80,%esi
call   10f0 <gethostname@plt>
mov    0x2dbe(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
jns    1245 <serve+0x55>
lea    0xdc0(%rip),%rdi
jmp    138b <serve+0x19b>
mov    %eax,%r12d
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    12c7 <serve+0xd7>
lea    0xd53(%rip),%rdi
jmp    138b <serve+0x19b>
mov    %r12d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jns    12e5 <serve+0xf5>
lea    0xd41(%rip),%rdi
jmp    138b <serve+0x19b>
lea    0xd43(%rip),%rdi
lea    0x30(%rsp),%rsi
mov    %r14d,%edx
call   1070 <printf@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r13
lea    0xd57(%rip),%r15
jmp    1350 <serve+0x160>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%ebx
mov    0x24(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x20(%rsp),%edi
call   10b0 <ntohs@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <printf@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r13,%rdx
call   10e0 <accept@plt>
test   %eax,%eax
jns    1320 <serve+0x130>
mov    0x2c57(%rip),%rax
mov    (%rax),%rax
mov    0x2c3d(%rip),%rcx
cmp    (%rcx),%rax
je     1350 <serve+0x160>
mov    0x2c39(%rip),%rcx
cmp    (%rcx),%rax
je     1350 <serve+0x160>
lea    0xcd0(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <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 = [21 x i8] c"cannot create socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [12 x i8] c"bind failed\00"
@.str.2 = [14 x i8] c"listen failed\00"
@.str.3 = [44 x i8] c"server started on %s, listening on port %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@EINTR = global i64 0
@.str.4 = [14 x i8] c"accept failed\00"
@.str.5 = [40 x i8] c"received a connection from: %s port %d\0A\00"
define void @serve(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca [128 x i8]
store i32 %0, i32* %2
store i32 1, i32* %8
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%11 = call i32 @gethostname(i8* %10, i32 128)
%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* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load i32, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %8, i32 4)
%24 = bitcast %struct.sockaddr_in* %6 to i8*
%25 = call i32 @memset(i8* %24, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* %2
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* %3
%36 = bitcast %struct.sockaddr_in* %6 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 %42
39: ; preds = %19
%40 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %19
%43 = load i32, i32* %3
%44 = call i64 @listen(i32 %43, i32 5)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%51 = load i32, i32* %2
%52 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %50, i32 %51)
store i32 12, i32* %5
br label %53
53: ; preds = %75, %49
br label %54
54: ; preds = %74, %53
%55 = load i32, i32* %3
%56 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%57 = call i32 @accept(i32 %55, %struct.sockaddr* %56, i32* %5)
store i32 %57, i32* %4
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %75
59: ; preds = %54
%60 = load i64, i64* @errno
%61 = load i64, i64* @ECHILD
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %74
63: ; preds = %59
%64 = load i64, i64* @errno
%65 = load i64, i64* @EINTR
%66 = icmp ne i64 %64, %65
br i1 %66, label %67, label %74
67: ; preds = %63
%68 = load i64, i64* @errno
%69 = load i64, i64* @EINTR
%70 = icmp ne i64 %68, %69
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %67, %63, %59
br label %54
75: ; preds = %54
%76 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%77 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i8* @inet_ntoa(i32 %78)
%80 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%81 = load i32, i32* %80
%82 = call i32 @ntohs(i32 %81)
%83 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %79, i32 %82)
%84 = load i32, i32* %4
%85 = call i32 @shutdown(i32 %84, i32 2)
br label %53
}

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 = [21 x i8] c"cannot create socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [12 x i8] c"bind failed\00"
@.str.2 = [14 x i8] c"listen failed\00"
@.str.3 = [44 x i8] c"server started on %s, listening on port %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@EINTR = global i64 0
@.str.4 = [14 x i8] c"accept failed\00"
@.str.5 = [40 x i8] c"received a connection from: %s port %d\0A\00"
define void @serve(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = bitcast i32* %2 to i8*
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%12 = call i32 @gethostname(i8* %11, i32 128)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %15, i32 %21, i32 %22, i32* %5, i32 4)
%24 = call i32 @memset(i8* %8, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = call i32 @htons(i32 %0)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @bind(i32 %15, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = call i64 @listen(i32 %15, i32 5)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %38
%45 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %11, i32 %0)
store i32 12, i32* %2
%46 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %49
49: ; preds = %59, %44
%50 = call i32 @accept(i32 %15, %struct.sockaddr* %46, i32* %2)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %63
52: ; preds = %49
%53 = load i64, i64* @errno
%54 = load i64, i64* @ECHILD
%55 = icmp eq i64 %53, %54
%56 = load i64, i64* @EINTR
%57 = icmp eq i64 %53, %56
%58 = select i1 %55, i1 true, i1 %57
br i1 %58, label %59, label %60
59: ; preds = %52, %63
br label %49
60: ; preds = %52
%61 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %49
%64 = load i32, i32* %47
%65 = call i8* @inet_ntoa(i32 %64)
%66 = load i32, i32* %48
%67 = call i32 @ntohs(i32 %66)
%68 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %65, i32 %67)
%69 = call i32 @shutdown(i32 %50, i32 2)
br label %59
}