#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 hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  struct hostent *hp;
  struct sockaddr_in sin;
  char *host;
  char buf[256];
  int s;
  int len;
  if (argc == 2) {
    host = argv[1];
  } else {
    fprintf(stderr, "Usage: simplex-talk host\n");
    exit(1);
  }
  hp = gethostbyname(host);
  if (!hp) {
    fprintf(stderr, "simple-talk: unknown host: %s\n", host);
    exit(1);
  }
  bzero((char *)&sin, sizeof(sin));
  sin.sin_family = AF_INET;
  bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  sin.sin_port = htons(5432);
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("simplex-talk: socket");
    exit(1);
  }
  if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("simple-talk: connect");
    close(s);
    exit(1);
  }
  while (fgets(buf, sizeof(buf), stdin)) {
    buf[256] = '\0';
    len = strlen(buf) + 1;
    send(s, buf, len, 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1204 <main+0x34>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    1225 <main+0x55>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1262 <main+0x92>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
mov    $0x1538,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x144(%rbp)
cmp    $0x0,%eax
jge    12e1 <main+0x111>
lea    0xd67(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x144(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    1320 <main+0x150>
lea    0xd48(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x144(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1325 <main+0x155>
lea    -0x140(%rbp),%rdi
mov    0x2cad(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1381 <main+0x1b1>
movb   $0x0,-0x40(%rbp)
lea    -0x140(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x148(%rbp)
mov    -0x144(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    -0x148(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    1325 <main+0x155>
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
cmp    $0x2,%edi
jne    12e2 <main+0x112>
mov    0x8(%rsi),%rbx
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1303 <main+0x133>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%edi
lea    0x4(%rsp),%rsi
call   10a0 <bcopy@plt>
mov    $0x1538,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1327 <main+0x157>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     133d <main+0x16d>
mov    0x2d6b(%rip),%r14
mov    (%r14),%edx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     12d4 <main+0x104>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movb   $0x0,0x110(%rsp)
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    12a0 <main+0xd0>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd0b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd0a(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: simplex-talk host\0A\00"
@.str.1 = [31 x i8] c"simple-talk: unknown host: %s\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"simplex-talk: socket\00"
@.str.3 = [21 x i8] c"simple-talk: connect\00"
@stdin = 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.hostent*
%8 = alloca %struct.sockaddr_in
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %9
br label %23
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = load i8*, i8** %9
%25 = call %struct.hostent* @gethostbyname(i8* %24)
store %struct.hostent* %25, %struct.hostent** %7
%26 = load %struct.hostent*, %struct.hostent** %7
%27 = icmp ne %struct.hostent* %26, null
br i1 %27, label %33, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %9
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = bitcast %struct.sockaddr_in* %8 to i8*
%35 = call i32 @bzero(i8* %34, i32 12)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %36, i32* %37
%38 = load %struct.hostent*, %struct.hostent** %7
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%42 = bitcast i32* %41 to i8*
%43 = load %struct.hostent*, %struct.hostent** %7
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @bcopy(i32 %40, i8* %42, i32 %45)
%47 = call i32 @htons(i32 5432)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* @PF_INET
%50 = load i32, i32* @SOCK_STREAM
%51 = call i32 @socket(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %11
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %33
%54 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %33
%57 = load i32, i32* %11
%58 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%59 = call i64 @connect(i32 %57, %struct.sockaddr* %58, i32 12)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32, i32* %11
%64 = call i32 @close(i32 %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %56
br label %67
67: ; preds = %72, %66
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%69 = load i32, i32* @stdin
%70 = call i64 @fgets(i8* %68, i32 256, i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %81
72: ; preds = %67
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 256
store i8 0, i8* %73
%74 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%75 = call i32 @strlen(i8* %74)
%76 = add nsw i32 %75, 1
store i32 %76, i32* %12
%77 = load i32, i32* %11
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%79 = load i32, i32* %12
%80 = call i32 @send(i32 %77, i8* %78, i32 %79, i32 0)
br label %67
81: ; preds = %67
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: simplex-talk host\0A\00"
@.str.1 = [31 x i8] c"simple-talk: unknown host: %s\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"simplex-talk: socket\00"
@.str.3 = [21 x i8] c"simple-talk: connect\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call %struct.hostent* @gethostbyname(i8* %10)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %17, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %8
%22 = call i32 @bzero(i8* %5, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%28 = bitcast i32* %27 to i8*
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @bcopy(i32 %26, i8* %28, i32 %30)
%32 = call i32 @htons(i32 5432)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* @PF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %21
%39 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %21
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @connect(i32 %36, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %41
%46 = load i32, i32* @stdin
%47 = call i64 @fgets(i8* %6, i32 256, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %62, label %49
49: ; preds = %45
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 256
br label %55
51: ; preds = %41
%52 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @close(i32 %36)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49, %55
store i8 0, i8* %50
%56 = call i32 @strlen(i8* %6)
%57 = add nsw i32 %56, 1
%58 = call i32 @send(i32 %36, i8* %6, i32 %57, i32 0)
%59 = load i32, i32* @stdin
%60 = call i64 @fgets(i8* %6, i32 256, i32 %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %55
62: ; preds = %55, %45
ret i32 0
}