#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ in;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_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*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ process_syscall();
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void init_srv_sp() {
  struct sockaddr_in in, l;
  int srv;
  int con;
  int len;
  int opt;
  int t;
  memset(&in, 0, sizeof(in));
  in.sin_port = htons(8181);
  in.sin_family = AF_INET;
  srv = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (srv < 0) {
    perror("socket");
    exit(1);
  }
  opt = 1;
  if (setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
    perror("setsockopt");
    exit(1);
  }
  len = sizeof(in);
  if (bind(srv, (struct sockaddr *)&in, sizeof(in)) < 0) {
    perror("bind");
    exit(1);
  }
  listen(srv, 5);
  while ((con = accept(srv, (struct sockaddr *)&in, &len)) > 0) {
    int pid;
    pid = fork();
    if (pid == 0) {
      close(srv);
      printf("con: %i\n", con);
      process_syscall();
    } else {
      close(con);
    }
  }
}


x86 assembly (clang, -O0):
<init_srv_sp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    $0x1ff5,%edi
call   1040 <htons@plt>
mov    %eax,-0x4(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1244 <init_srv_sp+0x74>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    128f <init_srv_sp+0xbf>
lea    0xd87(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movl   $0x8,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12c7 <init_srv_sp+0xf7>
lea    0xd5a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1334 <init_srv_sp+0x164>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1327 <init_srv_sp+0x157>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%esi
lea    0xd01(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <process_syscall@plt>
jmp    132f <init_srv_sp+0x15f>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    12d4 <init_srv_sp+0x104>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_srv_sp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    $0x1ff5,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    0x2dba(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     130c <init_srv_sp+0x13c>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1315 <init_srv_sp+0x145>
movl   $0x8,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     131e <init_srv_sp+0x14e>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    %rsp,%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
test   %eax,%eax
jle    12ff <init_srv_sp+0x12f>
mov    %eax,%ebp
lea    0xd6b(%rip),%r14
mov    %rsp,%r15
lea    0x8(%rsp),%r12
jmp    12da <init_srv_sp+0x10a>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    12ff <init_srv_sp+0x12f>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    12c0 <init_srv_sp+0xf0>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   10b0 <process_syscall@plt>
jmp    12c7 <init_srv_sp+0xf7>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xced(%rip),%rdi
jmp    1325 <init_srv_sp+0x155>
lea    0xceb(%rip),%rdi
jmp    1325 <init_srv_sp+0x155>
lea    0xced(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [9 x i8] c"con: %i\0A\00"
define void @init_srv_sp() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%8 = call i32 @htons(i32 8181)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %0
%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 = %0
store i32 1, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i64 @setsockopt(i32 %15, i32 %21, i32 %22, i32* %3, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
store i32 8, i32* %2
%29 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%30 = call i64 @bind(i32 %15, %struct.sockaddr* %29, i32 8)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = call i32 @listen(i32 %15, i32 5)
%37 = call i32 @accept(i32 %15, %struct.sockaddr* %29, i32* %2)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %35, %49
%40 = phi i32 [ %50, %49 ], [ %37, %35 ]
%41 = call i32 (...) @fork()
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = call i32 @close(i32 %15)
%45 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%46 = call i32 (...) @process_syscall()
br label %49
47: ; preds = %39
%48 = call i32 @close(i32 %40)
br label %49
49: ; preds = %47, %43
%50 = call i32 @accept(i32 %15, %struct.sockaddr* %29, i32* %2)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %39, label %52
52: ; preds = %49, %35
ret void
}