#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;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int printer_connect(const char *host, const int timeout) {
  int socket_descriptor = -1;
  int i;
  for (i = 0; i < timeout; i++) {
    struct addrinfo *res;
    struct addrinfo *addr;
    struct addrinfo base = {0, PF_UNSPEC, SOCK_STREAM};
    int error_code = getaddrinfo(host, "printer", &base, &res);
    alarm((60));
    if (!error_code) {
      for (addr = res; addr; addr = addr->ai_next) {
        socket_descriptor =
            socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if (socket_descriptor >= 0) {
          if (!connect(socket_descriptor, addr->ai_addr, addr->ai_addrlen)) {
            break;
          } else {
            close(socket_descriptor);
            socket_descriptor = -1;
          }
        }
      }
      freeaddrinfo(res);
    }
    if (socket_descriptor >= 0) {
      break;
    }
    sleep(1);
  }
  if (i >= timeout) {
    fprintf(stderr, "Cannot connect to %s\n", host);
    return -1;
  }
  alarm(0);
  return socket_descriptor;
}


x86 assembly (clang, -O0):
<printer_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12c7 <printer_connect+0x137>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe14(%rip),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x64(%rbp)
mov    $0x3c,%edi
call   1040 <alarm@plt>
cmpl   $0x0,-0x64(%rbp)
jne    12a0 <printer_connect+0x110>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1297 <printer_connect+0x107>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1281 <printer_connect+0xf1>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%eax
jne    126d <printer_connect+0xdd>
jmp    1297 <printer_connect+0x107>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x18(%rbp)
jmp    1281 <printer_connect+0xf1>
jmp    1286 <printer_connect+0xf6>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1218 <printer_connect+0x88>
mov    -0x28(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpl   $0x0,-0x18(%rbp)
jl     12af <printer_connect+0x11f>
jmp    12c7 <printer_connect+0x137>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ad <printer_connect+0x1d>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     12f8 <printer_connect+0x168>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd21(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <printer_connect+0x175>
xor    %edi,%edi
call   1040 <alarm@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printer_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%r12
test   %esi,%esi
jle    12a2 <printer_connect+0x122>
mov    %esi,%r13d
xor    %r14d,%r14d
mov    $0x1,%r15b
mov    0x2e3f(%rip),%rbp
mov    %r12,0x8(%rsp)
jmp    11e0 <printer_connect+0x60>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2e28(%rip),%rbp
mov    0x8(%rsp),%r12
mov    $0x1,%edi
call   1080 <sleep@plt>
add    $0x1,%r14d
cmp    %r13d,%r14d
setl   %r15b
je     12a2 <printer_connect+0x122>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %r12,%rdi
lea    0xdf0(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,%ebx
mov    $0x3c,%edi
call   1030 <alarm@plt>
test   %ebx,%ebx
jne    11c5 <printer_connect+0x45>
mov    (%rsp),%r12
test   %r12,%r12
jne    124e <printer_connect+0xce>
xor    %edi,%edi
jmp    11b4 <printer_connect+0x34>
nopl   (%rax)
mov    0x18(%r12),%r12
test   %r12,%r12
je     11b0 <printer_connect+0x30>
mov    0x10(%r12),%edi
mov    0x8(%r12),%edx
mov    0xc(%r12),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     1240 <printer_connect+0xc0>
mov    %eax,%ebx
mov    (%r12),%edx
mov    0x4(%r12),%esi
mov    %eax,%edi
call   1060 <connect@plt>
test   %eax,%eax
je     1285 <printer_connect+0x105>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1240 <printer_connect+0xc0>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
test   $0x1,%r15b
mov    0x8(%rsp),%r12
je     12a2 <printer_connect+0x122>
xor    %edi,%edi
call   1030 <alarm@plt>
jmp    12bf <printer_connect+0x13f>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
mov    %r12,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32, i32, i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"printer\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Cannot connect to %s\0A\00"
define i32 @printer_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %74, %2
%13 = load i32, i32* %7
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %77
16: ; preds = %12
%17 = bitcast %struct.addrinfo* %10 to i8*
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 1
%19 = load i32, i32* @PF_UNSPEC
store i32 %19, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 2
%21 = load i32, i32* @SOCK_STREAM
store i32 %21, i32* %20
%22 = load i8*, i8** %4
%23 = call i32 @getaddrinfo(i8* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %10, %struct.addrinfo** %8)
store i32 %23, i32* %11
%24 = call i32 @alarm(i32 60)
%25 = load i32, i32* %11
%26 = icmp ne i32 %25, 0
br i1 %26, label %68, label %27
27: ; preds = %16
%28 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %28, %struct.addrinfo** %9
br label %29
29: ; preds = %61, %27
%30 = load %struct.addrinfo*, %struct.addrinfo** %9
%31 = icmp ne %struct.addrinfo* %30, null
br i1 %31, label %32, label %65
32: ; preds = %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 4
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %9
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 3
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %9
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 2
%41 = load i32, i32* %40
%42 = call i32 @socket(i32 %35, i32 %38, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %60
45: ; preds = %32
%46 = load i32, i32* %6
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %9
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @connect(i32 %46, i32 %49, i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %45
br label %65
56: ; preds = %45
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %6
br label %59
59: ; preds = %56
br label %60
60: ; preds = %59, %32
br label %61
61: ; preds = %60
%62 = load %struct.addrinfo*, %struct.addrinfo** %9
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 5
%64 = load %struct.addrinfo*, %struct.addrinfo** %63
store %struct.addrinfo* %64, %struct.addrinfo** %9
br label %29
65: ; preds = %55, %29
%66 = load %struct.addrinfo*, %struct.addrinfo** %8
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
br label %68
68: ; preds = %65, %16
%69 = load i32, i32* %6
%70 = icmp sge i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %68
br label %77
72: ; preds = %68
%73 = call i32 @sleep(i32 1)
br label %74
74: ; preds = %72
%75 = load i32, i32* %7
%76 = add nsw i32 %75, 1
store i32 %76, i32* %7
br label %12
77: ; preds = %71, %12
%78 = load i32, i32* %7
%79 = load i32, i32* %5
%80 = icmp sge i32 %78, %79
br i1 %80, label %81, label %85
81: ; preds = %77
%82 = load i32, i32* @stderr
%83 = load i8*, i8** %4
%84 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %83)
store i32 -1, i32* %3
br label %88
85: ; preds = %77
%86 = call i32 @alarm(i32 0)
%87 = load i32, i32* %6
store i32 %87, i32* %3
br label %88
88: ; preds = %85, %81
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32, i32, i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"printer\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Cannot connect to %s\0A\00"
define i32 @printer_connect(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %58
6: ; preds = %2
%7 = bitcast %struct.addrinfo** %3 to i8*
%8 = bitcast %struct.addrinfo* %4 to i8*
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
br label %11
11: ; preds = %6, %50
%12 = phi i1 [ true, %6 ], [ %53, %50 ]
%13 = phi i32 [ 0, %6 ], [ %52, %50 ]
%14 = load i32, i32* @PF_UNSPEC
store i32 %14, i32* %9
%15 = load i32, i32* @SOCK_STREAM
store i32 %15, i32* %10
%16 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %3)
%17 = call i32 @alarm(i32 60)
%18 = icmp eq i32 %16, 0
br i1 %18, label %19, label %50
19: ; preds = %11
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %47, label %22
22: ; preds = %19, %41
%23 = phi %struct.addrinfo* [ %43, %41 ], [ %20, %19 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %25, i32 %27, i32 %29)
%31 = icmp sgt i32 %30, -1
br i1 %31, label %32, label %41
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @connect(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %55, label %39
39: ; preds = %32
%40 = call i32 @close(i32 %30)
br label %41
41: ; preds = %22, %39
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
%44 = icmp eq %struct.addrinfo* %43, null
br i1 %44, label %45, label %22
45: ; preds = %41
%46 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %47
47: ; preds = %45, %19
%48 = phi %struct.addrinfo* [ null, %19 ], [ %46, %45 ]
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %50
50: ; preds = %11, %47
%51 = call i32 @sleep(i32 1)
%52 = add nuw nsw i32 %13, 1
%53 = icmp slt i32 %52, %1
%54 = icmp eq i32 %52, %1
br i1 %54, label %58, label %11
55: ; preds = %32
%56 = load %struct.addrinfo*, %struct.addrinfo** %3
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
br i1 %12, label %61, label %58
58: ; preds = %50, %2, %55
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %63
61: ; preds = %55
%62 = call i32 @alarm(i32 0)
br label %63
63: ; preds = %61, %58
%64 = phi i32 [ -1, %58 ], [ %30, %61 ]
ret i32 %64
}