#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*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ WNOHANG;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, 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*/);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  int sockfd, new_fd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  int sin_size;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(3490);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  bzero(my_addr.sin_zero, sizeof(my_addr.sin_zero));
  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind");
    exit(1);
  }
  if (listen(sockfd, 10) == -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);
    while (waitpid(-1, NULL, WNOHANG) > 0)
      ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    122a <main+0x4a>
lea    0xde5(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0xda2,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x4,%esi
call   10c0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    128c <main+0xac>
lea    0xd8a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b8 <main+0xd8>
lea    0xd63(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12bd <main+0xdd>
movl   $0x10,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12f1 <main+0x111>
lea    0xd2c(%rip),%rdi
call   10b0 <perror@plt>
jmp    12bd <main+0xdd>
mov    -0x30(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd17(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%eax
jne    1352 <main+0x172>
mov    -0xc(%rbp),%edi
lea    0xd18(%rip),%rsi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1343 <main+0x163>
lea    0xd0b(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <waitpid@plt>
cmp    $0x0,%rax
jle    1380 <main+0x1a0>
jmp    135a <main+0x17a>
jmp    12bd <main+0xdd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2df3(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x36>
lea    0xdec(%rip),%rdi
jmp    127b <main+0x9b>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    $0xda2,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x4,%esi
call   10c0 <bzero@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1263 <main+0x83>
lea    0xda6(%rip),%rdi
jmp    127b <main+0x9b>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    128a <main+0xaa>
lea    0xd91(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd7f(%rip),%r13
mov    0x2d3e(%rip),%r12
jmp    12b0 <main+0xd0>
lea    0xd68(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x10,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0xc4>
mov    %eax,%ebp
mov    0x18(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1317 <main+0x137>
mov    %ebp,%edi
call   1070 <close@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   10a0 <waitpid@plt>
test   %rax,%rax
jg     1300 <main+0x120>
jmp    12b0 <main+0xd0>
lea    0xd1c(%rip),%rsi
mov    %ebp,%edi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    133d <main+0x15d>
lea    0xd11(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_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() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 3490)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @bzero(i32 undef, i32 4)
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i32 @bind(i32 %9, %struct.sockaddr* %22, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %14
%29 = call i32 @listen(i32 %9, i32 10)
%30 = icmp eq i32 %29, -1
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%33 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %42, %31
store i32 16, i32* %3
%38 = call i32 @accept(i32 %9, %struct.sockaddr* %33, i32* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %59, %40
br label %37
43: ; preds = %37
%44 = load i32, i32* %32
%45 = call i8* @inet_ntoa(i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = call i32 (...) @fork()
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %43
%50 = call i32 @send(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 14, i32 0)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = call i32 @close(i32 %38)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %43
%58 = call i32 @close(i32 %38)
br label %59
59: ; preds = %59, %57
%60 = load i32, i32* @WNOHANG
%61 = call i64 @waitpid(i32 -1, i32* null, i32 %60)
%62 = icmp sgt i64 %61, 0
br i1 %62, label %59, label %42
}