#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dotunneling(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void acceptloop(int sock, int commsock) {
  socklen_t sin_size;
  struct sockaddr_in remote_addr;
  int new_fd, pid;
  int maxfd;
  fd_set read_fds;
  FD_ZERO(&read_fds);
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    if ((new_fd = accept(sock, (struct sockaddr *)&remote_addr, &sin_size)) ==
        -1) {
      perror("accept");
      continue;
    }
    printf("server: got connection from %s\n", inet_ntoa(remote_addr.sin_addr));
    if (!(pid = fork())) {
      close(sock);
      dotunneling(new_fd);
      close(new_fd);
      exit(0);
    }
    if (pid == -1) {
      perror("Could not fork");
    }
    close(new_fd);
  }
}


x86 assembly (clang, -O0):
<acceptloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <FD_ZERO@plt>
movl   $0x4,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11db <acceptloop+0x4b>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
jmp    11a7 <acceptloop+0x17>
mov    -0x10(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    1224 <acceptloop+0x94>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   10a0 <dotunneling@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    123a <acceptloop+0xaa>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    11a7 <acceptloop+0x17>

x86 assembly (clang, -O2):
<acceptloop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x14(%rsp),%rdi
call   1060 <FD_ZERO@plt>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0xe45(%rip),%r12
lea    0xe45(%rip),%r13
jmp    11d0 <acceptloop+0x40>
mov    %r12,%rdi
call   1070 <perror@plt>
nopl   0x0(%rax)
movl   $0x4,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
cmp    $0xffffffff,%eax
je     11c4 <acceptloop+0x34>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1219 <acceptloop+0x89>
test   %eax,%eax
je     1227 <acceptloop+0x97>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11d0 <acceptloop+0x40>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
jmp    1210 <acceptloop+0x80>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   10a0 <dotunneling@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.2 = [15 x i8] c"Could not fork\00"
define void @acceptloop(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = call i32 @FD_ZERO(i32* %10)
br label %12
12: ; preds = %2, %17, %39
store i32 4, i32* %5
%13 = load i32, i32* %3
%14 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%15 = call i32 @accept(i32 %13, %struct.sockaddr* %14, i32* %5)
store i32 %15, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
19: ; preds = %12
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i8* @inet_ntoa(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 (...) @fork()
store i32 %24, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %19
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %7
%30 = call i32 @dotunneling(i32 %29)
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %19
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
br label %12
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.2 = [15 x i8] c"Could not fork\00"
define void @acceptloop(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
%10 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %12
12: ; preds = %17, %2
store i32 4, i32* %3
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %30
br label %12
18: ; preds = %12
%19 = load i32, i32* %11
%20 = call i8* @inet_ntoa(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 (...) @fork()
switch i32 %22, label %30 [
i32 0, label %23
i32 -1, label %28
]
23: ; preds = %18
%24 = call i32 @close(i32 %0)
%25 = call i32 @dotunneling(i32 %13)
%26 = call i32 @close(i32 %13)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %18
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %18, %28
%31 = call i32 @close(i32 %13)
br label %17
}