#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock_serv;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error_handling(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read_routine(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write_routine(int, char *);
int main(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in sock_serv;
  pid_t pid;
  char buf[30];
  if (argc != 3) {
    printf("Usage: %s <IP> <Port> \n", argv[0]);
    exit(1);
  }
  sock = socket(PF_INET, SOCK_STREAM, 0);
  memset(&sock_serv, 0, sizeof(sock_serv));
  sock_serv.sin_family = AF_INET;
  sock_serv.sin_addr.s_addr = inet_addr(argv[1]);
  sock_serv.sin_port = htons(atoi(argv[2]));
  if (connect(sock, (struct sockaddr *)&sock_serv, sizeof(sock_serv)) == -1)
    error_handling("connect() error");
  pid = fork();
  if (pid == 0) {
    write_routine(sock, buf);
  } else {
    read_routine(sock, buf);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     120d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1292 <main+0xc2>
lea    0xd8b(%rip),%rdi
call   1090 <error_handling@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b9 <main+0xe9>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   10a0 <write_routine@plt>
jmp    12c5 <main+0xf5>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1070 <read_routine@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    128f <main+0xbf>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x10(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0x8e>
lea    0xdbf(%rip),%rdi
call   1090 <error_handling@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
test   %rax,%rax
je     1278 <main+0xa8>
call   1070 <read_routine@plt>
jmp    127d <main+0xad>
call   10a0 <write_routine@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd67(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage: %s <IP> <Port> \0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str.1 = [16 x i8] c"connect() error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i64
%9 = alloca [30 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %18
%42 = call i32 @error_handling(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %18
%44 = call i64 (...) @fork()
store i64 %44, i64* %8
%45 = load i64, i64* %8
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i32, i32* %6
%49 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%50 = call i32 @write_routine(i32 %48, i8* %49)
br label %55
51: ; preds = %43
%52 = load i32, i32* %6
%53 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%54 = call i32 @read_routine(i32 %52, i8* %53)
br label %55
55: ; preds = %51, %47
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage: %s <IP> <Port> \0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str.1 = [16 x i8] c"connect() error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [30 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @inet_addr(i8* %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @connect(i32 %15, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %12
%32 = call i32 @error_handling(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %12
%34 = call i64 (...) @fork()
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @write_routine(i32 %15, i8* %6)
br label %40
38: ; preds = %33
%39 = call i32 @read_routine(i32 %15, i8* %6)
br label %40
40: ; preds = %38, %36
%41 = call i32 @close(i32 %15)
ret i32 0
}