#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 sockaddr_storage {
  int dummy;
};
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ss;
typedef int socklen_t;
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
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);
int /*<<< orphan*/ child(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, 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 listener;
  struct sockaddr_in sin;
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = 0;
  sin.sin_port = htons(40713);
  listener = socket(AF_INET, SOCK_STREAM, 0);
  {
    int one = 1;
    setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  }
  if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("bind");
    return;
  }
  if (listen(listener, 16) < 0) {
    perror("listen");
    return;
  }
  while (1) {
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr *)&ss, &slen);
    if (fd < 0) {
      perror("accept");
    } else {
      if (fork() == 0) {
        child(fd);
        exit(0);
      }
    }
  }
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x9f09,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    123c <run+0x9c>
lea    0xdce(%rip),%rdi
call   1080 <perror@plt>
jmp    12c8 <run+0x128>
mov    -0x4(%rbp),%edi
mov    $0x10,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1264 <run+0xc4>
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
jmp    12c8 <run+0x128>
jmp    1269 <run+0xc9>
movl   $0x4,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    129e <run+0xfe>
lea    0xd78(%rip),%rdi
call   1080 <perror@plt>
jmp    12c3 <run+0x123>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    12be <run+0x11e>
mov    -0x30(%rbp),%edi
call   1030 <child@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12c3 <run+0x123>
jmp    1269 <run+0xc9>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2e2d(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    $0x9f09,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
movl   $0x1,(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    122c <run+0x8c>
lea    0xdd6(%rip),%rdi
jmp    1244 <run+0xa4>
mov    %ebx,%edi
mov    $0x10,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    1256 <run+0xb6>
lea    0xdc1(%rip),%rdi
call   1080 <perror@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%r14
lea    0x4(%rsp),%r15
lea    0xda7(%rip),%r12
jmp    1270 <run+0xd0>
mov    %r12,%rdi
call   1080 <perror@plt>
nop
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <accept@plt>
test   %eax,%eax
js     1267 <run+0xc7>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    1270 <run+0xd0>
mov    %ebp,%edi
call   1030 <child@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [7 x i8] c"accept\00"
define void @run() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i64 0, i64* %10
%11 = call i32 @htons(i32 40713)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %1
store i32 1, i32* %3
%16 = load i32, i32* %1
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %3, i32 4)
%20 = load i32, i32* %1
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 24)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %50
26: ; preds = %0
%27 = load i32, i32* %1
%28 = call i64 @listen(i32 %27, i32 16)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %50
32: ; preds = %26
br label %33
33: ; preds = %32, %49
store i32 4, i32* %5
%34 = load i32, i32* %1
%35 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%36 = call i32 @accept(i32 %34, %struct.sockaddr* %35, i32* %5)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %49
41: ; preds = %33
%42 = call i64 (...) @fork()
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* %6
%46 = call i32 @child(i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %41
br label %49
49: ; preds = %48, %39
br label %33
50: ; preds = %30, %24
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [7 x i8] c"accept\00"
define void @run() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i64 0, i64* %8
%9 = tail call i32 @htons(i32 40713)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %13, i32 %15, i32 %16, i32* %2, i32 4)
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = call i64 @bind(i32 %13, %struct.sockaddr* %18, i32 24)
%20 = icmp slt i64 %19, 0
br i1 %20, label %40, label %21
21: ; preds = %0
%22 = call i64 @listen(i32 %13, i32 16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %40, label %24
24: ; preds = %21
%25 = bitcast %struct.sockaddr_storage* %3 to i8*
%26 = bitcast i32* %4 to i8*
%27 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
br label %28
28: ; preds = %24, %39
store i32 4, i32* %4
%29 = call i32 @accept(i32 %13, %struct.sockaddr* %27, i32* %4)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %39
33: ; preds = %28
%34 = call i64 (...) @fork()
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @child(i32 %29)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %33, %31
br label %28
40: ; preds = %21, %0
%41 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %21 ]
%42 = call i32 @perror(i8* %41)
ret void
}