#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 stat {
  scalar_t__ st_size;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ sc_size;
typedef int /*<<< orphan*/ platform;
typedef scalar_t__ off_t;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
scalar_t__ getpeername(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, unsigned int *, int);
scalar_t__ sendfile(int, int, scalar_t__ *, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, unsigned int *, int);
void do_child(int fd) {
  char host[128];
  unsigned int platform = 0, sc_size = 0;
  const char *fn = NULL, *plat = NULL;
  ssize_t rret = 0;
  struct sockaddr_in sin;
  struct stat sb;
  off_t sfd_off = 0;
  int sfd = 0;
  socklen_t slen = 0;
  slen = sizeof(sin);
  if (getpeername(fd, (struct sockaddr *)&sin, &slen) < 0) {
    perror("getpeername");
    goto error;
  }
  if (sin.sin_family == AF_INET) {
    sprintf(host, "%s:%u", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  } else {
    printf("whoa, non inet4?\n");
    goto error;
  }
  rret = read(fd, &platform, sizeof(platform));
  if (rret < 0) {
    perror("read");
    close(fd);
    goto error;
  }
  switch (platform) {
  case 0:
    printf("what?\n");
    break;
  case 1:
    plat = "linux-x86";
    fn = "linux-x86.bin";
    break;
  case 2:
    plat = "netbsd-x86";
    fn = "netbsd-x86.bin";
    break;
  default:
    printf("Unknown platform %d\n", platform);
    goto error;
  }
  if (stat(fn, &sb) < 0) {
    fprintf(stderr, "Cant open %s: %s", fn, strerror(errno));
    goto error;
  }
  sc_size = (unsigned int)sb.st_size;
  printf("%s %s payload is %u bytes\n", host, plat, sc_size);
  if (write(fd, &sc_size, sizeof(sc_size)) < 0) {
    perror("write");
    goto error;
  }
  sfd = open(fn, O_RDONLY);
  if (sfd < 0) {
    fprintf(stderr, "Cant open %s: %s", fn, strerror(errno));
    goto error;
  }
  sfd_off = 0;
  if (sendfile(fd, sfd, &sfd_off, sb.st_size) < 0) {
    perror("sendfile");
    goto error;
  }
error:
  printf("%s Closing\n", host);
  fflush(stdout);
  if (sfd)
    close(sfd);
  if (fd)
    close(fd);
  return;
}


x86 assembly (clang, -O0):
<do_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
movq   $0x0,-0xa0(%rbp)
movq   $0x0,-0xa8(%rbp)
movq   $0x0,-0xb0(%rbp)
movq   $0x0,-0xd0(%rbp)
movl   $0x0,-0xd4(%rbp)
movl   $0x0,-0xd8(%rbp)
movl   $0x10,-0xd8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0xd8(%rbp),%rdx
call   1050 <getpeername@plt>
cmp    $0x0,%rax
jge    128d <do_child+0x9d>
lea    0xd7d(%rip),%rdi
call   10f0 <perror@plt>
jmp    1576 <do_child+0x386>
mov    -0xc0(%rbp),%rax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
jne    12f0 <do_child+0x100>
lea    -0x90(%rbp),%rax
mov    %rax,-0xe8(%rbp)
mov    -0xb4(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0xe0(%rbp)
mov    -0xb8(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0xe8(%rbp),%rdi
mov    -0xe0(%rbp),%rdx
mov    %eax,%ecx
lea    0xd26(%rip),%rsi
call   1100 <sprintf@plt>
jmp    1303 <do_child+0x113>
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1576 <do_child+0x386>
mov    -0x4(%rbp),%edi
lea    -0x94(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jge    1345 <do_child+0x155>
lea    0xcf1(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
jmp    1576 <do_child+0x386>
mov    -0x94(%rbp),%eax
mov    %eax,-0xec(%rbp)
test   %eax,%eax
je     1386 <do_child+0x196>
jmp    135e <do_child+0x16e>
mov    -0xec(%rbp),%eax
sub    $0x1,%eax
je     1399 <do_child+0x1a9>
jmp    1372 <do_child+0x182>
mov    -0xec(%rbp),%eax
sub    $0x2,%eax
je     13ba <do_child+0x1ca>
jmp    13db <do_child+0x1eb>
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13f4 <do_child+0x204>
lea    0xc90(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    0xc8c(%rip),%rax
mov    %rax,-0xa0(%rbp)
jmp    13f4 <do_child+0x204>
lea    0xc87(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    0xc84(%rip),%rax
mov    %rax,-0xa0(%rbp)
jmp    13f4 <do_child+0x204>
mov    -0x94(%rbp),%esi
lea    0xc7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1576 <do_child+0x386>
mov    -0xa0(%rbp),%rdi
lea    -0xc8(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jge    145d <do_child+0x26d>
mov    0x2bb8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xfc(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xf8(%rbp)
mov    0x2b93(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0xfc(%rbp),%edi
mov    -0xf8(%rbp),%rdx
mov    %rax,%rcx
lea    0xc24(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1576 <do_child+0x386>
mov    -0xc8(%rbp),%rax
mov    %eax,-0x98(%rbp)
lea    -0x90(%rbp),%rsi
mov    -0xa8(%rbp),%rdx
mov    -0x98(%rbp),%ecx
lea    0xc03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    14bb <do_child+0x2cb>
lea    0xbf2(%rip),%rdi
call   10f0 <perror@plt>
jmp    1576 <do_child+0x386>
mov    -0xa0(%rbp),%rdi
mov    0x2b17(%rip),%rax
mov    (%rax),%esi
call   10e0 <open@plt>
mov    %eax,-0xd4(%rbp)
cmpl   $0x0,-0xd4(%rbp)
jge    152f <do_child+0x33f>
mov    0x2ae6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10c(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0x108(%rbp)
mov    0x2ac1(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rdx
mov    %rax,%rcx
lea    0xb52(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1576 <do_child+0x386>
movq   $0x0,-0xd0(%rbp)
mov    -0x4(%rbp),%edi
mov    -0xd4(%rbp),%esi
mov    -0xc8(%rbp),%rcx
lea    -0xd0(%rbp),%rdx
call   10c0 <sendfile@plt>
cmp    $0x0,%rax
jge    1571 <do_child+0x381>
lea    0xb42(%rip),%rdi
call   10f0 <perror@plt>
jmp    1576 <do_child+0x386>
jmp    1576 <do_child+0x386>
lea    -0x90(%rbp),%rsi
lea    0xb2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2a2e(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
cmpl   $0x0,-0xd4(%rbp)
je     15b1 <do_child+0x3c1>
mov    -0xd4(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x4(%rbp)
je     15c3 <do_child+0x3d3>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0x10,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1060 <getpeername@plt>
test   %rax,%rax
js     13f9 <do_child+0x1f9>
mov    0x20(%rsp),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <do_child+0xc0>
mov    0x2c(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x28(%rsp),%edi
call   10e0 <ntohs@plt>
lea    0xd9f(%rip),%rsi
lea    0x30(%rsp),%rdi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1110 <sprintf@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1090 <read@plt>
test   %rax,%rax
js     1409 <do_child+0x209>
mov    0x4(%rsp),%esi
cmp    $0x2,%esi
je     12d3 <do_child+0xd3>
cmp    $0x1,%esi
je     12e3 <do_child+0xe3>
test   %esi,%esi
jne    13b8 <do_child+0x1b8>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    12f1 <do_child+0xf1>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    142c <do_child+0x22c>
lea    0xd55(%rip),%r15
lea    0xd59(%rip),%r14
jmp    12f1 <do_child+0xf1>
lea    0xd2d(%rip),%r15
lea    0xd30(%rip),%r14
lea    0x18(%rsp),%rsi
mov    %r14,%rdi
call   10b0 <stat@plt>
test   %rax,%rax
js     1389 <do_child+0x189>
mov    0x18(%rsp),%ecx
mov    %ecx,0xc(%rsp)
lea    0xd59(%rip),%rdi
xor    %ebp,%ebp
lea    0x30(%rsp),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <write@plt>
test   %rax,%rax
js     1420 <do_child+0x220>
mov    0x2c98(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10f0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     13ca <do_child+0x1ca>
movq   $0x0,0x10(%rsp)
mov    0x18(%rsp),%rcx
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %ebp,%esi
call   10d0 <sendfile@plt>
test   %rax,%rax
jns    142c <do_child+0x22c>
lea    0xd0c(%rip),%rdi
jmp    1427 <do_child+0x227>
mov    0x2c40(%rip),%rax
mov    (%rax),%ebp
mov    0x2c2f(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xcb7(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   10a0 <fprintf@plt>
xor    %ebp,%ebp
jmp    142c <do_child+0x22c>
lea    0xc8a(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <printf@plt>
jmp    142c <do_child+0x22c>
mov    0x2bff(%rip),%rax
mov    (%rax),%r15d
mov    0x2bed(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc75(%rip),%rsi
mov    %r15d,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   10a0 <fprintf@plt>
jmp    142c <do_child+0x22c>
lea    0xc00(%rip),%rdi
call   1100 <perror@plt>
xor    %ebp,%ebp
jmp    142c <do_child+0x22c>
lea    0xc02(%rip),%rdi
call   1100 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %ebp,%ebp
jmp    142c <do_child+0x22c>
lea    0xc63(%rip),%rdi
call   1100 <perror@plt>
lea    0xc66(%rip),%rdi
lea    0x30(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2b7a(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
test   %ebp,%ebp
je     1458 <do_child+0x258>
mov    %ebp,%edi
call   1080 <close@plt>
test   %ebx,%ebx
je     1463 <do_child+0x263>
mov    %ebx,%edi
call   1080 <close@plt>
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.stat = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getpeername\00"
@AF_INET = global i64 0
@.str.1 = [6 x i8] c"%s:%u\00"
@.str.2 = [18 x i8] c"whoa, non inet4?\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [7 x i8] c"what?\0A\00"
@.str.5 = [10 x i8] c"linux-x86\00"
@.str.6 = [14 x i8] c"linux-x86.bin\00"
@.str.7 = [11 x i8] c"netbsd-x86\00"
@.str.8 = [15 x i8] c"netbsd-x86.bin\00"
@.str.9 = [21 x i8] c"Unknown platform %d\0A\00"
@stderr = global i32 0
@.str.10 = [17 x i8] c"Cant open %s: %s\00"
@errno = global i32 0
@.str.11 = [27 x i8] c"%s %s payload is %u bytes\0A\00"
@.str.12 = [6 x i8] c"write\00"
@O_RDONLY = global i32 0
@.str.13 = [9 x i8] c"sendfile\00"
@.str.14 = [12 x i8] c"%s Closing\0A\00"
@stdout = global i32 0
define void @do_child(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.stat
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
store i32 0, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
store i64 0, i64* %8
store i64 0, i64* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 16, i32* %13
%14 = load i32, i32* %2
%15 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%16 = call i64 @getpeername(i32 %14, %struct.sockaddr* %15, i32* %13)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %99
20: ; preds = %1
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i64, i64* @AF_INET
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%28 = load i32, i32* %27
%29 = call i8* @inet_ntoa(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @ntohs(i32 %31)
%33 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %32)
br label %36
34: ; preds = %20
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %99
36: ; preds = %25
%37 = load i32, i32* %2
%38 = call i64 @read(i32 %37, i32* %4, i32 4)
store i64 %38, i64* %8
%39 = load i64, i64* %8
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
br label %99
45: ; preds = %36
%46 = load i32, i32* %4
switch i32 %46, label %51 [
i32 0, label %47
i32 1, label %49
i32 2, label %50
]
47: ; preds = %45
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %54
49: ; preds = %45
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8** %6
br label %54
50: ; preds = %45
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8** %6
br label %54
51: ; preds = %45
%52 = load i32, i32* %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %52)
br label %99
54: ; preds = %50, %49, %47
%55 = load i8*, i8** %6
%56 = call i64 @stat(i8* %55, %struct.stat* %10)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %54
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %6
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %60, i8* %62)
br label %99
64: ; preds = %54
%65 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%66 = load i64, i64* %65
%67 = trunc i64 %66 to i32
store i32 %67, i32* %5
%68 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%69 = load i8*, i8** %7
%70 = load i32, i32* %5
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i8* %68, i8* %69, i32 %70)
%72 = load i32, i32* %2
%73 = call i64 @write(i32 %72, i32* %5, i32 4)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %64
%76 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
br label %99
77: ; preds = %64
%78 = load i8*, i8** %6
%79 = load i32, i32* @O_RDONLY
%80 = call i32 @open(i8* %78, i32 %79)
store i32 %80, i32* %12
%81 = load i32, i32* %12
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %89
83: ; preds = %77
%84 = load i32, i32* @stderr
%85 = load i8*, i8** %6
%86 = load i32, i32* @errno
%87 = call i8* @strerror(i32 %86)
%88 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %85, i8* %87)
br label %99
89: ; preds = %77
store i64 0, i64* %11
%90 = load i32, i32* %2
%91 = load i32, i32* %12
%92 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%93 = load i64, i64* %92
%94 = call i64 @sendfile(i32 %90, i32 %91, i64* %11, i64 %93)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %89
%97 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0))
br label %99
98: ; preds = %89
br label %99
99: ; preds = %98, %96, %83, %75, %58, %51, %41, %34, %18
%100 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.14, i64 0, i64 0), i8* %100)
%102 = load i32, i32* @stdout
%103 = call i32 @fflush(i32 %102)
%104 = load i32, i32* %12
%105 = icmp ne i32 %104, 0
br i1 %105, label %106, label %109
106: ; preds = %99
%107 = load i32, i32* %12
%108 = call i32 @close(i32 %107)
br label %109
109: ; preds = %106, %99
%110 = load i32, i32* %2
%111 = icmp ne i32 %110, 0
br i1 %111, label %112, label %115
112: ; preds = %109
%113 = load i32, i32* %2
%114 = call i32 @close(i32 %113)
br label %115
115: ; preds = %112, %109
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.stat = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getpeername\00"
@AF_INET = global i64 0
@.str.1 = [6 x i8] c"%s:%u\00"
@.str.3 = [5 x i8] c"read\00"
@.str.5 = [10 x i8] c"linux-x86\00"
@.str.6 = [14 x i8] c"linux-x86.bin\00"
@.str.7 = [11 x i8] c"netbsd-x86\00"
@.str.8 = [15 x i8] c"netbsd-x86.bin\00"
@.str.9 = [21 x i8] c"Unknown platform %d\0A\00"
@stderr = global i32 0
@.str.10 = [17 x i8] c"Cant open %s: %s\00"
@errno = global i32 0
@.str.11 = [27 x i8] c"%s %s payload is %u bytes\0A\00"
@.str.12 = [6 x i8] c"write\00"
@O_RDONLY = global i32 0
@.str.13 = [9 x i8] c"sendfile\00"
@.str.14 = [12 x i8] c"%s Closing\0A\00"
@stdout = global i32 0
@str = [17 x i8] c"whoa, non inet4?\00"
@str.15 = [6 x i8] c"what?\00"
define void @do_child(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i32
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%10 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%11 = bitcast i32* %4 to i8*
%12 = bitcast %struct.sockaddr_in* %5 to i8*
%13 = bitcast %struct.stat* %6 to i8*
%14 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%15 = bitcast i32* %8 to i8*
store i32 16, i32* %8
%16 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%17 = call i64 @getpeername(i32 %0, %struct.sockaddr* %16, i32* %8)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %82
21: ; preds = %1
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @AF_INET
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %36
26: ; preds = %21
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i8* @inet_ntoa(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @ntohs(i32 %31)
%33 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %32)
%34 = call i64 @read(i32 %0, i32* %3, i32 4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %38, label %41
36: ; preds = %21
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %82
38: ; preds = %26
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @close(i32 %0)
br label %82
41: ; preds = %26
%42 = load i32, i32* %3
switch i32 %42, label %46 [
i32 0, label %43
i32 1, label %48
i32 2, label %45
]
43: ; preds = %41
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.15, i64 0, i64 0))
br label %48
45: ; preds = %41
br label %48
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %42)
br label %82
48: ; preds = %41, %45, %43
%49 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), %45 ], [ null, %43 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), %41 ]
%50 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), %45 ], [ null, %43 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %41 ]
%51 = call i64 @stat(i8* %49, %struct.stat* %6)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %49, i8* %56)
br label %82
58: ; preds = %48
%59 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i32
store i32 %61, i32* %4
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i8* %9, i8* %50, i32 %61)
%63 = call i64 @write(i32 %0, i32* %4, i32 4)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %58
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
br label %82
67: ; preds = %58
%68 = load i32, i32* @O_RDONLY
%69 = call i32 @open(i8* %49, i32 %68)
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %67
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %49, i8* %74)
br label %82
76: ; preds = %67
store i64 0, i64* %7
%77 = load i64, i64* %59
%78 = call i64 @sendfile(i32 %0, i32 %69, i64* %7, i64 %77)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %76
%81 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0))
br label %82
82: ; preds = %76, %80, %71, %65, %53, %46, %38, %36, %19
%83 = phi i32 [ 0, %19 ], [ 0, %38 ], [ 0, %46 ], [ 0, %53 ], [ 0, %65 ], [ %69, %71 ], [ %69, %80 ], [ %69, %76 ], [ 0, %36 ]
%84 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.14, i64 0, i64 0), i8* %9)
%85 = load i32, i32* @stdout
%86 = call i32 @fflush(i32 %85)
%87 = icmp eq i32 %83, 0
br i1 %87, label %90, label %88
88: ; preds = %82
%89 = call i32 @close(i32 %83)
br label %90
90: ; preds = %88, %82
%91 = icmp eq i32 %0, 0
br i1 %91, label %94, label %92
92: ; preds = %90
%93 = call i32 @close(i32 %0)
br label %94
94: ; preds = %92, %90
ret void
}