#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 /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int serve_new_conn(int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sk, port, ret;
  struct sockaddr_in addr;
  if (argc < 2) {
    printf("Need port\n");
    return -1;
  }
  signal(SIGCHLD, SIG_IGN);
  sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sk < 0) {
    perror("Can't create socket");
    return -1;
  }
  port = atoi(argv[1]);
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  printf("Binding to port %d\n", port);
  ret = bind(sk, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    perror("Can't bind socket");
    return -1;
  }
  ret = listen(sk, 16);
  if (ret < 0) {
    perror("Can't put sock to listen");
    return -1;
  }
  printf("Waiting for connections\n");
  while (1) {
    int ask, pid;
    ask = accept(sk, NULL, NULL);
    if (ask < 0) {
      perror("Can't accept new conn");
      return -1;
    }
    pid = fork();
    if (pid < 0) {
      perror("Can't fork");
      return -1;
    }
    if (pid > 0)
      close(ask);
    else {
      close(sk);
      ret = serve_new_conn(ask);
      exit(ret);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    122a <main+0x3a>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ec <main+0x1fc>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1286 <main+0x96>
lea    0xd96(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ec <main+0x1fc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1315 <main+0x125>
lea    0xd2f(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ec <main+0x1fc>
mov    -0x14(%rbp),%edi
mov    $0x10,%esi
call   1090 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1347 <main+0x157>
lea    0xd0f(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ec <main+0x1fc>
lea    0xd10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1389 <main+0x199>
lea    0xcff(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ec <main+0x1fc>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    13b5 <main+0x1c5>
lea    0xce9(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ec <main+0x1fc>
cmpl   $0x0,-0x30(%rbp)
jle    13cc <main+0x1dc>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
jmp    13e7 <main+0x1f7>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%edi
call   10f0 <serve_new_conn@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10e0 <exit@plt>
jmp    1355 <main+0x165>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     1227 <main+0x27>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rsi,%rbp
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
call   1120 <socket@plt>
test   %eax,%eax
jns    1271 <main+0x71>
lea    0xd94(%rip),%rdi
jmp    1355 <main+0x155>
mov    %eax,%ebx
mov    0x8(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
lea    0xd54(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %eax,%eax
jns    12e5 <main+0xe5>
lea    0xd45(%rip),%rdi
jmp    1355 <main+0x155>
mov    %ebx,%edi
mov    $0x10,%esi
call   10a0 <listen@plt>
test   %eax,%eax
jns    12fe <main+0xfe>
lea    0xd3e(%rip),%rdi
jmp    1355 <main+0x155>
lea    0xd6f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
test   %eax,%eax
js     1345 <main+0x145>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
js     134e <main+0x14e>
je     135f <main+0x15f>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1320 <main+0x120>
lea    0xd07(%rip),%rdi
jmp    1355 <main+0x155>
lea    0xd14(%rip),%rdi
call   10c0 <perror@plt>
jmp    1219 <main+0x19>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%edi
call   1100 <serve_new_conn@plt>
mov    %eax,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"Need port\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [20 x i8] c"Can't create socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [20 x i8] c"Binding to port %d\0A\00"
@.str.3 = [18 x i8] c"Can't bind socket\00"
@.str.4 = [25 x i8] c"Can't put sock to listen\00"
@.str.5 = [25 x i8] c"Waiting for connections\0A\00"
@.str.6 = [22 x i8] c"Can't accept new conn\00"
@.str.7 = [11 x i8] c"Can't fork\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 %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
16: ; preds = %2
%17 = load i32, i32* @SIGCHLD
%18 = load i32, i32* @SIG_IGN
%19 = call i32 @signal(i32 %17, i32 %18)
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = load i32, i32* @IPPROTO_TCP
%23 = call i32 @socket(i32 %20, i32 %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
28: ; preds = %16
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %7
%33 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %7
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %41, i32* %42
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%47 = call i32 @bind(i32 %45, %struct.sockaddr* %46, i32 12)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %28
%51 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
52: ; preds = %28
%53 = load i32, i32* %6
%54 = call i32 @listen(i32 %53, i32 16)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
59: ; preds = %52
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %59, %87
%62 = load i32, i32* %6
%63 = call i32 @accept(i32 %62, i32* null, i32* null)
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
68: ; preds = %61
%69 = call i32 (...) @fork()
store i32 %69, i32* %11
%70 = load i32, i32* %11
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
74: ; preds = %68
%75 = load i32, i32* %11
%76 = icmp sgt i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i32, i32* %10
%79 = call i32 @close(i32 %78)
br label %87
80: ; preds = %74
%81 = load i32, i32* %6
%82 = call i32 @close(i32 %81)
%83 = load i32, i32* %10
%84 = call i32 @serve_new_conn(i32 %83)
store i32 %84, i32* %8
%85 = load i32, i32* %8
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %77
br label %61
88: ; preds = %72, %66, %57, %50, %26, %14
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [20 x i8] c"Can't create socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [20 x i8] c"Binding to port %d\0A\00"
@.str.3 = [18 x i8] c"Can't bind socket\00"
@.str.4 = [25 x i8] c"Can't put sock to listen\00"
@.str.6 = [22 x i8] c"Can't accept new conn\00"
@.str.7 = [11 x i8] c"Can't fork\00"
@str = [24 x i8] c"Waiting for connections\00"
@str.8 = [10 x i8] c"Need port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.8, i64 0, i64 0))
br label %64
8: ; preds = %2
%9 = load i32, i32* @SIGCHLD
%10 = load i32, i32* @SIG_IGN
%11 = tail call i32 @signal(i32 %9, i32 %10)
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = tail call i32 @socket(i32 %12, i32 %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %64
19: ; preds = %8
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %27, i32* %28
%29 = call i32 @htons(i32 %22)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @bind(i32 %15, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %64
37: ; preds = %19
%38 = call i32 @listen(i32 %15, i32 16)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %64
42: ; preds = %37
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%44 = call i32 @accept(i32 %15, i32* null, i32* null)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %60, %42
%47 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %64
48: ; preds = %42, %60
%49 = phi i32 [ %62, %60 ], [ %44, %42 ]
%50 = call i32 (...) @fork()
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %64
54: ; preds = %48
%55 = icmp eq i32 %50, 0
br i1 %55, label %56, label %60
56: ; preds = %54
%57 = call i32 @close(i32 %15)
%58 = call i32 @serve_new_conn(i32 %49)
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %54
%61 = call i32 @close(i32 %49)
%62 = call i32 @accept(i32 %15, i32* null, i32* null)
%63 = icmp slt i32 %62, 0
br i1 %63, label %46, label %48
64: ; preds = %46, %52, %40, %35, %17, %6
ret i32 -1
}