#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 */
typedef int /*<<< orphan*/ yes;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, unsigned int *);
unsigned int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ function(int);
int /*<<< orphan*/ htons(unsigned short);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  unsigned int port;
  struct sockaddr_in server;
  struct sockaddr_in client;
  int serv_sock;
  int sock;
  unsigned int len;
  int yes = 1;
  if (argc != 2) {
    printf("%s <listening_port>\n", argv[0]);
    exit(-1);
  }
  port = atoi(argv[1]);
  bzero((char *)&server, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons((unsigned short)port);
  serv_sock = socket(PF_INET, SOCK_STREAM, 0);
  if (serv_sock < 0) {
    perror("socket(): ");
    exit(-1);
  }
  if (setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&yes,
                 sizeof(yes)) < 0) {
    perror("setsockopt(): ");
    exit(-1);
  }
  if ((bind(serv_sock, (struct sockaddr *)&server, sizeof(server))) < 0) {
    perror("bind(): ");
    exit(-1);
  }
  if ((listen(serv_sock, 1)) < 0) {
    perror("listen(): ");
    exit(-1);
  }
  while (1) {
    printf("[*] wait for a connection on port: %d\n", port);
    len = sizeof(client);
    sock = accept(serv_sock, (struct sockaddr *)&client, &len);
    printf("[*] connection from: %s\n", inet_ntoa(client.sin_addr));
    function(sock);
  }
  shutdown(sock, SHUT_RDWR);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1216 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
movzwl %ax,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    128a <main+0xba>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x34(%rbp),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12ce <main+0xfe>
lea    0xd61(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12ff <main+0x12f>
lea    0xd3f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    132c <main+0x15c>
lea    0xd1b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
jmp    1331 <main+0x161>
mov    -0x14(%rbp),%esi
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%edi
call   1090 <function@plt>
jmp    1331 <main+0x161>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x1,(%rsp)
cmp    $0x2,%edi
jne    12f8 <main+0x128>
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movzwl %bx,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1313 <main+0x143>
mov    %eax,%ebp
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     131c <main+0x14c>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1325 <main+0x155>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     132e <main+0x15e>
lea    0x18(%rsp),%r12
lea    0x4(%rsp),%r13
lea    0xdbb(%rip),%r15
nop
lea    0xd8c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0xc,0x4(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10c0 <accept@plt>
mov    %eax,%r14d
mov    0x18(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1090 <function@plt>
jmp    12b0 <main+0xe0>
mov    (%rsi),%rsi
lea    0xcfe(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    0xcfb(%rip),%rdi
jmp    1335 <main+0x165>
lea    0xcfd(%rip),%rdi
jmp    1335 <main+0x165>
lea    0xd03(%rip),%rdi
jmp    1335 <main+0x165>
lea    0xd03(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"%s <listening_port>\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"socket(): \00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [15 x i8] c"setsockopt(): \00"
@.str.3 = [9 x i8] c"bind(): \00"
@.str.4 = [11 x i8] c"listen(): \00"
@.str.5 = [39 x i8] c"[*] wait for a connection on port: %d\0A\00"
@.str.6 = [25 x i8] c"[*] connection from: %s\0A\00"
@SHUT_RDWR = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %12
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = bitcast %struct.sockaddr_in* %7 to i8*
%27 = call i32 @bzero(i8* %26, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = trunc i32 %30 to i16
%32 = call i32 @htons(i16 zeroext %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @PF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %21
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %21
%43 = load i32, i32* %9
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = bitcast i32* %12 to i8*
%47 = call i64 @setsockopt(i32 %43, i32 %44, i32 %45, i8* %46, i32 4)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %42
%53 = load i32, i32* %9
%54 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%55 = call i64 @bind(i32 %53, %struct.sockaddr* %54, i32 12)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @exit(i32 -1)
unreachable
60: ; preds = %52
%61 = load i32, i32* %9
%62 = call i64 @listen(i32 %61, i32 1)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%66 = call i32 @exit(i32 -1)
unreachable
67: ; preds = %60
br label %68
68: ; preds = %67, %68
%69 = load i32, i32* %6
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %69)
store i32 12, i32* %11
%71 = load i32, i32* %9
%72 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%73 = call i32 @accept(i32 %71, %struct.sockaddr* %72, i32* %11)
store i32 %73, i32* %10
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i8* @inet_ntoa(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %76)
%78 = load i32, i32* %10
%79 = call i32 @function(i32 %78)
br label %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"%s <listening_port>\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"socket(): \00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [15 x i8] c"setsockopt(): \00"
@.str.3 = [9 x i8] c"bind(): \00"
@.str.4 = [11 x i8] c"listen(): \00"
@.str.5 = [39 x i8] c"[*] wait for a connection on port: %d\0A\00"
@.str.6 = [25 x i8] c"[*] connection from: %s\0A\00"
@SHUT_RDWR = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = call i32 @bzero(i8* %7, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = trunc i32 %19 to i16
%24 = tail call i32 @htons(i16 zeroext %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @PF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = tail call i32 @socket(i32 %26, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %16
%31 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %16
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i64 @setsockopt(i32 %28, i32 %34, i32 %35, i8* %10, i32 4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %33
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @bind(i32 %28, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %41
%49 = call i64 @listen(i32 %28, i32 1)
%50 = icmp slt i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %48
%52 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %57
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %51, %57
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %19)
store i32 12, i32* %5
%59 = call i32 @accept(i32 %28, %struct.sockaddr* %52, i32* %5)
%60 = load i32, i32* %53
%61 = call i8* @inet_ntoa(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %61)
%63 = call i32 @function(i32 %59)
br label %57
}