#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
scalar_t__ getaddrinfo(char *, char *, int /*<<< orphan*/ *,
                       struct addrinfo **);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main() {
  int buf[100];
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    printf("ERROR socket\n");
    return 1;
  }
  struct addrinfo *res;
  if (getaddrinfo("127.0.0.1", "1234", NULL, &res) < 0) {
    printf("ERROR getaddrinfo, %d, %s\n", errno, strerror(errno));
    return 2;
  }
  if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
    printf("ERROR connect, %d, %s\n", errno, strerror(errno));
    return 3;
  }
  ssize_t len = read(fd, buf, 100);
  if (len < -1) {
    printf("ERROR read, %d, %s\n", errno, strerror(errno));
    return 4;
  }
  printf("%s", buf);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1a4(%rbp)
cmpl   $0x0,-0x1a4(%rbp)
jge    11b8 <main+0x58>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x1a7>
lea    0xe4f(%rip),%rdi
lea    0xe52(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x1b0(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
jge    1220 <main+0xc0>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1b8(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1b8(%rbp),%esi
mov    %rax,%rdx
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1307 <main+0x1a7>
mov    -0x1a4(%rbp),%edi
mov    -0x1b0(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x1b0(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    1288 <main+0x128>
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1bc(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1bc(%rbp),%esi
mov    %rax,%rdx
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1307 <main+0x1a7>
mov    -0x1a4(%rbp),%edi
lea    -0x1a0(%rbp),%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x1b4(%rbp)
cmpl   $0xffffffff,-0x1b4(%rbp)
jge    12f2 <main+0x192>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c0(%rbp)
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c0(%rbp),%esi
mov    %rax,%rdx
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x4,-0x4(%rbp)
jmp    1307 <main+0x1a7>
lea    -0x1a0(%rbp),%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1a0,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1214 <main+0xa4>
mov    %eax,%ebx
lea    0xe62(%rip),%rdi
lea    0xe65(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
js     1227 <main+0xb7>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1060 <connect@plt>
test   %rax,%rax
js     1251 <main+0xe1>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1050 <read@plt>
cmp    $0xfffffffe,%eax
jg     127b <main+0x10b>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe40(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x4,%ebx
jmp    1290 <main+0x120>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1290 <main+0x120>
mov    0x2da2(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdd1(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%ebx
jmp    1290 <main+0x120>
mov    0x2d78(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdc2(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%ebx
jmp    1290 <main+0x120>
lea    0xdd3(%rip),%rdi
xor    %ebx,%ebx
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x1a0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"ERROR socket\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [5 x i8] c"1234\00"
@.str.3 = [27 x i8] c"ERROR getaddrinfo, %d, %s\0A\00"
@errno = global i32 0
@.str.4 = [23 x i8] c"ERROR connect, %d, %s\0A\00"
@.str.5 = [20 x i8] c"ERROR read, %d, %s\0A\00"
@.str.6 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i32]
%3 = alloca i32
%4 = alloca %struct.addrinfo*
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %50
13: ; preds = %0
%14 = call i64 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* null, %struct.addrinfo** %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %17, i8* %19)
store i32 2, i32* %1
br label %50
21: ; preds = %13
%22 = load i32, i32* %3
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i64 @connect(i32 %22, i32 %25, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %21
%32 = load i32, i32* @errno
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %32, i8* %34)
store i32 3, i32* %1
br label %50
36: ; preds = %21
%37 = load i32, i32* %3
%38 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%39 = call i32 @read(i32 %37, i32* %38, i32 100)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i32, i32* @errno
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %43, i8* %45)
store i32 4, i32* %1
br label %50
47: ; preds = %36
%48 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %48)
br label %50
50: ; preds = %47, %42, %31, %16, %11
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [5 x i8] c"1234\00"
@.str.3 = [27 x i8] c"ERROR getaddrinfo, %d, %s\0A\00"
@errno = global i32 0
@.str.4 = [23 x i8] c"ERROR connect, %d, %s\0A\00"
@.str.5 = [20 x i8] c"ERROR read, %d, %s\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@str = [13 x i8] c"ERROR socket\00"
define i32 @main() {
%1 = alloca [100 x i32]
%2 = alloca %struct.addrinfo*
%3 = bitcast [100 x i32]* %1 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %42
10: ; preds = %0
%11 = bitcast %struct.addrinfo** %2 to i8*
%12 = call i64 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* null, %struct.addrinfo** %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %15, i8* %16)
br label %40
18: ; preds = %10
%19 = load %struct.addrinfo*, %struct.addrinfo** %2
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @connect(i32 %6, i32 %21, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %27, i8* %28)
br label %40
30: ; preds = %18
%31 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%32 = call i32 @read(i32 %6, i32* %31, i32 100)
%33 = icmp slt i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %35, i8* %36)
br label %40
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %31)
br label %40
40: ; preds = %34, %38, %26, %14
%41 = phi i32 [ 2, %14 ], [ 3, %26 ], [ 4, %34 ], [ 0, %38 ]
br label %42
42: ; preds = %40, %8
%43 = phi i32 [ 1, %8 ], [ %41, %40 ]
ret i32 %43
}