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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ atoi(char const *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ processChild(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  int lfd, cfd;
  socklen_t clilen;
  pid_t childpid;
  struct sockaddr_in servaddr, cliaddr;
  if (argc != 2) {
    printf("Error : server port missing\n");
    exit(-1);
  }
  lfd = socket(AF_INET, SOCK_STREAM, 0);
  if (lfd < 0) {
    printf("Error : couldn't opening socket\n");
    exit(-1);
  }
  bzero((char *)&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = INADDR_ANY;
  servaddr.sin_port = htons(atoi(argv[1]));
  if (bind(lfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    printf("Error : binding port number\n");
  }
  listen(lfd, 5);
  clilen = sizeof(cliaddr);
  for (;;) {
    if ((cfd = accept(lfd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
      printf("Error : accept\n");
      exit(-1);
    }
    childpid = fork();
    if (childpid < 0) {
      error("Error : fork\n");
    }
    if (childpid == 0) {
      close(lfd);
      processChild(cfd);
      exit(0);
    }
    close(cfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1206 <main+0x36>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1242 <main+0x72>
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12a6 <main+0xd6>
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12ec <main+0x11c>
lea    0xd7e(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    130e <main+0x13e>
lea    0xd62(%rip),%rdi
call   1090 <error@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1330 <main+0x160>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <processChild@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    12ba <main+0xea>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
cmp    $0x2,%edi
jne    120e <main+0x3e>
mov    %rsi,%rbx
mov    0x2dfd(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
jns    121a <main+0x4a>
lea    0xe30(%rip),%rdi
jmp    12aa <main+0xda>
lea    0xe44(%rip),%rdi
jmp    12aa <main+0xda>
mov    %eax,%r15d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x8(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1278 <main+0xa8>
lea    0xdaa(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   10b0 <accept@plt>
test   %eax,%eax
jns    12b9 <main+0xe9>
lea    0xd64(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    %eax,%ebp
lea    0xd3e(%rip),%r14
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%rbx
jmp    12f3 <main+0x123>
xchg   %ax,%ax
mov    %r14,%rdi
call   1090 <error@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12a3 <main+0xd3>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     12d0 <main+0x100>
jne    12d8 <main+0x108>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1070 <processChild@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Error : server port missing\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"Error : couldn't opening socket\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [29 x i8] c"Error : binding port number\0A\00"
@.str.3 = [16 x i8] c"Error : accept\0A\00"
@.str.4 = [14 x i8] c"Error : fork\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %17
%27 = bitcast %struct.sockaddr_in* %10 to i8*
%28 = call i32 @bzero(i8* %27, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %26
%45 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %26
%47 = load i32, i32* %6
%48 = call i32 @listen(i32 %47, i32 5)
store i32 12, i32* %8
br label %49
49: ; preds = %72, %46
%50 = load i32, i32* %6
%51 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%52 = call i32 @accept(i32 %50, %struct.sockaddr* %51, i32* %8)
store i32 %52, i32* %7
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %49
%58 = call i64 (...) @fork()
store i64 %58, i64* %9
%59 = load i64, i64* %9
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %61, %57
%64 = load i64, i64* %9
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %63
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %7
%70 = call i32 @processChild(i32 %69)
%71 = call i32 @exit(i32 0)
unreachable
72: ; preds = %63
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
br label %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.4 = [14 x i8] c"Error : fork\0A\00"
@str = [15 x i8] c"Error : accept\00"
@str.5 = [28 x i8] c"Error : binding port number\00"
@str.6 = [32 x i8] c"Error : couldn't opening socket\00"
@str.7 = [28 x i8] c"Error : server port missing\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %13
%22 = call i32 @bzero(i8* %7, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 @atoi(i8* %28)
%30 = tail call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = call i64 @bind(i32 %16, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %21
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %21
%38 = call i32 @listen(i32 %16, i32 5)
store i32 12, i32* %3
%39 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%40 = call i32 @accept(i32 %16, %struct.sockaddr* %39, i32* %3)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %57, %37
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %37, %57
%46 = phi i32 [ %59, %57 ], [ %40, %37 ]
%47 = call i64 (...) @fork()
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %57
51: ; preds = %45
%52 = icmp eq i64 %47, 0
br i1 %52, label %53, label %57
53: ; preds = %51
%54 = call i32 @close(i32 %16)
%55 = call i32 @processChild(i32 %46)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %49, %51
%58 = call i32 @close(i32 %46)
%59 = call i32 @accept(i32 %16, %struct.sockaddr* %39, i32* %3)
%60 = icmp slt i32 %59, 0
br i1 %60, label %42, label %45
}