#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;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void *atoi(char *);
scalar_t__ bind(int, struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int ac, char *av[]) {
  int delay = 0, port = 0;
  int reuseaddr = 1;
  unsigned max_packets = -1;
  if (ac < 3)
    exit(usage(av[0]));
  port = atoi(av[1]);
  delay = atoi(av[2]);
  if (av[3])
    max_packets = atoi(av[3]);
  struct sockaddr_in sa;
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = INADDR_ANY;
  int s = socket(PF_INET, SOCK_STREAM, 0);
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) < 0)
    perror("reuseaddr");
  if (bind(s, &sa, sizeof(sa)) < 0)
    perror("bind");
  if (listen(s, 1))
    perror("listen");
  int fd = accept(s, NULL, NULL);
  if (delay > 0)
    sleep(delay);
  char buf[1024];
  while (recv(fd, buf, sizeof(buf), 0) > 0 && --max_packets)
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1222 <main+0x52>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
mov    %eax,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1261 <main+0x91>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    12ea <main+0x11a>
lea    0xd1b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1311 <main+0x141>
lea    0xcfe(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     1334 <main+0x164>
lea    0xce0(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1358 <main+0x188>
mov    -0x14(%rbp),%edi
call   10e0 <sleep@plt>
jmp    135d <main+0x18d>
mov    -0x38(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x441(%rbp)
jle    139d <main+0x1cd>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x441(%rbp)
mov    -0x441(%rbp),%al
test   $0x1,%al
jne    13b0 <main+0x1e0>
jmp    13b5 <main+0x1e5>
jmp    135d <main+0x18d>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rsi,%rbx
movl   $0x1,0xc(%rsp)
cmp    $0x2,%edi
jle    1351 <main+0x181>
mov    0x8(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %rax,%rbp
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %rax,%r14
mov    0x18(%rbx),%rdi
test   %rdi,%rdi
je     121a <main+0x4a>
call   10c0 <atoi@plt>
mov    %rax,%rbx
jmp    121f <main+0x4f>
mov    $0xffffffff,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     131e <main+0x14e>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     132f <main+0x15f>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    1340 <main+0x170>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %r14d,%r14d
jle    12e2 <main+0x112>
mov    %r14d,%edi
call   10e0 <sleep@plt>
add    $0xffffffff,%ebx
lea    0x20(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
sub    $0x1,%ebx
setb   %cl
test   %rax,%rax
jle    1310 <main+0x140>
test   %cl,%cl
je     12f0 <main+0x120>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcdb(%rip),%rdi
call   10a0 <perror@plt>
jmp    129d <main+0xcd>
lea    0xcd4(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b7 <main+0xe7>
lea    0xcc8(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c8 <main+0xf8>
mov    (%rbx),%rdi
call   1080 <usage@plt>
mov    %eax,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [10 x i8] c"reuseaddr\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
%13 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 1, i32* %8
store i32 -1, i32* %9
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @usage(i8* %19)
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i8* @atoi(i8* %25)
%27 = ptrtoint i8* %26 to i32
store i32 %27, i32* %7
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i8* @atoi(i8* %30)
%32 = ptrtoint i8* %31 to i32
store i32 %32, i32* %6
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 3
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %43
37: ; preds = %22
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 3
%40 = load i8*, i8** %39
%41 = call i8* @atoi(i8* %40)
%42 = ptrtoint i8* %41 to i32
store i32 %42, i32* %9
br label %43
43: ; preds = %37, %22
%44 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* %7
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %48, i32* %49
%50 = load i32, i32* @INADDR_ANY
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = load i32, i32* @PF_INET
%54 = load i32, i32* @SOCK_STREAM
%55 = call i32 @socket(i32 %53, i32 %54, i32 0)
store i32 %55, i32* %11
%56 = load i32, i32* %11
%57 = load i32, i32* @SOL_SOCKET
%58 = load i32, i32* @SO_REUSEADDR
%59 = call i64 @setsockopt(i32 %56, i32 %57, i32 %58, i32* %8, i32 4)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %43
%62 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %63
63: ; preds = %61, %43
%64 = load i32, i32* %11
%65 = call i64 @bind(i32 %64, %struct.sockaddr_in* %10, i32 12)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %69
69: ; preds = %67, %63
%70 = load i32, i32* %11
%71 = call i64 @listen(i32 %70, i32 1)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %75
75: ; preds = %73, %69
%76 = load i32, i32* %11
%77 = call i32 @accept(i32 %76, i32* null, i32* null)
store i32 %77, i32* %12
%78 = load i32, i32* %6
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = load i32, i32* %6
%82 = call i32 @sleep(i32 %81)
br label %83
83: ; preds = %80, %75
br label %84
84: ; preds = %95, %83
%85 = load i32, i32* %12
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%87 = call i64 @recv(i32 %85, i8* %86, i32 1024, i32 0)
%88 = icmp sgt i64 %87, 0
br i1 %88, label %89, label %93
89: ; preds = %84
%90 = load i32, i32* %9
%91 = add i32 %90, -1
store i32 %91, i32* %9
%92 = icmp ne i32 %91, 0
br label %93
93: ; preds = %89, %84
%94 = phi i1 [ false, %84 ], [ %92, %89 ]
br i1 %94, label %95, label %96
95: ; preds = %93
br label %84
96: ; preds = %93
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [10 x i8] c"reuseaddr\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @usage(i8* %9)
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i8* @atoi(i8* %14)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i8* @atoi(i8* %19)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i32
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %30, label %26
26: ; preds = %12
%27 = tail call i8* @atoi(i8* %24)
%28 = ptrtoint i8* %27 to i64
%29 = trunc i64 %28 to i32
br label %30
30: ; preds = %26, %12
%31 = phi i32 [ %29, %26 ], [ -1, %12 ]
%32 = bitcast %struct.sockaddr_in* %4 to i8*
%33 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %34, i32* %35
%36 = call i32 @htons(i32 %17)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* @PF_INET
%41 = load i32, i32* @SOCK_STREAM
%42 = call i32 @socket(i32 %40, i32 %41, i32 0)
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_REUSEADDR
%45 = call i64 @setsockopt(i32 %42, i32 %43, i32 %44, i32* %3, i32 4)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %30
%48 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %30
%50 = call i64 @bind(i32 %42, %struct.sockaddr_in* %4, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = call i64 @listen(i32 %42, i32 1)
%56 = icmp eq i64 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %54
%60 = call i32 @accept(i32 %42, i32* null, i32* null)
%61 = icmp sgt i32 %22, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 @sleep(i32 %22)
br label %64
64: ; preds = %62, %59
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %66
66: ; preds = %66, %64
%67 = phi i32 [ %31, %64 ], [ %70, %66 ]
%68 = call i64 @recv(i32 %60, i8* %65, i32 1024, i32 0)
%69 = icmp slt i64 %68, 1
%70 = add i32 %67, -1
%71 = icmp eq i32 %70, 0
%72 = select i1 %69, i1 true, i1 %71
br i1 %72, label %73, label %66
73: ; preds = %66
ret i32 0
}