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

/* Variables and functions */
int SOCK_DGRAM;
int accept(int, void *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int fork();
int /*<<< orphan*/ process_tcp(int);
int /*<<< orphan*/ process_udp(int);
void handle(int type, int sfd) {
  struct sockaddr_storage ss;
  socklen_t alen = sizeof(ss);
  int afd;
  if (type != SOCK_DGRAM) {
    if ((afd = accept(sfd, (void *)&ss, &alen)) == -1)
      err(1, "accept");
  } else
    afd = sfd;
  switch (fork()) {
  case -1:
    err(1, "fork");
  case 0:
    if (type == SOCK_DGRAM)
      process_udp(afd);
    else
      process_tcp(afd);
    break;
  default:
    close(afd);
    break;
  }
}


x86 assembly (clang, -O0):
<handle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%eax
je     11b9 <handle+0x59>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11b4 <handle+0x54>
mov    $0x1,%edi
lea    0xe51(%rip),%rsi
call   1050 <err@plt>
jmp    11bf <handle+0x5f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11e9 <handle+0x89>
jmp    11d9 <handle+0x79>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11fa <handle+0x9a>
jmp    1226 <handle+0xc6>
mov    $0x1,%edi
lea    0xe12(%rip),%rsi
call   1050 <err@plt>
mov    -0x4(%rbp),%eax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%eax
jne    1219 <handle+0xb9>
mov    -0x18(%rbp),%edi
call   1040 <process_udp@plt>
jmp    1221 <handle+0xc1>
mov    -0x18(%rbp),%edi
call   1060 <process_tcp@plt>
jmp    122e <handle+0xce>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
mov    0x2e6d(%rip),%r14
cmp    %edi,(%r14)
je     11ae <handle+0x4e>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1070 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ae <handle+0x4e>
lea    0xe61(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    $0xffffffff,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11cf <handle+0x6f>
cmp    $0xffffffff,%eax
jne    11dd <handle+0x7d>
lea    0xe42(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
cmp    %ebx,(%r14)
jne    11e6 <handle+0x86>
mov    %ebp,%edi
call   1040 <process_udp@plt>
jmp    11ed <handle+0x8d>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11ed <handle+0x8d>
mov    %ebp,%edi
call   1060 <process_tcp@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [5 x i8] c"fork\00"
define void @handle(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 4, i32* %6
%8 = load i32, i32* %3
%9 = load i32, i32* @SOCK_DGRAM
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* %4
%13 = bitcast %struct.sockaddr_storage* %5 to i8*
%14 = call i32 @accept(i32 %12, i8* %13, i32* %6)
store i32 %14, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
br label %21
19: ; preds = %2
%20 = load i32, i32* %4
store i32 %20, i32* %7
br label %21
21: ; preds = %19, %18
%22 = call i32 (...) @fork()
switch i32 %22, label %36 [
i32 -1, label %23
i32 0, label %25
]
23: ; preds = %21
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %21, %23
%26 = load i32, i32* %3
%27 = load i32, i32* @SOCK_DGRAM
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %7
%31 = call i32 @process_udp(i32 %30)
br label %35
32: ; preds = %25
%33 = load i32, i32* %7
%34 = call i32 @process_tcp(i32 %33)
br label %35
35: ; preds = %32, %29
br label %39
36: ; preds = %21
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %35
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [5 x i8] c"fork\00"
define void @handle(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_storage* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%7 = load i32, i32* @SOCK_DGRAM
%8 = icmp eq i32 %7, %0
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = call i32 @accept(i32 %1, i8* %5, i32* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %2, %9, %12
%15 = phi i32 [ -1, %12 ], [ %10, %9 ], [ %1, %2 ]
%16 = call i32 (...) @fork()
switch i32 %16, label %26 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %14
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %14, %17
%20 = load i32, i32* @SOCK_DGRAM
%21 = icmp eq i32 %20, %0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @process_udp(i32 %15)
br label %28
24: ; preds = %19
%25 = call i32 @process_tcp(i32 %15)
br label %28
26: ; preds = %14
%27 = call i32 @close(i32 %15)
br label %28
28: ; preds = %22, %24, %26
ret void
}