#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ WNOHANG;
int accept(int, struct sockaddr *, unsigned int *);
unsigned int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htons(unsigned int);
char *inet_ntoa(TYPE_1__);
int listen(int, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd, new_fd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  unsigned int sin_size, myport, lisnum;
  if (argv[1])
    myport = atoi(argv[1]);
  else
    myport = 7838;
  if (argv[2])
    lisnum = atoi(argv[2]);
  else
    lisnum = 2;
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  my_addr.sin_family = PF_INET;
  my_addr.sin_port = htons(myport);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  bzero(&(my_addr.sin_zero), 0);
  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind");
    exit(1);
  }
  if (listen(sockfd, lisnum) == -1) {
    perror("listen");
    exit(1);
  }
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) ==
        -1) {
      perror("accept");
      continue;
    }
    printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
    if (!fork()) {
      if (send(new_fd, "Hello, world!\n", 14, 0) == -1) {
        perror("send");
        close(new_fd);
        exit(0);
      }
    }
    close(new_fd);
    waitpid(-1, NULL, WNOHANG);
  }
}


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)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     122a <main+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x40(%rbp)
jmp    1231 <main+0x41>
movl   $0x1e9e,-0x40(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1255 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x44(%rbp)
jmp    125c <main+0x6c>
movl   $0x2,-0x44(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1297 <main+0xa7>
lea    0xd78(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x40(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
xor    %esi,%esi
call   10c0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12f9 <main+0x109>
lea    0xd1d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x44(%rbp),%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1323 <main+0x133>
lea    0xcf8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    1328 <main+0x138>
movl   $0x10,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    135c <main+0x16c>
lea    0xcc1(%rip),%rdi
call   10b0 <perror@plt>
jmp    1328 <main+0x138>
mov    -0x38(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xcac(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
cmp    $0x0,%eax
jne    13c2 <main+0x1d2>
mov    -0x18(%rbp),%edi
lea    0xcad(%rip),%rsi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    13bd <main+0x1cd>
lea    0xca0(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
jmp    13c2 <main+0x1d2>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    0x2c17(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <waitpid@plt>
jmp    1328 <main+0x138>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    $0x1e9e,%r15d
test   %rdi,%rdi
je     1218 <main+0x28>
call   10e0 <atoi@plt>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
mov    $0x2,%r14d
test   %rdi,%rdi
je     122f <main+0x3f>
call   10e0 <atoi@plt>
mov    %eax,%r14d
mov    0x2d92(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
jne    1257 <main+0x67>
lea    0xdab(%rip),%rdi
jmp    12ba <main+0xca>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   10c0 <bzero@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a4 <main+0xb4>
lea    0xd65(%rip),%rdi
jmp    12ba <main+0xca>
mov    %ebx,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c9 <main+0xd9>
lea    0xd52(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd40(%rip),%r13
mov    0x2d07(%rip),%rbp
jmp    1307 <main+0x117>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12d,%edi
call   1070 <close@plt>
mov    0x0(%rbp),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   10a0 <waitpid@plt>
movl   $0x10,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     1360 <main+0x170>
mov    %eax,%r12d
mov    0x18(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    12f0 <main+0x100>
mov    %r12d,%edi
lea    0xced(%rip),%rsi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    12f0 <main+0x100>
jmp    136e <main+0x17e>
lea    0xcac(%rip),%rdi
call   10b0 <perror@plt>
jmp    1307 <main+0x117>
lea    0xcd4(%rip),%rdi
call   10b0 <perror@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.5 = [15 x i8] c"Hello, world!\0A\00"
@.str.6 = [5 x i8] c"send\00"
@WNOHANG = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %11
br label %23
22: ; preds = %2
store i32 7838, i32* %11
br label %23
23: ; preds = %22, %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %12
br label %34
33: ; preds = %23
store i32 2, i32* %12
br label %34
34: ; preds = %33, %28
%35 = load i32, i32* @PF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
%43 = load i32, i32* @PF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 3
store i32 %43, i32* %44
%45 = load i32, i32* %11
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %46, i32* %47
%48 = load i32, i32* @INADDR_ANY
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
store i32 %48, i32* %50
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%52 = call i32 @bzero(i32* %51, i32 0)
%53 = load i32, i32* %6
%54 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%55 = call i32 @bind(i32 %53, %struct.sockaddr* %54, i32 4)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %42
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %42
%61 = load i32, i32* %6
%62 = load i32, i32* %12
%63 = call i32 @listen(i32 %61, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %68
65: ; preds = %60
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %60
br label %69
69: ; preds = %68, %74, %94
store i32 16, i32* %10
%70 = load i32, i32* %6
%71 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%72 = call i32 @accept(i32 %70, %struct.sockaddr* %71, i32* %10)
store i32 %72, i32* %7
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %69
76: ; preds = %69
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%78 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i8* @inet_ntoa(i32 %79)
%81 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %80)
%82 = call i32 (...) @fork()
%83 = icmp ne i32 %82, 0
br i1 %83, label %94, label %84
84: ; preds = %76
%85 = load i32, i32* %7
%86 = call i32 @send(i32 %85, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 14, i32 0)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %93
88: ; preds = %84
%89 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%90 = load i32, i32* %7
%91 = call i32 @close(i32 %90)
%92 = call i32 @exit(i32 0)
unreachable
93: ; preds = %84
br label %94
94: ; preds = %93, %76
%95 = load i32, i32* %7
%96 = call i32 @close(i32 %95)
%97 = load i32, i32* @WNOHANG
%98 = call i32 @waitpid(i32 -1, i32* null, i32 %97)
br label %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.5 = [15 x i8] c"Hello, world!\0A\00"
@.str.6 = [5 x i8] c"send\00"
@WNOHANG = 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 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = tail call i32 @atoi(i8* %10)
br label %14
14: ; preds = %2, %12
%15 = phi i32 [ %13, %12 ], [ 7838, %2 ]
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 @atoi(i8* %17)
br label %21
21: ; preds = %14, %19
%22 = phi i32 [ %20, %19 ], [ 2, %14 ]
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = tail call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i32, i32* @PF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %31, i32* %32
%33 = tail call i32 @htons(i32 %15)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_ANY
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%38 = call i32 @bzero(i32* %37, i32 0)
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @bind(i32 %25, %struct.sockaddr* %39, i32 4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = call i32 @listen(i32 %25, i32 %22)
%47 = icmp eq i32 %46, -1
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %54
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %59, %48
store i32 16, i32* %5
%55 = call i32 @accept(i32 %25, %struct.sockaddr* %49, i32* %5)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %73
br label %54
60: ; preds = %54
%61 = load i32, i32* %50
%62 = call i8* @inet_ntoa(i32 %61)
%63 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %62)
%64 = call i32 (...) @fork()
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %60
%67 = call i32 @send(i32 %55, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 14, i32 0)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%71 = call i32 @close(i32 %55)
%72 = call i32 @exit(i32 0)
unreachable
73: ; preds = %66, %60
%74 = call i32 @close(i32 %55)
%75 = load i32, i32* @WNOHANG
%76 = call i32 @waitpid(i32 -1, i32* null, i32 %75)
br label %59
}