#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 */
typedef int /*<<< orphan*/ u_short;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ recv_buf;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int accept_loop(int soc) {
  struct sockaddr_in client_addr;
  int client_addr_len;
  int acc;
  char *client_ip_addr_str;
  u_short client_port;
  int ret = 0;
  char recv_buf[4096] = {0};
  char send_buf[4096] = {0};
  while (1) {
    client_addr_len = sizeof(client_addr);
    acc = accept(soc, (struct sockaddr *)&client_addr, &client_addr_len);
    if (acc < 0) {
      perror("accept");
      ret = -1;
      break;
    }
    client_ip_addr_str = inet_ntoa(client_addr.sin_addr);
    client_port = ntohs(client_addr.sin_port);
    printf("accept!(IPAddress = %s, Port = %hu)\n", client_ip_addr_str,
           client_port);
    memset(recv_buf, 0, sizeof(char) * 4096);
    recv(acc, recv_buf, sizeof(recv_buf), 0);
    printf("----- recv begin -----\n");
    printf("%s\n", recv_buf);
    printf("----- recv end -----\n");
    strcpy(send_buf, "HTTP/1.1 302 Found\r\n");
    strcat(send_buf, "Location: http://bgstation0.com/classic1/\r\n");
    strcat(send_buf, "\r\n");
    send(acc, send_buf, strlen(send_buf), 0);
    close(acc);
  }
  return ret;
}


x86 assembly (clang, -O0):
<accept_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
lea    -0x2030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
movl   $0x8,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1237 <accept_loop+0x77>
lea    0xdda(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    133c <accept_loop+0x17c>
mov    -0xc(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   10b0 <ntohs@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x1030(%rbp),%rsi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x2030(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x2030(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x2030(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x203c(%rbp)
lea    -0x2030(%rbp),%rax
mov    %rax,-0x2038(%rbp)
lea    -0x2030(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x203c(%rbp),%edi
mov    -0x2038(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x18(%rbp),%edi
call   10a0 <close@plt>
jmp    11fb <accept_loop+0x3b>
mov    -0x28(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %edi,%r13d
lea    0x1010(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   10a0 <memset@plt>
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   10a0 <memset@plt>
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r13d,%edi
call   10e0 <accept@plt>
test   %eax,%eax
js     1314 <accept_loop+0x144>
mov    %eax,%ebp
lea    0x1010(%rsp),%rbx
lea    0x10(%rsp),%r15
lea    0x4(%rsp),%r14
nop
mov    0xc(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%r12
mov    0x8(%rsp),%edi
call   10c0 <ntohs@plt>
lea    0xdab(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   10a0 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xdde(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
lea    0xde1(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
lea    0xd7a(%rip),%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
lea    0xd80(%rip),%rsi
call   10f0 <strcat@plt>
mov    %r15,%rdi
lea    0xd9a(%rip),%rsi
call   10f0 <strcat@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
mov    %ebp,%edi
call   10b0 <close@plt>
movl   $0x8,0x4(%rsp)
mov    %r13d,%edi
lea    0x8(%rsp),%rsi
mov    %r14,%rdx
call   10e0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1240 <accept_loop+0x70>
lea    0xce5(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [37 x i8] c"accept!(IPAddress = %s, Port = %hu)\0A\00"
@.str.2 = [24 x i8] c"----- recv begin -----\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [22 x i8] c"----- recv end -----\0A\00"
@.str.5 = [21 x i8] c"HTTP/1.1 302 Found\0D\0A\00"
@.str.6 = [44 x i8] c"Location: http://bgstation0.com/classic1/\0D\0A\00"
@.str.7 = [3 x i8] c"\0D\0A\00"
define i32 @accept_loop(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca [4096 x i8]
store i32 %0, i32* %2
store i32 0, i32* %8
%11 = bitcast [4096 x i8]* %9 to i8*
%12 = bitcast [4096 x i8]* %10 to i8*
br label %13
13: ; preds = %1, %21
store i32 8, i32* %4
%14 = load i32, i32* %2
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%16 = call i32 @accept(i32 %14, %struct.sockaddr* %15, i32* %4)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %8
br label %53
21: ; preds = %13
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i8* @inet_ntoa(i32 %23)
store i8* %24, i8** %6
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @ntohs(i32 %26)
store i32 %27, i32* %7
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 %29)
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 4096)
%33 = load i32, i32* %5
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%35 = call i32 @recv(i32 %33, i8* %34, i32 4096, i32 0)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%41 = call i32 @strcpy(i8* %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%43 = call i32 @strcat(i8* %42, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%45 = call i32 @strcat(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%46 = load i32, i32* %5
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @send(i32 %46, i8* %47, i32 %49, i32 0)
%51 = load i32, i32* %5
%52 = call i32 @close(i32 %51)
br label %13
53: ; preds = %19
%54 = load i32, i32* %8
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [37 x i8] c"accept!(IPAddress = %s, Port = %hu)\0A\00"
@.str.5 = [21 x i8] c"HTTP/1.1 302 Found\0D\0A\00"
@.str.6 = [44 x i8] c"Location: http://bgstation0.com/classic1/\0D\0A\00"
@.str.7 = [3 x i8] c"\0D\0A\00"
@str = [23 x i8] c"----- recv begin -----\00"
@str.8 = [21 x i8] c"----- recv end -----\00"
define i32 @accept_loop(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca [4096 x i8]
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%10 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
store i32 8, i32* %3
%11 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %18
16: ; preds = %18, %1
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
ret i32 -1
18: ; preds = %13, %18
%19 = phi i32 [ %11, %13 ], [ %36, %18 ]
%20 = load i32, i32* %14
%21 = call i8* @inet_ntoa(i32 %20)
%22 = load i32, i32* %15
%23 = call i32 @ntohs(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %21, i32 %23)
%25 = call i32 @memset(i8* %8, i32 0, i32 4096)
%26 = call i32 @recv(i32 %19, i8* %8, i32 4096, i32 0)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%28 = call i32 @puts(i8* %8)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%30 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%31 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%33 = call i32 @strlen(i8* %9)
%34 = call i32 @send(i32 %19, i8* %9, i32 %33, i32 0)
%35 = call i32 @close(i32 %19)
store i32 8, i32* %3
%36 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %3)
%37 = icmp slt i32 %36, 0
br i1 %37, label %16, label %18
}