#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_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ servadd;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, struct sockaddr *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int main(int ac, char *av[]) {
  struct sockaddr_in servadd;
  struct hostent *hp;
  int sock_id, sock_fd;
  char buffer[BUFSIZ];
  int n_read;
  if (ac != 3)
    exit(1);
  sock_id = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_id == -1) {
    perror("sosket");
    exit(1);
  };
  bzero(&servadd, sizeof(servadd));
  hp = gethostbyname(av[1]);
  if (hp == NULL) {
    perror(av[1]);
    exit(1);
  };
  bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
  servadd.sin_port = htons(15000);
  servadd.sin_family = AF_INET;
  if (connect(sock_id, (struct sockaddr *)&servadd, sizeof(servadd)) != 0) {
    perror("connect");
    exit(1);
  };
  if (write(sock_id, av[2], strlen(av[2])) == -1) {
    perror("write");
    exit(1);
  };
  if (write(sock_id, "\n", 1) == -1) {
    perror("write");
    exit(1);
  };
  while ((n_read = read(sock_id, buffer, BUFSIZ)) > 0) {
    if (write(1, buffer, n_read) == -1) {
      perror("write");
      exit(1);
    };
  }
  close(sock_id);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1216 <main+0x56>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1252 <main+0x92>
lea    0xdbd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1293 <main+0xd3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x8,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1090 <bcopy@plt>
mov    $0x3a98,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
je     12f7 <main+0x137>
lea    0xd1f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1343 <main+0x183>
lea    0xcdb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    0xcc8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1376 <main+0x1b6>
lea    0xca8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    137b <main+0x1bb>
mov    -0x50(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    0x2c47(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jle    13d1 <main+0x211>
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    13cc <main+0x20c>
lea    0xc52(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    137b <main+0x1bb>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x40(%rbp)
mov    0x2df4(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x3,%edi
jne    1302 <main+0x142>
mov    %rsi,%r15
mov    0x2de4(%rip),%r12
mov    (%r12),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     130c <main+0x14c>
mov    %eax,%r14d
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x8(%r15),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1315 <main+0x155>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    -0x30(%rbp),%rsi
call   1090 <bcopy@plt>
mov    $0x3a98,%edi
call   1050 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    (%r12),%eax
mov    %eax,-0x38(%rbp)
lea    -0x38(%rbp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jne    131b <main+0x15b>
mov    0x10(%r15),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f6 <main+0x136>
lea    0xd70(%rip),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
nopl   (%rax)
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f6 <main+0x136>
mov    0x0(%r13),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
test   %eax,%eax
jle    12d9 <main+0x119>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
jmp    12b0 <main+0xf0>
mov    %r14d,%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd12(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xced(%rip),%rdi
jmp    12fd <main+0x13d>
mov    0x8(%r15),%rdi
jmp    12fd <main+0x13d>
lea    0xce5(%rip),%rdi
jmp    12fd <main+0x13d>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"sosket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call %struct.hostent* @gethostbyname(i8* %34)
store %struct.hostent* %35, %struct.hostent** %7
%36 = load %struct.hostent*, %struct.hostent** %7
%37 = icmp eq %struct.hostent* %36, null
br i1 %37, label %38, label %44
38: ; preds = %30
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @perror(i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %30
%45 = load %struct.hostent*, %struct.hostent** %7
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%49 = bitcast i32* %48 to %struct.sockaddr*
%50 = load %struct.hostent*, %struct.hostent** %7
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @bcopy(i32 %47, %struct.sockaddr* %49, i32 %52)
%54 = call i32 @htons(i32 15000)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %54, i32* %55
%56 = load i32, i32* @AF_INET
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %56, i32* %57
%58 = load i32, i32* %8
%59 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%60 = call i64 @connect(i32 %58, %struct.sockaddr* %59, i32 12)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %44
%63 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %44
%66 = load i32, i32* %8
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 2
%69 = load i8*, i8** %68
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32 @strlen(i8* %72)
%74 = call i32 @write(i32 %66, i8* %69, i32 %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %65
%77 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %65
%80 = load i32, i32* %8
%81 = call i32 @write(i32 %80, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %86
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %79
br label %87
87: ; preds = %99, %86
%88 = load i32, i32* %8
%89 = load i32, i32* @BUFSIZ
%90 = call i32 @read(i32 %88, i8* %16, i32 %89)
store i32 %90, i32* %12
%91 = icmp sgt i32 %90, 0
br i1 %91, label %92, label %100
92: ; preds = %87
%93 = load i32, i32* %12
%94 = call i32 @write(i32 1, i8* %16, i32 %93)
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %99
96: ; preds = %92
%97 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%98 = call i32 @exit(i32 1)
unreachable
99: ; preds = %92
br label %87
100: ; preds = %87
%101 = load i32, i32* %8
%102 = call i32 @close(i32 %101)
%103 = load i8*, i8** %10
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"sosket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp eq i32 %0, 3
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call %struct.hostent* @gethostbyname(i8* %23)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i8*, i8** %22
%28 = tail call i32 @perror(i8* %27)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%34 = bitcast i32* %33 to %struct.sockaddr*
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @bcopy(i32 %32, %struct.sockaddr* %34, i32 %36)
%38 = call i32 @htons(i32 15000)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %38, i32* %39
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %40, i32* %41
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @connect(i32 %15, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %30
%46 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %30
%49 = getelementptr inbounds i8*, i8** %1, i64 2
%50 = load i8*, i8** %49
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %15, i8* %50, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %48
%58 = call i32 @write(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %57, %67
%64 = load i32, i32* @BUFSIZ
%65 = call i32 @read(i32 %15, i8* %8, i32 %64)
%66 = icmp sgt i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %63
%68 = call i32 @write(i32 1, i8* %8, i32 %65)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %63
70: ; preds = %67
%71 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %63
%74 = call i32 @close(i32 %15)
ret i32 0
}