#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ kclose(int);
scalar_t__ kconnect(int, struct sockaddr *, int);
int kread(int, char *, int);
int ksocket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ kwrite(int, char *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_usage(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
char *strdup(char *);
int strlen(char *);
int main(int argc, char *argv[]) {
  int sd, len, file_len, total;
  char *progname = argv[0];
  char *opt, *p;
  struct sockaddr_in sin;
  char snd_buf[8192], rcv_buf[8192];
  FILE *fp;
  if (argc != 3) {
    print_usage(progname);
    exit(1);
  }
  opt = strdup(argv[1]);
  p = strchr(opt, ':');
  if (p == NULL) {
    print_usage(progname);
    exit(1);
  }
  *p = '\0';
  fp = fopen(argv[2], "rb");
  if (fp == NULL) {
    perror("Fail: file open");
    exit(1);
  }
  memset(&sin, 0x00, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(atoi(p + 1));
  if (!inet_aton(opt, &sin.sin_addr)) {
    print_usage(progname);
    exit(1);
  }
  if ((sd = ksocket(AF_INET, SOCK_STREAM, 0)) <= 0) {
    perror("ksocket");
    exit(1);
  }
  if (kconnect(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("kconnect");
    exit(1);
  }
  printf("connected to file server which runs on KENS\n");
  total = 0;
  fseek(fp, 0, SEEK_END);
  file_len = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  sprintf(snd_buf, "%d", file_len);
  kwrite(sd, snd_buf, strlen(snd_buf));
  sleep(1);
  while ((len = fread(snd_buf, 1, 8192, fp)) > 0) {
    total += len;
    fprintf(stderr, "%d bytes transferred.\n", total);
    kwrite(sd, snd_buf, len);
  }
  fprintf(stderr, "transferring complete.\n");
  while ((len = kread(sd, rcv_buf, 8192)) > 0)
    ;
  sleep(1);
  fprintf(stderr, "connection closed.\n");
  kclose(sd);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4070,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     12c1 <main+0x41>
mov    -0x28(%rbp),%rdi
call   1170 <print_usage@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1190 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1302 <main+0x82>
mov    -0x28(%rbp),%rdi
call   1170 <print_usage@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xce8(%rip),%rsi
call   1120 <fopen@plt>
mov    %rax,-0x4058(%rbp)
cmpq   $0x0,-0x4058(%rbp)
jne    1348 <main+0xc8>
lea    0xcca(%rip),%rdi
call   1130 <perror@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1140 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10f0 <inet_aton@plt>
cmp    $0x0,%eax
jne    13a4 <main+0x124>
mov    -0x28(%rbp),%rdi
call   1170 <print_usage@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
mov    0x2c35(%rip),%rax
mov    (%rax),%edi
mov    0x2c14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <ksocket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     13df <main+0x15f>
lea    0xc43(%rip),%rdi
call   1130 <perror@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <kconnect@plt>
cmp    $0x0,%rax
jge    1410 <main+0x190>
lea    0xc1a(%rip),%rdi
call   1130 <perror@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
lea    0xc0d(%rip),%rdi
call   1080 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x4058(%rbp),%rdi
mov    0x2bb7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1100 <fseek@plt>
mov    -0x4058(%rbp),%rdi
call   10c0 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4058(%rbp),%rdi
mov    0x2b69(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1100 <fseek@plt>
lea    -0x2050(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xbe0(%rip),%rsi
call   1150 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4064(%rbp)
lea    -0x2050(%rbp),%rax
mov    %rax,-0x4060(%rbp)
lea    -0x2050(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4064(%rbp),%edi
mov    -0x4060(%rbp),%rsi
mov    %eax,%edx
call   10d0 <kwrite@plt>
mov    $0x1,%edi
call   11a0 <sleep@plt>
lea    -0x2050(%rbp),%rdi
mov    -0x4058(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    151a <main+0x29a>
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ae0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xb58(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x2050(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   10d0 <kwrite@plt>
jmp    14b7 <main+0x237>
mov    0x2aaf(%rip),%rax
mov    (%rax),%edi
lea    0xb41(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x4050(%rbp),%rsi
mov    $0x2000,%edx
call   1110 <kread@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1556 <main+0x2d6>
jmp    1531 <main+0x2b1>
mov    $0x1,%edi
call   11a0 <sleep@plt>
mov    0x2a69(%rip),%rax
mov    (%rax),%edi
lea    0xb13(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   10a0 <kclose@plt>
mov    -0x4058(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x4070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4018,%rsp
mov    (%rsi),%r14
cmp    $0x3,%edi
jne    14c3 <main+0x243>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1190 <strdup@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     14c3 <main+0x243>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    0x10(%rbp),%rdi
lea    0xd2d(%rip),%rsi
call   1120 <fopen@plt>
test   %rax,%rax
je     14d5 <main+0x255>
mov    %rax,%r13
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2ce3(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
add    $0x1,%rbx
mov    %rbx,%rdi
call   1140 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
mov    %rbp,%rsi
call   10f0 <inet_aton@plt>
test   %eax,%eax
je     14c3 <main+0x243>
mov    (%r15),%edi
mov    0x2c90(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <ksocket@plt>
test   %eax,%eax
jle    14de <main+0x25e>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10e0 <kconnect@plt>
test   %rax,%rax
js     14e7 <main+0x267>
lea    0xcff(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c71(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1100 <fseek@plt>
mov    %r13,%rdi
call   10c0 <ftell@plt>
mov    %eax,%ebx
mov    0x2c2c(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1100 <fseek@plt>
lea    0xc7d(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   1150 <sprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   10d0 <kwrite@plt>
mov    $0x1,%edi
call   11a0 <sleep@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
test   %eax,%eax
mov    %r13,%r15
jle    144f <main+0x1cf>
mov    %eax,%ebp
lea    0xc2d(%rip),%r14
lea    0x10(%rsp),%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %ebp,%ebx
mov    0x2bb7(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   10d0 <kwrite@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1410 <main+0x190>
mov    0x2b7a(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xbde(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
lea    0x2010(%rsp),%rbx
nop
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1110 <kread@plt>
test   %eax,%eax
jg     1470 <main+0x1f0>
mov    $0x1,%edi
call   11a0 <sleep@plt>
mov    0x0(%rbp),%edi
lea    0xbbe(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %r12d,%edi
call   10a0 <kclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x4018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1170 <print_usage@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
lea    0xb27(%rip),%rdi
jmp    14ee <main+0x26e>
lea    0xb2e(%rip),%rdi
jmp    14ee <main+0x26e>
lea    0xb2d(%rip),%rdi
call   1130 <perror@plt>
mov    $0x1,%edi
call   1160 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"Fail: file open\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [8 x i8] c"ksocket\00"
@.str.3 = [9 x i8] c"kconnect\00"
@.str.4 = [45 x i8] c"connected to file server which runs on KENS\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.6 = [23 x i8] c"%d bytes transferred.\0A\00"
@.str.7 = [24 x i8] c"transferring complete.\0A\00"
@.str.8 = [20 x i8] c"connection closed.\0A\00"
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 i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.sockaddr_in
%14 = alloca [8192 x i8]
%15 = alloca [8192 x i8]
%16 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
store i8* %19, i8** %10
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 3
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i8*, i8** %10
%24 = call i32 @print_usage(i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i8* @strdup(i8* %29)
store i8* %30, i8** %11
%31 = load i8*, i8** %11
%32 = call i8* @strchr(i8* %31, i8 signext 58)
store i8* %32, i8** %12
%33 = load i8*, i8** %12
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %26
%36 = load i8*, i8** %10
%37 = call i32 @print_usage(i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %26
%40 = load i8*, i8** %12
store i8 0, i8* %40
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %44, i32** %16
%45 = load i32*, i32** %16
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %39
%51 = call i32 @memset(%struct.sockaddr_in* %13, i32 0, i32 12)
%52 = load i32, i32* @AF_INET
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %52, i32* %53
%54 = load i8*, i8** %12
%55 = getelementptr inbounds i8, i8* %54, i64 1
%56 = call i32 @atoi(i8* %55)
%57 = call i32 @htons(i32 %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %57, i32* %58
%59 = load i8*, i8** %11
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%61 = call i32 @inet_aton(i8* %59, i32* %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %67, label %63
63: ; preds = %50
%64 = load i8*, i8** %10
%65 = call i32 @print_usage(i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %50
%68 = load i32, i32* @AF_INET
%69 = load i32, i32* @SOCK_STREAM
%70 = call i32 @ksocket(i32 %68, i32 %69, i32 0)
store i32 %70, i32* %6
%71 = icmp sle i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %67
%76 = load i32, i32* %6
%77 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%78 = call i64 @kconnect(i32 %76, %struct.sockaddr* %77, i32 12)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %75
%84 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %9
%85 = load i32*, i32** %16
%86 = load i32, i32* @SEEK_END
%87 = call i32 @fseek(i32* %85, i32 0, i32 %86)
%88 = load i32*, i32** %16
%89 = call i32 @ftell(i32* %88)
store i32 %89, i32* %8
%90 = load i32*, i32** %16
%91 = load i32, i32* @SEEK_SET
%92 = call i32 @fseek(i32* %90, i32 0, i32 %91)
%93 = getelementptr inbounds [8192 x i8], [8192 x i8]* %14, i64 0, i64 0
%94 = load i32, i32* %8
%95 = call i32 @sprintf(i8* %93, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %94)
%96 = load i32, i32* %6
%97 = getelementptr inbounds [8192 x i8], [8192 x i8]* %14, i64 0, i64 0
%98 = getelementptr inbounds [8192 x i8], [8192 x i8]* %14, i64 0, i64 0
%99 = call i32 @strlen(i8* %98)
%100 = call i32 @kwrite(i32 %96, i8* %97, i32 %99)
%101 = call i32 @sleep(i32 1)
br label %102
102: ; preds = %107, %83
%103 = getelementptr inbounds [8192 x i8], [8192 x i8]* %14, i64 0, i64 0
%104 = load i32*, i32** %16
%105 = call i32 @fread(i8* %103, i32 1, i32 8192, i32* %104)
store i32 %105, i32* %7
%106 = icmp sgt i32 %105, 0
br i1 %106, label %107, label %118
107: ; preds = %102
%108 = load i32, i32* %7
%109 = load i32, i32* %9
%110 = add nsw i32 %109, %108
store i32 %110, i32* %9
%111 = load i32, i32* @stderr
%112 = load i32, i32* %9
%113 = call i32 (i32, i8*, ...) @fprintf(i32 %111, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %112)
%114 = load i32, i32* %6
%115 = getelementptr inbounds [8192 x i8], [8192 x i8]* %14, i64 0, i64 0
%116 = load i32, i32* %7
%117 = call i32 @kwrite(i32 %114, i8* %115, i32 %116)
br label %102
118: ; preds = %102
%119 = load i32, i32* @stderr
%120 = call i32 (i32, i8*, ...) @fprintf(i32 %119, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %121
121: ; preds = %126, %118
%122 = load i32, i32* %6
%123 = getelementptr inbounds [8192 x i8], [8192 x i8]* %15, i64 0, i64 0
%124 = call i32 @kread(i32 %122, i8* %123, i32 8192)
store i32 %124, i32* %7
%125 = icmp sgt i32 %124, 0
br i1 %125, label %126, label %127
126: ; preds = %121
br label %121
127: ; preds = %121
%128 = call i32 @sleep(i32 1)
%129 = load i32, i32* @stderr
%130 = call i32 (i32, i8*, ...) @fprintf(i32 %129, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%131 = load i32, i32* %6
%132 = call i32 @kclose(i32 %131)
%133 = load i32*, i32** %16
%134 = call i32 @fclose(i32* %133)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"Fail: file open\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [8 x i8] c"ksocket\00"
@.str.3 = [9 x i8] c"kconnect\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.6 = [23 x i8] c"%d bytes transferred.\0A\00"
@.str.7 = [24 x i8] c"transferring complete.\0A\00"
@.str.8 = [20 x i8] c"connection closed.\0A\00"
@str = [44 x i8] c"connected to file server which runs on KENS\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [8192 x i8]
%5 = alloca [8192 x i8]
%6 = load i8*, i8** %1
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%10 = icmp eq i32 %0, 3
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = tail call i32 @print_usage(i8* %6)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i8* @strdup(i8* %16)
%18 = tail call i8* @strchr(i8* dereferenceable(1) %17, i8 signext 58)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = tail call i32 @print_usage(i8* %6)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %14
store i8 0, i8* %18
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %33, i32* %34
%35 = getelementptr inbounds i8, i8* %18, i64 1
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%40 = call i32 @inet_aton(i8* %17, i32* %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %31
%43 = call i32 @print_usage(i8* %6)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %31
%46 = load i32, i32* @AF_INET
%47 = load i32, i32* @SOCK_STREAM
%48 = call i32 @ksocket(i32 %46, i32 %47, i32 0)
%49 = icmp slt i32 %48, 1
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %45
%54 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%55 = call i64 @kconnect(i32 %48, %struct.sockaddr* %54, i32 12)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %53
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%62 = load i32, i32* @SEEK_END
%63 = call i32 @fseek(i32* %26, i32 0, i32 %62)
%64 = call i32 @ftell(i32* %26)
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @fseek(i32* %26, i32 0, i32 %65)
%67 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %64)
%68 = call i32 @strlen(i8* %8)
%69 = call i32 @kwrite(i32 %48, i8* %8, i32 %68)
%70 = call i32 @sleep(i32 1)
%71 = call i32 @fread(i8* %8, i32 1, i32 8192, i32* %26)
%72 = icmp sgt i32 %71, 0
br i1 %72, label %73, label %82
73: ; preds = %60, %73
%74 = phi i32 [ %80, %73 ], [ %71, %60 ]
%75 = phi i32 [ %76, %73 ], [ 0, %60 ]
%76 = add nsw i32 %74, %75
%77 = load i32, i32* @stderr
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %76)
%79 = call i32 @kwrite(i32 %48, i8* %8, i32 %74)
%80 = call i32 @fread(i8* %8, i32 1, i32 8192, i32* %26)
%81 = icmp sgt i32 %80, 0
br i1 %81, label %73, label %82
82: ; preds = %73, %60
%83 = load i32, i32* @stderr
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %85
85: ; preds = %85, %82
%86 = call i32 @kread(i32 %48, i8* %9, i32 8192)
%87 = icmp sgt i32 %86, 0
br i1 %87, label %85, label %88
88: ; preds = %85
%89 = call i32 @sleep(i32 1)
%90 = load i32, i32* @stderr
%91 = call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%92 = call i32 @kclose(i32 %48)
%93 = call i32 @fclose(i32* %26)
ret i32 0
}