#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*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd;
  char buffer[4096 + 1];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <Server IP address>\neg. $ ./%s 127.0.0.1\n",
            argv[0], argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "socket() error");
    exit(EXIT_FAILURE);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(9669);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
    fprintf(stderr, "inet_pton(%s) error", argv[1]);
    exit(EXIT_FAILURE);
  }
  if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) <
      0) {
    perror("connect() error");
    exit(EXIT_FAILURE);
  }
  while ((fgets(buffer, 4096, stdin) != NULL)) {
    write(sockfd, buffer, strlen(buffer));
    if (read(sockfd, buffer, 4096) == 0) {
      perror("failed to read data from server");
      exit(EXIT_FAILURE);
    }
    if (fputs(buffer, stdout) == EOF) {
      perror("fputs() error");
      exit(EXIT_FAILURE);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1226 <main+0x56>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1270 <main+0xa0>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x1030(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    $0x25c5,%edi
call   1050 <htons@plt>
mov    %eax,-0x102c(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x1030(%rbp),%rdx
call   10a0 <inet_pton@plt>
cmp    $0x0,%rax
jg     12f4 <main+0x124>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd64(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    132c <main+0x15c>
lea    0xd3e(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1331 <main+0x161>
lea    -0x1020(%rbp),%rdi
mov    0x2ca1(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1408 <main+0x238>
mov    -0x14(%rbp),%eax
mov    %eax,-0x103c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1038(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jne    13c4 <main+0x1f4>
lea    0xcb6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2bde(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
mov    0x2bf8(%rip),%rcx
cmp    (%rcx),%rax
jne    1403 <main+0x233>
lea    0xc97(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2bbc(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1331 <main+0x161>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1020,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1303 <main+0x133>
mov    0x2df6(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1322 <main+0x152>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x25c5,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edi
mov    0x8(%r14),%rsi
mov    %r15,%rdx
call   10a0 <inet_pton@plt>
test   %rax,%rax
jle    133b <main+0x16b>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     1358 <main+0x188>
mov    0x2d78(%rip),%r14
lea    0x10(%rsp),%rbx
mov    0x2d3c(%rip),%r15
mov    0x2d5d(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     12d7 <main+0x107>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
je     12e9 <main+0x119>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1060 <fputs@plt>
cmp    (%r12),%rax
jne    1280 <main+0xb0>
lea    0xdb2(%rip),%rdi
jmp    12f0 <main+0x120>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd77(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rcx
lea    0xcea(%rip),%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12f5 <main+0x125>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12f5 <main+0x125>
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xcf4(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12f5 <main+0x125>
lea    0xcf8(%rip),%rdi
jmp    12f0 <main+0x120>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [52 x i8] c"usage: %s <Server IP address>\0Aeg. $ ./%s 127.0.0.1\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@.str.2 = [20 x i8] c"inet_pton(%s) error\00"
@.str.3 = [16 x i8] c"connect() error\00"
@stdin = global i32 0
@.str.4 = [32 x i8] c"failed to read data from server\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.5 = [14 x i8] c"fputs() error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4097 x i8]
%8 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %22
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
%33 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %34, i32* %35
%36 = call i32 @htons(i32 9669)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @AF_INET
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%43 = call i64 @inet_pton(i32 %38, i8* %41, i32* %42)
%44 = icmp sle i64 %43, 0
br i1 %44, label %45, label %53
45: ; preds = %32
%46 = load i32, i32* @stderr
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %32
%54 = load i32, i32* %6
%55 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%56 = call i64 @connect(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %53
br label %63
63: ; preds = %92, %62
%64 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%65 = load i32, i32* @stdin
%66 = call i32* @fgets(i8* %64, i32 4096, i32 %65)
%67 = icmp ne i32* %66, null
br i1 %67, label %68, label %93
68: ; preds = %63
%69 = load i32, i32* %6
%70 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%71 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%72 = call i32 @strlen(i8* %71)
%73 = call i32 @write(i32 %69, i8* %70, i32 %72)
%74 = load i32, i32* %6
%75 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%76 = call i64 @read(i32 %74, i8* %75, i32 4096)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %68
%79 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %68
%83 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%84 = load i32, i32* @stdout
%85 = call i64 @fputs(i8* %83, i32 %84)
%86 = load i64, i64* @EOF
%87 = icmp eq i64 %85, %86
br i1 %87, label %88, label %92
88: ; preds = %82
%89 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; preds = %82
br label %63
93: ; preds = %63
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [52 x i8] c"usage: %s <Server IP address>\0Aeg. $ ./%s 127.0.0.1\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@.str.2 = [20 x i8] c"inet_pton(%s) error\00"
@.str.3 = [16 x i8] c"connect() error\00"
@stdin = global i32 0
@.str.4 = [32 x i8] c"failed to read data from server\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.5 = [14 x i8] c"fputs() error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4097 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %26, i32* %27
%28 = tail call i32 @htons(i32 9669)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%34 = call i64 @inet_pton(i32 %30, i8* %32, i32* %33)
%35 = icmp slt i64 %34, 1
br i1 %35, label %36, label %42
36: ; preds = %24
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %31
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %24
%43 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%44 = call i64 @connect(i32 %17, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %42, %63
%51 = load i32, i32* @stdin
%52 = call i32* @fgets(i8* %5, i32 4096, i32 %51)
%53 = icmp eq i32* %52, null
br i1 %53, label %72, label %54
54: ; preds = %50
%55 = call i32 @strlen(i8* %5)
%56 = call i32 @write(i32 %17, i8* %5, i32 %55)
%57 = call i64 @read(i32 %17, i8* %5, i32 4096)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %54
%64 = load i32, i32* @stdout
%65 = call i64 @fputs(i8* %5, i32 %64)
%66 = load i64, i64* @EOF
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %50
68: ; preds = %63
%69 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %50
ret i32 0
}