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

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ parsereq(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int handle(int fd) {
  char buf[1024];
  int rfd, reqlen;
  socklen_t salen;
  struct sockaddr sa;
  rfd = accept(fd, &sa, &salen);
  if (rfd < 0) {
    fprintf(stderr, "cannot accept request: %s\n", strerror(errno));
    close(rfd);
    return -1;
  }
  reqlen = recv(rfd, buf, 1024 - 1, 0);
  if (reqlen < 0) {
    fprintf(stderr, "recv error: %s\n", strerror(errno));
    close(rfd);
    return -1;
  }
  if (parsereq(buf) < 0) {
    fprintf(stderr, "unable to parse req: %s\n", buf);
    close(rfd);
    return -1;
  }
  close(rfd);
  return 0;
}


x86 assembly (clang, -O0):
<handle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x41c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    11e0 <handle+0x80>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x424(%rbp),%edi
mov    %rax,%rdx
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <handle+0x150>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    1255 <handle+0xf5>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x428(%rbp),%edi
mov    %rax,%rdx
lea    0xde2(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <handle+0x150>
lea    -0x410(%rbp),%rdi
call   1070 <parsereq@plt>
cmp    $0x0,%rax
jge    129e <handle+0x13e>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xda9(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <handle+0x150>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1060 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11bd <handle+0x5d>
lea    0x10(%rsp),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11dd <handle+0x7d>
lea    0x10(%rsp),%rdi
call   1070 <parsereq@plt>
test   %rax,%rax
jns    120a <handle+0xaa>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
lea    0x10(%rsp),%rdx
jmp    1200 <handle+0xa0>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe25(%rip),%rsi
jmp    11fb <handle+0x9b>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebp
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe20(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"cannot accept request: %s\0A\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"recv error: %s\0A\00"
@.str.2 = [25 x i8] c"unable to parse req: %s\0A\00"
define i32 @handle(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @accept(i32 %9, %struct.sockaddr* %8, i32* %7)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
store i32 -1, i32* %2
br label %46
20: ; preds = %1
%21 = load i32, i32* %5
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = call i32 @recv(i32 %21, i8* %22, i32 1023, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %2
br label %46
33: ; preds = %20
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%35 = call i64 @parsereq(i8* %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %2
br label %46
43: ; preds = %33
%44 = load i32, i32* %5
%45 = call i32 @close(i32 %44)
store i32 0, i32* %2
br label %46
46: ; preds = %43, %37, %26, %13
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"cannot accept request: %s\0A\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"recv error: %s\0A\00"
@.str.2 = [25 x i8] c"unable to parse req: %s\0A\00"
define i32 @handle(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca %struct.sockaddr
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr* %4 to i8*
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %4, i32* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %29
15: ; preds = %1
%16 = call i32 @recv(i32 %8, i8* %5, i32 1023, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %29
23: ; preds = %15
%24 = call i64 @parsereq(i8* %5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %29
29: ; preds = %23, %26, %18, %10
%30 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %26 ], [ 0, %23 ]
%31 = call i32 @close(i32 %8)
ret i32 %30
}