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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ kclose(int);
scalar_t__ kconnect(int, struct sockaddr *, int);
int kreadline(int, char *, int);
int ksocket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int kwrite(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_usage(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
char *strdup(char *);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  int sd, len;
  char *progname = argv[0];
  char *opt, *p;
  struct sockaddr_in sin;
  char snd_buf[8192], rcv_buf[8192];
  if (argc != 2) {
    print_usage(progname);
    exit(1);
  }
  opt = strdup(argv[1]);
  p = strchr(opt, ':');
  if (p == NULL) {
    print_usage(progname);
    exit(1);
  }
  *p = '\0';
  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 echo server which runs on KENS\n");
  while (fgets(snd_buf, 8192, stdin) != NULL) {
    len = kwrite(sd, snd_buf, strlen(snd_buf));
    len = kreadline(sd, rcv_buf, sizeof(rcv_buf));
    if (len > 0)
      printf("[%s:%d] %s", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port),
             rcv_buf);
    else {
      printf("connection closed by foriegn host\n");
      break;
    }
  }
  kclose(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4060,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1271 <main+0x41>
mov    -0x20(%rbp),%rdi
call   1130 <print_usage@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1150 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b2 <main+0x82>
mov    -0x20(%rbp),%rdi
call   1130 <print_usage@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1110 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
add    $0x4,%rsi
call   10e0 <inet_aton@plt>
cmp    $0x0,%eax
jne    1319 <main+0xe9>
mov    -0x20(%rbp),%rdi
call   1130 <print_usage@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
mov    0x2ca7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <ksocket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     1354 <main+0x124>
lea    0xcbb(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <kconnect@plt>
cmp    $0x0,%rax
jge    1385 <main+0x155>
lea    0xc92(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    0xc85(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x2040(%rbp),%rdi
mov    0x2c3f(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     1464 <main+0x234>
mov    -0x14(%rbp),%eax
mov    %eax,-0x404c(%rbp)
lea    -0x2040(%rbp),%rax
mov    %rax,-0x4048(%rbp)
lea    -0x2040(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x404c(%rbp),%edi
mov    -0x4048(%rbp),%rsi
mov    %eax,%edx
call   10c0 <kwrite@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x4040(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <kreadline@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    144c <main+0x21c>
mov    -0x3c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x4058(%rbp)
mov    -0x40(%rbp),%edi
call   10f0 <ntohs@plt>
mov    -0x4058(%rbp),%rsi
mov    %eax,%edx
lea    -0x4040(%rbp),%rcx
lea    0xbfe(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    145f <main+0x22f>
lea    0xbf6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1464 <main+0x234>
jmp    1393 <main+0x163>
mov    -0x14(%rbp),%edi
call   10b0 <kclose@plt>
xor    %eax,%eax
add    $0x4060,%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    $0x2,%edi
jne    13dc <main+0x19c>
mov    0x8(%rsi),%rdi
call   1160 <strdup@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     13dc <main+0x19c>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2d4d(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
add    $0x1,%rbx
mov    %rbx,%rdi
call   1120 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,(%rsp)
lea    0x4(%rsp),%rsi
mov    %r15,%rdi
call   10f0 <inet_aton@plt>
test   %eax,%eax
je     13dc <main+0x19c>
mov    0x0(%rbp),%edi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <ksocket@plt>
test   %eax,%eax
jle    13ee <main+0x1ae>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10e0 <kconnect@plt>
test   %rax,%rax
js     13f7 <main+0x1b7>
lea    0xd13(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ccb(%rip),%r13
mov    0x0(%r13),%edx
lea    0x10(%rsp),%rdi
mov    $0x2000,%esi
call   10b0 <fgets@plt>
test   %rax,%rax
je     13c1 <main+0x181>
lea    0x10(%rsp),%r14
lea    0x2010(%rsp),%r15
lea    0xccc(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   10d0 <kwrite@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x2000,%edx
call   1040 <kreadline@plt>
test   %eax,%eax
jle    13b5 <main+0x175>
mov    0x4(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%rsp),%edi
call   1100 <ntohs@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    $0x2000,%esi
call   10b0 <fgets@plt>
test   %rax,%rax
jne    1350 <main+0x110>
jmp    13c1 <main+0x181>
lea    0xc8c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   10c0 <kclose@plt>
xor    %eax,%eax
add    $0x4018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1140 <print_usage@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
lea    0xc0b(%rip),%rdi
jmp    13fe <main+0x1be>
lea    0xc0a(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"ksocket\00"
@.str.1 = [9 x i8] c"kconnect\00"
@.str.2 = [45 x i8] c"connected to echo server which runs on KENS\0A\00"
@stdin = global i32 0
@.str.3 = [11 x i8] c"[%s:%d] %s\00"
@.str.4 = [35 x i8] c"connection closed by foriegn host\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 i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.sockaddr_in
%12 = alloca [8192 x i8]
%13 = alloca [8192 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
store i8* %16, i8** %8
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i8*, i8** %8
%21 = call i32 @print_usage(i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i8* @strdup(i8* %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = call i8* @strchr(i8* %28, i8 signext 58)
store i8* %29, i8** %10
%30 = load i8*, i8** %10
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %23
%33 = load i8*, i8** %8
%34 = call i32 @print_usage(i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %23
%37 = load i8*, i8** %10
store i8 0, i8* %37
%38 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %39, i32* %40
%41 = load i8*, i8** %10
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = call i32 @atoi(i8* %42)
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i8*, i8** %9
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%48 = call i32 @inet_aton(i8* %46, i32* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %54, label %50
50: ; preds = %36
%51 = load i8*, i8** %8
%52 = call i32 @print_usage(i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %36
%55 = load i32, i32* @AF_INET
%56 = load i32, i32* @SOCK_STREAM
%57 = call i32 @ksocket(i32 %55, i32 %56, i32 0)
store i32 %57, i32* %6
%58 = icmp sle i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %54
%63 = load i32, i32* %6
%64 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%65 = call i64 @kconnect(i32 %63, %struct.sockaddr* %64, i32 12)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %62
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %99, %70
%73 = getelementptr inbounds [8192 x i8], [8192 x i8]* %12, i64 0, i64 0
%74 = load i32, i32* @stdin
%75 = call i32* @fgets(i8* %73, i32 8192, i32 %74)
%76 = icmp ne i32* %75, null
br i1 %76, label %77, label %100
77: ; preds = %72
%78 = load i32, i32* %6
%79 = getelementptr inbounds [8192 x i8], [8192 x i8]* %12, i64 0, i64 0
%80 = getelementptr inbounds [8192 x i8], [8192 x i8]* %12, i64 0, i64 0
%81 = call i32 @strlen(i8* %80)
%82 = call i32 @kwrite(i32 %78, i8* %79, i32 %81)
store i32 %82, i32* %7
%83 = load i32, i32* %6
%84 = getelementptr inbounds [8192 x i8], [8192 x i8]* %13, i64 0, i64 0
%85 = call i32 @kreadline(i32 %83, i8* %84, i32 8192)
store i32 %85, i32* %7
%86 = load i32, i32* %7
%87 = icmp sgt i32 %86, 0
br i1 %87, label %88, label %97
88: ; preds = %77
%89 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%90 = load i32, i32* %89
%91 = call i8* @inet_ntoa(i32 %90)
%92 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%93 = load i32, i32* %92
%94 = call i32 @ntohs(i32 %93)
%95 = getelementptr inbounds [8192 x i8], [8192 x i8]* %13, i64 0, i64 0
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %91, i32 %94, i8* %95)
br label %99
97: ; preds = %77
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %100
99: ; preds = %88
br label %72
100: ; preds = %97, %72
%101 = load i32, i32* %6
%102 = call i32 @kclose(i32 %101)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"ksocket\00"
@.str.1 = [9 x i8] c"kconnect\00"
@stdin = global i32 0
@.str.3 = [11 x i8] c"[%s:%d] %s\00"
@str = [44 x i8] c"connected to echo server which runs on KENS\00"
@str.5 = [34 x i8] c"connection closed by foriegn host\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, 2
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 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds i8, i8* %18, i64 1
%28 = call i32 @atoi(i8* %27)
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%32 = call i32 @inet_aton(i8* %17, i32* %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %23
%35 = call i32 @print_usage(i8* %6)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %23
%38 = load i32, i32* @AF_INET
%39 = load i32, i32* @SOCK_STREAM
%40 = call i32 @ksocket(i32 %38, i32 %39, i32 0)
%41 = icmp slt i32 %40, 1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %37
%46 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%47 = call i64 @kconnect(i32 %40, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %45
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%54 = load i32, i32* @stdin
%55 = call i32* @fgets(i8* %8, i32 8192, i32 %54)
%56 = icmp eq i32* %55, null
br i1 %56, label %73, label %57
57: ; preds = %52, %62
%58 = call i32 @strlen(i8* %8)
%59 = call i32 @kwrite(i32 %40, i8* %8, i32 %58)
%60 = call i32 @kreadline(i32 %40, i8* %9, i32 8192)
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %57
%63 = load i32, i32* %31
%64 = call i8* @inet_ntoa(i32 %63)
%65 = load i32, i32* %30
%66 = call i32 @ntohs(i32 %65)
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %64, i32 %66, i8* %9)
%68 = load i32, i32* @stdin
%69 = call i32* @fgets(i8* %8, i32 8192, i32 %68)
%70 = icmp eq i32* %69, null
br i1 %70, label %73, label %57
71: ; preds = %57
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
br label %73
73: ; preds = %62, %52, %71
%74 = call i32 @kclose(i32 %40)
ret i32 0
}