#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 */
typedef int /*<<< orphan*/ val;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ children(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void run(void) {
  int sockfd, clifd, val;
  struct sockaddr_in serv_addr, cli_addr;
  socklen_t len;
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(40713);
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  val = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    perror("bind() failed");
    exit(EXIT_FAILURE);
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen() failed");
    exit(EXIT_FAILURE);
  }
  len = sizeof(cli_addr);
  while (1) {
    clifd = accept(sockfd, (struct sockaddr *)&cli_addr, &len);
    if (clifd == -1) {
      perror("accept() failed");
      continue;
    }
    switch (fork()) {
    case -1:
      perror("fork() failed");
      close(clifd);
      continue;
    case 0:
      close(sockfd);
      children(clifd);
      close(clifd);
      _exit(EXIT_SUCCESS);
    default:
      close(clifd);
    }
  }
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x9f09,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1262 <run+0x82>
lea    0xdb1(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c1 <run+0xe1>
lea    0xd62(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12f1 <run+0x111>
lea    0xd40(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1326 <run+0x146>
lea    0xd12(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f8 <run+0x118>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     1350 <run+0x170>
jmp    1340 <run+0x160>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     1369 <run+0x189>
jmp    138f <run+0x1af>
lea    0xce7(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    12f8 <run+0x118>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%edi
call   10d0 <children@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    12f8 <run+0x118>

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dda(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x9f09,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
jne    124c <run+0x6c>
lea    0xdb6(%rip),%rdi
jmp    12b1 <run+0xd1>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1299 <run+0xb9>
lea    0xd79(%rip),%rdi
jmp    12b1 <run+0xd1>
mov    %ebx,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c4 <run+0xe4>
lea    0xd6d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
movl   $0xc,(%rsp)
lea    0x18(%rsp),%r14
mov    %rsp,%r15
lea    0xd54(%rip),%r12
lea    0xd5d(%rip),%r13
jmp    12f0 <run+0x110>
mov    %r12,%rdi
call   10b0 <perror@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     12e3 <run+0x103>
mov    %eax,%ebp
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
je     131d <run+0x13d>
test   %eax,%eax
je     1327 <run+0x147>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    12f0 <run+0x110>
mov    %r13,%rdi
call   10b0 <perror@plt>
jmp    1314 <run+0x134>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%edi
call   10d0 <children@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [16 x i8] c"listen() failed\00"
@.str.3 = [16 x i8] c"accept() failed\00"
@.str.4 = [14 x i8] c"fork() failed\00"
@EXIT_SUCCESS = global i32 0
define void @run() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 40713)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %0
store i32 1, i32* %1
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i32 @setsockopt(i32 %19, i32 %26, i32 %27, i32* %1, i32 4)
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %19, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
%37 = call i32 @listen(i32 %19, i32 10)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %36
store i32 12, i32* %4
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %45
45: ; preds = %50, %43
%46 = call i32 @accept(i32 %19, %struct.sockaddr* %44, i32* %4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %53, %62
br label %45
51: ; preds = %45
%52 = call i32 (...) @fork()
switch i32 %52, label %62 [
i32 -1, label %53
i32 0, label %56
]
53: ; preds = %51
%54 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @close(i32 %46)
br label %50
56: ; preds = %51
%57 = call i32 @close(i32 %19)
%58 = call i32 @children(i32 %46)
%59 = call i32 @close(i32 %46)
%60 = load i32, i32* @EXIT_SUCCESS
%61 = call i32 @_exit(i32 %60)
unreachable
62: ; preds = %51
%63 = call i32 @close(i32 %46)
br label %50
}