#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;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ handle_child(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
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(6789);
  listener = socket(AF_INET, SOCK_STREAM, 0);
  if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("bind");
    return;
  }
  if (listen(listener, 10) < 0) {
    perror("listen");
    return;
  }
  for (;;) {
    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) {
        handle_child(fd);
        exit(EXIT_SUCCESS);
      }
    }
  }
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x1a85,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    1201 <run+0x71>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
jmp    1294 <run+0x104>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    1229 <run+0x99>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
jmp    1294 <run+0x104>
jmp    122e <run+0x9e>
movl   $0x4,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1263 <run+0xd3>
lea    0xdb3(%rip),%rdi
call   1070 <perror@plt>
jmp    128f <run+0xff>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    128a <run+0xfa>
mov    -0x30(%rbp),%edi
call   1060 <handle_child@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    128f <run+0xff>
jmp    122e <run+0x9e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e45(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    $0x1a85,%edi
call   1030 <htons@plt>
mov    %eax,0x18(%rsp)
mov    (%rbx),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
lea    0x18(%rsp),%rsi
mov    %eax,%edi
mov    $0x18,%edx
call   1050 <bind@plt>
test   %rax,%rax
jns    11f3 <run+0x63>
lea    0xe0f(%rip),%rdi
jmp    120b <run+0x7b>
mov    %ebx,%edi
mov    $0xa,%esi
call   1040 <listen@plt>
test   %rax,%rax
jns    121d <run+0x8d>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xdde(%rip),%r12
jmp    1240 <run+0xb0>
mov    %r12,%rdi
call   1070 <perror@plt>
nopl   0x0(%rax,%rax,1)
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
test   %eax,%eax
js     1230 <run+0xa0>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    1240 <run+0xb0>
mov    %ebp,%edi
call   1060 <handle_child@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <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
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [7 x i8] c"accept\00"
@EXIT_SUCCESS = global i32 0
define void @run() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 0
store i64 0, i64* %9
%10 = call i32 @htons(i32 6789)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %1
%15 = load i32, i32* %1
%16 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%17 = call i64 @bind(i32 %15, %struct.sockaddr* %16, i32 24)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %46
21: ; preds = %0
%22 = load i32, i32* %1
%23 = call i64 @listen(i32 %22, i32 10)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %46
27: ; preds = %21
br label %28
28: ; preds = %45, %27
store i32 4, i32* %4
%29 = load i32, i32* %1
%30 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%31 = call i32 @accept(i32 %29, %struct.sockaddr* %30, i32* %4)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %45
36: ; preds = %28
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* %5
%41 = call i32 @handle_child(i32 %40)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
br label %45
45: ; preds = %44, %34
br label %28
46: ; preds = %25, %19
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
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [7 x i8] c"accept\00"
@EXIT_SUCCESS = global i32 0
define void @run() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i64 0, i64* %7
%8 = tail call i32 @htons(i32 6789)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%14 = call i64 @bind(i32 %12, %struct.sockaddr* %13, i32 24)
%15 = icmp slt i64 %14, 0
br i1 %15, label %36, label %16
16: ; preds = %0
%17 = call i64 @listen(i32 %12, i32 10)
%18 = icmp slt i64 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %16
%20 = bitcast %struct.sockaddr_storage* %2 to i8*
%21 = bitcast i32* %3 to i8*
%22 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
br label %23
23: ; preds = %19, %35
store i32 4, i32* %3
%24 = call i32 @accept(i32 %12, %struct.sockaddr* %22, i32* %3)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %35
28: ; preds = %23
%29 = call i64 (...) @fork()
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = call i32 @handle_child(i32 %24)
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %28, %26
br label %23
36: ; preds = %16, %0
%37 = 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), %16 ]
%38 = call i32 @perror(i8* %37)
ret void
}