#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 socklen_t;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ handle_request(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int startup();
int main() {
  int server_fd = -1;
  int client_fd = -1;
  struct sockaddr_in client_addr;
  char recv_buf[1024];
  int recv_len = 0;
  socklen_t client_addr_len = sizeof(client_addr);
  server_fd = startup();
  while (1) {
    client_fd =
        accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
    if (client_fd < 0) {
      perror("accept");
      exit(-1);
    }
    printf("client socket fd: %d\n", client_fd);
    handle_request(client_fd);
    close(client_fd);
  }
  close(server_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x414(%rbp)
movl   $0x4,-0x418(%rbp)
mov    $0x0,%al
call   1060 <startup@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x418(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e4 <main+0x74>
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%esi
lea    0xe19(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <handle_request@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    11ae <main+0x3e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0x4(%rsp)
xor    %eax,%eax
call   1060 <startup@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   1080 <accept@plt>
test   %eax,%eax
js     11eb <main+0x7b>
mov    %eax,%ebp
lea    0xe5c(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebp,%edi
call   1050 <handle_request@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c0 <main+0x50>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%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 = [22 x i8] c"client socket fd: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
store i32 -1, i32* %3
store i32 0, i32* %6
store i32 4, i32* %7
%8 = call i32 (...) @startup()
store i32 %8, i32* %2
br label %9
9: ; preds = %0, %18
%10 = load i32, i32* %2
%11 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%12 = call i32 @accept(i32 %10, %struct.sockaddr* %11, i32* %7)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %9
%19 = load i32, i32* %3
%20 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %3
%22 = call i32 @handle_request(i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
br label %9
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [22 x i8] c"client socket fd: %d\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 4, i32* %2
%5 = tail call i32 (...) @startup()
%6 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%7 = call i32 @accept(i32 %5, %struct.sockaddr* %6, i32* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %12, %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0, %12
%13 = phi i32 [ %17, %12 ], [ %7, %0 ]
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 @handle_request(i32 %13)
%16 = call i32 @close(i32 %13)
%17 = call i32 @accept(i32 %5, %struct.sockaddr* %6, i32* %2)
%18 = icmp slt i32 %17, 0
br i1 %18, label %9, label %12
}