#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 */
typedef int /*<<< orphan*/ data_file;
typedef int /*<<< orphan*/ clone_num;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char const *);
scalar_t__ write(int, char const *, int);
int main(int argc, char **argv) {
  int clone_fd, data_fd, size;
  char data_file[256], buffer[1024], clone_num[64] = {0};
  const char *ctl_request = "connect 74.125.235.240!80",
             *http_request = "GET /index.html HTTP/1.1\r\n"
                             "Host: www.google.co.jp\r\n"
                             "Connection: close\r\n\r\n";
  clone_fd = open("/net/tcp/clone", O_RDWR);
  if (clone_fd < 0)
    die("clone: open");
  if ((size = read(clone_fd, clone_num, sizeof(clone_num))) < 0)
    die("clone: read");
  if (write(clone_fd, ctl_request, strlen(ctl_request)) < 0)
    die("clone: write");
  clone_num[size - 1] = '\0';
  snprintf(data_file, sizeof(data_file), "/net/tcp/%s/data", clone_num);
  data_fd = open(data_file, O_RDWR);
  if (data_fd < 0)
    die("data: open");
  if (write(data_fd, http_request, strlen(http_request)) < 0)
    die("data: write");
  do {
    size = read(data_fd, buffer, sizeof(buffer));
    if (size < 0)
      die("data: read");
    write(STDOUT_FILENO, buffer, size);
  } while (size != 0);
  close(data_fd);
  close(clone_fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x590,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x560(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
lea    0xe4d(%rip),%rax
mov    %rax,-0x568(%rbp)
lea    0xe59(%rip),%rax
mov    %rax,-0x570(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f6 <main+0x76>
lea    0xe80(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
lea    -0x560(%rbp),%rsi
mov    $0x40,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1222 <main+0xa2>
lea    0xe60(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x57c(%rbp)
mov    -0x568(%rbp),%rax
mov    %rax,-0x578(%rbp)
mov    -0x568(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x57c(%rbp),%edi
mov    -0x578(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    126f <main+0xef>
lea    0xe1f(%rip),%rdi
call   10a0 <die@plt>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x560(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
lea    -0x560(%rbp),%rcx
mov    $0x100,%esi
lea    0xdfd(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12cc <main+0x14c>
lea    0xde0(%rip),%rdi
call   10a0 <die@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x58c(%rbp)
mov    -0x570(%rbp),%rax
mov    %rax,-0x588(%rbp)
mov    -0x570(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58c(%rbp),%edi
mov    -0x588(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1319 <main+0x199>
lea    0xd9e(%rip),%rdi
call   10a0 <die@plt>
jmp    131e <main+0x19e>
mov    -0x18(%rbp),%edi
lea    -0x520(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    134b <main+0x1cb>
lea    0xd78(%rip),%rdi
call   10a0 <die@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
lea    -0x520(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    131e <main+0x19e>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2c4c(%rip),%rax
mov    (%rax),%eax
add    $0x590,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x548,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e42(%rip),%r12
mov    (%r12),%esi
lea    0xeb9(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11c1 <main+0x51>
lea    0xeb5(%rip),%rdi
call   1090 <die@plt>
mov    %rsp,%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e6 <main+0x76>
lea    0xe9c(%rip),%rdi
call   1090 <die@plt>
lea    0xe13(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1213 <main+0xa3>
lea    0xe7b(%rip),%rdi
call   1090 <die@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r14,1)
lea    0xe73(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1257 <main+0xe7>
lea    0xe55(%rip),%rdi
call   1090 <die@plt>
lea    0xdbc(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1284 <main+0x114>
lea    0xe33(%rip),%rdi
call   1090 <die@plt>
lea    0x140(%rsp),%rbx
lea    0xe2b(%rip),%r14
mov    0x2d56(%rip),%r13
jmp    12b2 <main+0x142>
nopl   0x0(%rax)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %ebp,%ebp
je     12d2 <main+0x162>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12a0 <main+0x130>
mov    %r14,%rdi
call   1090 <die@plt>
jmp    12a0 <main+0x130>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
add    $0x548,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"connect 74.125.235.240!80\00"
@.str.1 = [72 x i8] c"GET /index.html HTTP/1.1\0D\0AHost: www.google.co.jp\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.2 = [15 x i8] c"/net/tcp/clone\00"
@O_RDWR = global i32 0
@.str.3 = [12 x i8] c"clone: open\00"
@.str.4 = [12 x i8] c"clone: read\00"
@.str.5 = [13 x i8] c"clone: write\00"
@.str.6 = [17 x i8] c"/net/tcp/%s/data\00"
@.str.7 = [11 x i8] c"data: open\00"
@.str.8 = [12 x i8] c"data: write\00"
@.str.9 = [11 x i8] c"data: read\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca [1024 x i8]
%11 = alloca [64 x i8]
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = bitcast [64 x i8]* %11 to i8*
store i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8** %12
store i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8** %13
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i32, i32* %6
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 64)
store i32 %24, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %6
%30 = load i8*, i8** %12
%31 = load i8*, i8** %12
%32 = call i32 @strlen(i8* %31)
%33 = call i64 @write(i32 %29, i8* %30, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %28
%38 = load i32, i32* %8
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%44 = call i32 @snprintf(i8* %42, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* %43)
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* @O_RDWR
%47 = call i32 @open(i8* %45, i32 %46)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %37
%51 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %52
52: ; preds = %50, %37
%53 = load i32, i32* %7
%54 = load i8*, i8** %13
%55 = load i8*, i8** %13
%56 = call i32 @strlen(i8* %55)
%57 = call i64 @write(i32 %53, i8* %54, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %52
%60 = call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %61
61: ; preds = %59, %52
br label %62
62: ; preds = %75, %61
%63 = load i32, i32* %7
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%65 = call i32 @read(i32 %63, i8* %64, i32 1024)
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
br label %70
70: ; preds = %68, %62
%71 = load i32, i32* @STDOUT_FILENO
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%73 = load i32, i32* %8
%74 = call i64 @write(i32 %71, i8* %72, i32 %73)
br label %75
75: ; preds = %70
%76 = load i32, i32* %8
%77 = icmp ne i32 %76, 0
br i1 %77, label %62, label %78
78: ; preds = %75
%79 = load i32, i32* %7
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* %6
%82 = call i32 @close(i32 %81)
%83 = load i32, i32* @EXIT_SUCCESS
ret i32 %83
}

LLVM IR (-O2):
@.str = [26 x i8] c"connect 74.125.235.240!80\00"
@.str.1 = [72 x i8] c"GET /index.html HTTP/1.1\0D\0AHost: www.google.co.jp\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.2 = [15 x i8] c"/net/tcp/clone\00"
@O_RDWR = global i32 0
@.str.3 = [12 x i8] c"clone: open\00"
@.str.4 = [12 x i8] c"clone: read\00"
@.str.5 = [13 x i8] c"clone: write\00"
@.str.6 = [17 x i8] c"/net/tcp/%s/data\00"
@.str.7 = [11 x i8] c"data: open\00"
@.str.8 = [12 x i8] c"data: write\00"
@.str.9 = [11 x i8] c"data: read\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32 @read(i32 %10, i8* %8, i32 64)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = tail call i32 @strlen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = tail call i64 @write(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = add nsw i32 %15, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %6, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %35
35: ; preds = %33, %25
%36 = call i32 @strlen(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0))
%37 = call i64 @write(i32 %31, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
br label %42
42: ; preds = %41, %47
%43 = call i32 @read(i32 %31, i8* %7, i32 1024)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i64 @write(i32 %48, i8* %7, i32 %43)
%50 = icmp eq i32 %43, 0
br i1 %50, label %51, label %42
51: ; preds = %47
%52 = call i32 @close(i32 %31)
%53 = call i32 @close(i32 %10)
%54 = load i32, i32* @EXIT_SUCCESS
ret i32 %54
}