#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ hostname;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int process(int, int /*<<< orphan*/);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main() {
  int fromlen;
  char hostname[64];
  struct hostent *hp;
  int cmd, s, ns, seq_num;
  struct sockaddr_in sin, fsin;
  char buff[1000];
  gethostname(hostname, sizeof(hostname));
  if ((hp = gethostbyname(hostname)) == NULL) {
    fprintf(stderr, "%s: host unknown.\n.", hostname);
    exit(1);
  }
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("server: socket");
    exit(1);
  }
  sin.sin_family = AF_INET;
  sin.sin_port = htons(1234);
  bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
  if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("server: bind");
    exit(1);
  }
  if (listen(s, 5) < 0) {
    perror("server: listen");
    exit(1);
  }
  printf("\nServer waiting...\n");
  seq_num = 1;
  cmd = 0;
  while (cmd != 255) {
    if ((ns = accept(s, (struct sockaddr *)&fsin, &fromlen)) < 0) {
      perror("server: accept");
      exit(1);
    }
    cmd = 0;
    while (cmd != 254) {
      cmd = process(ns, seq_num++);
      if (255 == cmd) {
        break;
      }
    }
    shutdown(ns, 1);
    while (recv(ns, buff, sizeof(buff), 0) > 0) {
    }
    shutdown(ns, 2);
    close(ns);
  }
  close(s);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x40,%esi
call   1100 <gethostname@plt>
lea    -0x50(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
jne    125a <main+0x5a>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rdx
lea    0xdb5(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x60(%rbp)
cmp    $0x0,%eax
jge    1295 <main+0x95>
lea    0xd8e(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    $0x4d2,%edi
call   1050 <htons@plt>
mov    %eax,-0x74(%rbp)
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x78(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   10c0 <bcopy@plt>
mov    -0x60(%rbp),%edi
lea    -0x78(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
jge    12f5 <main+0xf5>
lea    0xd3d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x60(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
jge    1322 <main+0x122>
lea    0xd1d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xd16(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x68(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0xff,-0x5c(%rbp)
je     140c <main+0x20c>
mov    -0x60(%rbp),%edi
lea    -0x88(%rbp),%rsi
lea    -0x8(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x64(%rbp)
cmp    $0x0,%eax
jge    137e <main+0x17e>
lea    0xce4(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
movl   $0x0,-0x5c(%rbp)
cmpl   $0xfe,-0x5c(%rbp)
je     13c0 <main+0x1c0>
mov    -0x64(%rbp),%edi
mov    -0x68(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
call   1070 <process@plt>
mov    %eax,-0x5c(%rbp)
mov    $0xff,%eax
cmp    -0x5c(%rbp),%eax
jne    13bb <main+0x1bb>
jmp    13c0 <main+0x1c0>
jmp    1385 <main+0x185>
mov    -0x64(%rbp),%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
mov    -0x64(%rbp),%edi
lea    -0x470(%rbp),%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jle    13f2 <main+0x1f2>
jmp    13cd <main+0x1cd>
mov    -0x64(%rbp),%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
mov    -0x64(%rbp),%edi
call   1080 <close@plt>
jmp    133c <main+0x13c>
mov    -0x60(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1110 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1100 <gethostname@plt>
mov    %rbx,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
jne    1254 <main+0x54>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
lea    0x20(%rsp),%rdx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    %rax,%rbx
mov    0x2d8a(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
jns    1281 <main+0x81>
lea    0xd98(%rip),%rdi
jmp    13ab <main+0x1ab>
mov    %eax,%r12d
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x4d2,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edx
mov    0x4(%rbx),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   10c0 <bcopy@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
jns    12ca <main+0xca>
lea    0xd5e(%rip),%rdi
jmp    13ab <main+0x1ab>
mov    %r12d,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
test   %rax,%rax
jns    12e8 <main+0xe8>
lea    0xd4d(%rip),%rdi
jmp    13ab <main+0x1ab>
lea    0xd5f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
lea    0xc(%rsp),%r15
lea    0x60(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    %r15,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     13a4 <main+0x1a4>
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%esi
lea    0x1(%rsi),%r14d
mov    %ebp,%edi
call   1070 <process@plt>
mov    %eax,%ebx
and    $0xfffffffe,%eax
cmp    $0xfe,%eax
jne    1330 <main+0x130>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <shutdown@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jg     1360 <main+0x160>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
mov    %ebp,%edi
call   1080 <close@plt>
cmp    $0xff,%ebx
jne    1310 <main+0x110>
mov    %r12d,%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    0xc94(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"%s: host unknown.\0A.\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"server: socket\00"
@.str.2 = [13 x i8] c"server: bind\00"
@.str.3 = [15 x i8] c"server: listen\00"
@.str.4 = [20 x i8] c"\0AServer waiting...\0A\00"
@.str.5 = [15 x i8] c"server: accept\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = alloca %struct.hostent*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca [1000 x i8]
store i32 0, i32* %1
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%13 = call i32 @gethostname(i8* %12, i32 64)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%15 = call %struct.hostent* @gethostbyname(i8* %14)
store %struct.hostent* %15, %struct.hostent** %4
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %22
17: ; preds = %0
%18 = load i32, i32* @stderr
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %0
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @htons(i32 1234)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %33, i32* %34
%35 = load %struct.hostent*, %struct.hostent** %4
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%39 = load %struct.hostent*, %struct.hostent** %4
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @bcopy(i32 %37, i32* %38, i32 %41)
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%45 = call i64 @bind(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %30
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %30
%51 = load i32, i32* %6
%52 = call i64 @listen(i32 %51, i32 5)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %50
%58 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %8
store i32 0, i32* %5
br label %59
59: ; preds = %92, %57
%60 = load i32, i32* %5
%61 = icmp ne i32 %60, 255
br i1 %61, label %62, label %97
62: ; preds = %59
%63 = load i32, i32* %6
%64 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%65 = call i32 @accept(i32 %63, %struct.sockaddr* %64, i32* %2)
store i32 %65, i32* %7
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %62
store i32 0, i32* %5
br label %71
71: ; preds = %82, %70
%72 = load i32, i32* %5
%73 = icmp ne i32 %72, 254
br i1 %73, label %74, label %83
74: ; preds = %71
%75 = load i32, i32* %7
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
%78 = call i32 @process(i32 %75, i32 %76)
store i32 %78, i32* %5
%79 = load i32, i32* %5
%80 = icmp eq i32 255, %79
br i1 %80, label %81, label %82
81: ; preds = %74
br label %83
82: ; preds = %74
br label %71
83: ; preds = %81, %71
%84 = load i32, i32* %7
%85 = call i32 @shutdown(i32 %84, i32 1)
br label %86
86: ; preds = %91, %83
%87 = load i32, i32* %7
%88 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%89 = call i64 @recv(i32 %87, i8* %88, i32 1000, i32 0)
%90 = icmp sgt i64 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %86
br label %86
92: ; preds = %86
%93 = load i32, i32* %7
%94 = call i32 @shutdown(i32 %93, i32 2)
%95 = load i32, i32* %7
%96 = call i32 @close(i32 %95)
br label %59
97: ; preds = %59
%98 = load i32, i32* %6
%99 = call i32 @close(i32 %98)
%100 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"%s: host unknown.\0A.\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"server: socket\00"
@.str.2 = [13 x i8] c"server: bind\00"
@.str.3 = [15 x i8] c"server: listen\00"
@.str.5 = [15 x i8] c"server: accept\00"
@str = [19 x i8] c"\0AServer waiting...\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [1000 x i8]
%6 = bitcast i32* %1 to i8*
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%11 = call i32 @gethostname(i8* %7, i32 64)
%12 = call %struct.hostent* @gethostbyname(i8* %7)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 1234)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @bcopy(i32 %32, i32* %33, i32 %35)
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i64 @bind(i32 %21, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %26
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %26
%44 = call i64 @listen(i32 %21, i32 5)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %43
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%51 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %52
52: ; preds = %49, %70
%53 = phi i32 [ 1, %49 ], [ %61, %70 ]
%54 = call i32 @accept(i32 %21, %struct.sockaddr* %51, i32* %1)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %52, %59
%60 = phi i32 [ %61, %59 ], [ %53, %52 ]
%61 = add nsw i32 %60, 1
%62 = call i32 @process(i32 %54, i32 %60)
%63 = and i32 %62, -2
%64 = icmp eq i32 %63, 254
br i1 %64, label %65, label %59
65: ; preds = %59
%66 = call i32 @shutdown(i32 %54, i32 1)
br label %67
67: ; preds = %67, %65
%68 = call i64 @recv(i32 %54, i8* %10, i32 1000, i32 0)
%69 = icmp sgt i64 %68, 0
br i1 %69, label %67, label %70
70: ; preds = %67
%71 = call i32 @shutdown(i32 %54, i32 2)
%72 = call i32 @close(i32 %54)
%73 = icmp eq i32 %62, 255
br i1 %73, label %74, label %52
74: ; preds = %70
%75 = call i32 @close(i32 %21)
%76 = call i32 @exit(i32 0)
unreachable
}