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

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
scalar_t__ fork();
int /*<<< orphan*/ handle_request();
int /*<<< orphan*/ printf(char *);
void accept_connection(int sockfd, int newsockfd, int client_size,
                       struct sockaddr_in clientaddr) {
  newsockfd =
      accept(sockfd, (struct sockaddr *)&clientaddr, (socklen_t *)&client_size);
  if (newsockfd < 0)
    error("ACCEPTING ERROR");
  printf("Client accepted\n");
  pid_t pid = fork();
  if (pid < 0)
    error("FORKING FAILED");
  if (pid) {
    close(newsockfd);
  } else {
    close(sockfd);
    handle_request();
    close(newsockfd);
  }
}


x86 assembly (clang, -O0):
<accept_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %ecx,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    119d <accept_connection+0x3d>
lea    0xe68(%rip),%rdi
call   1060 <error@plt>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11cb <accept_connection+0x6b>
lea    0xe5b(%rip),%rdi
call   1060 <error@plt>
cmpq   $0x0,-0x20(%rbp)
je     11e3 <accept_connection+0x83>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
jmp    11fa <accept_connection+0x9a>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <handle_request@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_connection>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %ecx,(%rsp)
mov    %edx,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1070 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    118b <accept_connection+0x2b>
lea    0xe7a(%rip),%rdi
call   1060 <error@plt>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11b5 <accept_connection+0x55>
jne    11c1 <accept_connection+0x61>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1050 <handle_request@plt>
jmp    11c1 <accept_connection+0x61>
lea    0xe54(%rip),%rdi
call   1060 <error@plt>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"ACCEPTING ERROR\00"
@.str.1 = [17 x i8] c"Client accepted\0A\00"
@.str.2 = [15 x i8] c"FORKING FAILED\00"
define void @accept_connection(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %3, i32* %10
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
%11 = load i32, i32* %6
%12 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%13 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %8)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %4
%19 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = call i64 (...) @fork()
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = load i64, i64* %9
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
br label %37
31: ; preds = %25
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = call i32 (...) @handle_request()
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %31, %28
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"ACCEPTING ERROR\00"
@.str.2 = [15 x i8] c"FORKING FAILED\00"
@str = [16 x i8] c"Client accepted\00"
define void @accept_connection(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %3, i32* %7
store i32 %2, i32* %6
%8 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%9 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %6)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %4
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%15 = call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %24
19: ; preds = %13
%20 = icmp eq i64 %15, 0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = call i32 @close(i32 %0)
%23 = call i32 (...) @handle_request()
br label %24
24: ; preds = %19, %17, %21
%25 = call i32 @close(i32 %9)
ret void
}