#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 /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ server;
typedef int pid_t;
typedef int /*<<< orphan*/ myint;
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 /*<<< orphan*/ *);
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ menu(int);
int /*<<< orphan*/ perror(char *);
int priv(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int five;
  int myint = 1;
  struct sockaddr_in server, client;
  sigemptyset((sigset_t *)&five);
  int init_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (init_fd == -1) {
    perror("socket");
    exit(-1);
  }
  bzero((char *)&server, sizeof(server));
  if (setsockopt(init_fd, SOL_SOCKET, SO_REUSEADDR, &myint, sizeof(myint)) ==
      -1) {
    perror("setsocket");
    exit(-1);
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  server.sin_port = htons(atoi(argv[1]));
  if (bind(init_fd, (struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("bind");
    exit(-1);
  }
  if ((listen(init_fd, 20)) == -1) {
    perror("listen");
    exit(-1);
  }
  int addr_len = sizeof(client);
  while (1) {
    int fd =
        accept(init_fd, (struct sockaddr *)&client, (socklen_t *)&addr_len);
    if (fd < 0) {
      perror("accept");
      exit(1);
    }
    pid_t pid = fork();
    if (pid == -1) {
      perror("fork");
      close(fd);
    }
    if (pid == 0) {
      alarm(15);
      close(init_fd);
      int user_priv = priv("tutorial");
      if (!user_priv) {
        menu(fd);
        close(fd);
        exit(0);
      }
    } else {
      close(fd);
    }
  }
  close(init_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x14(%rbp),%rdi
call   1090 <sigemptyset@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1272 <main+0x62>
lea    0xda0(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    -0x3c(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12c3 <main+0xb3>
lea    0xd4c(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x117>
lea    0xcf2(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x3c(%rbp),%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1353 <main+0x143>
lea    0xccb(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
movl   $0xc,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    138d <main+0x17d>
lea    0xc98(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    13b5 <main+0x1a5>
lea    0xc75(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x48(%rbp)
jne    1406 <main+0x1f6>
mov    $0xf,%edi
call   1070 <alarm@plt>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
lea    0xc4a(%rip),%rdi
call   10b0 <priv@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    1401 <main+0x1f1>
mov    -0x44(%rbp),%edi
call   1040 <menu@plt>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1110 <exit@plt>
jmp    140e <main+0x1fe>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
jmp    135a <main+0x14a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
movl   $0x1,0x4(%rsp)
lea    0x14(%rsp),%rdi
call   1090 <sigemptyset@plt>
mov    0x2da6(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0x4e>
lea    0xdaa(%rip),%rdi
jmp    1305 <main+0xf5>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12a1 <main+0x91>
lea    0xd61(%rip),%rdi
jmp    1305 <main+0xf5>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    0x8(%r14),%rdi
call   1100 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ed <main+0xdd>
lea    0xd1f(%rip),%rdi
jmp    1305 <main+0xf5>
mov    %ebx,%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1314 <main+0x104>
lea    0xd0a(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
movl   $0xc,(%rsp)
lea    0x18(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
call   10f0 <accept@plt>
test   %eax,%eax
jns    1344 <main+0x134>
lea    0xce1(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    %eax,%ebp
lea    0xcd5(%rip),%r14
lea    0x18(%rsp),%r15
mov    %rsp,%r12
lea    0xcc1(%rip),%r13
jmp    1390 <main+0x180>
xchg   %ax,%ax
mov    $0xf,%edi
call   1070 <alarm@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   10b0 <priv@plt>
test   %eax,%eax
je     13ba <main+0x1aa>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     132e <main+0x11e>
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
je     1360 <main+0x150>
cmp    $0xffffffff,%eax
je     13a9 <main+0x199>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    137d <main+0x16d>
mov    %r13,%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    13a0 <main+0x190>
mov    %ebp,%edi
call   1040 <menu@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1110 <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 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [10 x i8] c"setsocket\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [9 x i8] c"tutorial\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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
%15 = call i32 @sigemptyset(i32* %6)
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
%25 = bitcast %struct.sockaddr_in* %8 to i8*
%26 = call i32 @bzero(i8* %25, i32 12)
%27 = load i32, i32* %10
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %7, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %24
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = call i32 @htonl(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 @atoi(i8* %44)
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %46, i32* %47
%48 = load i32, i32* %10
%49 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%50 = call i32 @bind(i32 %48, %struct.sockaddr* %49, i32 12)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %35
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %35
%56 = load i32, i32* %10
%57 = call i32 @listen(i32 %56, i32 20)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 -1)
unreachable
62: ; preds = %55
store i32 12, i32* %11
br label %63
63: ; preds = %62, %100
%64 = load i32, i32* %10
%65 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%66 = call i32 @accept(i32 %64, %struct.sockaddr* %65, i32* %11)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %63
%70 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %63
%73 = call i32 (...) @fork()
store i32 %73, i32* %13
%74 = load i32, i32* %13
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %80
76: ; preds = %72
%77 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%78 = load i32, i32* %12
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %76, %72
%81 = load i32, i32* %13
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %97
83: ; preds = %80
%84 = call i32 @alarm(i32 15)
%85 = load i32, i32* %10
%86 = call i32 @close(i32 %85)
%87 = call i32 @priv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
store i32 %87, i32* %14
%88 = load i32, i32* %14
%89 = icmp ne i32 %88, 0
br i1 %89, label %96, label %90
90: ; preds = %83
%91 = load i32, i32* %12
%92 = call i32 @menu(i32 %91)
%93 = load i32, i32* %12
%94 = call i32 @close(i32 %93)
%95 = call i32 @exit(i32 0)
unreachable
96: ; preds = %83
br label %100
97: ; preds = %80
%98 = load i32, i32* %12
%99 = call i32 @close(i32 %98)
br label %100
100: ; preds = %97, %96
br label %63
}

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 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [10 x i8] c"setsocket\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [9 x i8] c"tutorial\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = call i32 @sigemptyset(i32* %3)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = call i32 @bzero(i8* %10, i32 12)
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %15, i32 %22, i32 %23, i32* %4, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %20
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %33, i32* %34
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %38, i32* %39
%40 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%41 = call i32 @bind(i32 %15, %struct.sockaddr* %40, i32 12)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %29
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %29
%47 = call i32 @listen(i32 %15, i32 20)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %46
%53 = bitcast i32* %7 to i8*
store i32 12, i32* %7
%54 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%55 = call i32 @accept(i32 %15, %struct.sockaddr* %54, i32* %7)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %77, %52
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %52, %77
%61 = phi i32 [ %78, %77 ], [ %55, %52 ]
%62 = call i32 (...) @fork()
switch i32 %62, label %75 [
i32 -1, label %63
i32 0, label %66
]
63: ; preds = %60
%64 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @close(i32 %61)
br label %75
66: ; preds = %60
%67 = call i32 @alarm(i32 15)
%68 = call i32 @close(i32 %15)
%69 = call i32 @priv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %77
71: ; preds = %66
%72 = call i32 @menu(i32 %61)
%73 = call i32 @close(i32 %61)
%74 = call i32 @exit(i32 0)
unreachable
75: ; preds = %63, %60
%76 = call i32 @close(i32 %61)
br label %77
77: ; preds = %66, %75
%78 = call i32 @accept(i32 %15, %struct.sockaddr* %54, i32* %7)
%79 = icmp slt i32 %78, 0
br i1 %79, label %57, label %60
}