#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;
};
struct servent {
  int /*<<< orphan*/ s_port;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
struct servent *getservbyname(char *, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sock;
  struct servent *service_info;
  struct hostent *host_info;
  struct sockaddr_in server;
  int count;
  char buffer[1024];
  service_info = getservbyname("rot13", "tcp");
  host_info = gethostbyname("localhost");
  server.sin_family = AF_INET;
  memcpy(&server.sin_addr, host_info->h_addr, host_info->h_length);
  server.sin_port = service_info->s_port;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    do {
      perror("socket");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
    do {
      perror("connect");
      exit(EXIT_FAILURE);
    } while (0);
  }
  while (1) {
    if ((count = read(0, buffer, 1024)) == -1) {
      do {
        perror("read");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (write(sock, buffer, count) == -1) {
      do {
        perror("write");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if ((count = read(sock, buffer, count)) == -1) {
      do {
        perror("read");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (write(1, buffer, count) == -1) {
      do {
        perror("write");
        exit(EXIT_FAILURE);
      } while (0);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe50(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
lea    0xe43(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1243 <main+0xb3>
jmp    1229 <main+0x99>
lea    0xde4(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    127c <main+0xec>
jmp    1262 <main+0xd2>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1281 <main+0xf1>
lea    -0x440(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    12bf <main+0x12f>
jmp    12a5 <main+0x115>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12f9 <main+0x169>
jmp    12df <main+0x14f>
lea    0xd42(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    1336 <main+0x1a6>
jmp    131c <main+0x18c>
lea    0xd00(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x440(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1372 <main+0x1e2>
jmp    1358 <main+0x1c8>
lea    0xcc9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1281 <main+0xf1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0xe5e(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,%rbx
lea    0xe52(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    0x2e24(%rip),%rbp
mov    0x0(%rbp),%ecx
mov    %ecx,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1070 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1203 <main+0x73>
lea    0xe16(%rip),%rdi
jmp    128e <main+0xfe>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1223 <main+0x93>
lea    0xdfa(%rip),%rdi
jmp    128e <main+0xfe>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0xf7>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     127e <main+0xee>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0xf7>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1230 <main+0xa0>
lea    0xda3(%rip),%rdi
jmp    128e <main+0xfe>
lea    0xd95(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.servent = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c"rot13\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [8 x i8] c"connect\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @memcpy(i32* %11, i32 %13, i32 %15)
%17 = getelementptr inbounds %struct.servent, %struct.servent* %7, i64 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %22, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %28, %57
%37 = call i32 @read(i32 0, i8* %6, i32 1024)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %36
%44 = call i32 @write(i32 %22, i8* %6, i32 %37)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %43
%51 = call i32 @read(i32 %22, i8* %6, i32 %37)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %50
%58 = call i32 @write(i32 1, i8* %6, i32 %51)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %36
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
}