#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int open_connection(char const *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char const *argv[]) {
  const char *host;
  int sock;
  FILE *f;
  char buf[1024];
  host = argc > 1 ? argv[1] : "localhost";
  if ((sock = open_connection(host, "daytime")) < 0) {
    fprintf(stderr, "socket(2)/connect(2) failed\n");
    exit(1);
  }
  f = fdopen(sock, "r");
  if (!f) {
    perror("fdopen(3)");
    exit(1);
  }
  fgets(buf, sizeof buf, f);
  fclose(f);
  fputs(buf, stdout);
  return 0;
}


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)
cmpl   $0x1,-0x8(%rbp)
jle    11b7 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x438(%rbp)
jmp    11ca <main+0x4a>
lea    0xe42(%rip),%rax
mov    %rax,-0x438(%rbp)
jmp    11ca <main+0x4a>
mov    -0x438(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1050 <open_connection@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1210 <main+0x90>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    0xe15(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1244 <main+0xc4>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
cmp    $0x2,%edi
jl     1195 <main+0x15>
mov    0x8(%rsi),%rdi
jmp    119c <main+0x1c>
lea    0xe64(%rip),%rdi
lea    0xe67(%rip),%rsi
call   1050 <open_connection@plt>
test   %eax,%eax
js     11fb <main+0x7b>
lea    0xe7c(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     121a <main+0x9a>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <fputs@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"localhost\00"
@.str.1 = [8 x i8] c"daytime\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"socket(2)/connect(2) failed\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [10 x i8] c"fdopen(3)\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %12
%18 = phi i8* [ %15, %12 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %16 ]
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = call i32 @open_connection(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %20, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* %7
%28 = call i32* @fdopen(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %28, i32** %8
%29 = load i32*, i32** %8
%30 = icmp ne i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%36 = load i32*, i32** %8
%37 = call i32 @fgets(i8* %35, i32 1024, i32* %36)
%38 = load i32*, i32** %8
%39 = call i32 @fclose(i32* %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%41 = load i32, i32* @stdout
%42 = call i32 @fputs(i8* %40, i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"localhost\00"
@.str.1 = [8 x i8] c"daytime\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"socket(2)/connect(2) failed\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [10 x i8] c"fdopen(3)\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = tail call i32 @open_connection(i8* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = tail call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = call i32 @fgets(i8* %4, i32 1024, i32* %18)
%25 = call i32 @fclose(i32* %18)
%26 = load i32, i32* @stdout
%27 = call i32 @fputs(i8* %4, i32 %26)
ret i32 0
}