#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ err_open();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *str_date_yymmdd();
char *str_time_hhmmss();
int db_tid_connect(char *hostname, int port) {
  int fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    do {
      do {
        err_open();
        fprintf(stderr, "%s.%s %s:%d: ", str_date_yymmdd(), str_time_hhmmss(),
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/tardyp/amake/refs/heads/"
                "master/tcd/db_tid.c",
                34);
        fprintf(stderr, "db_tid_connect(): socket() failed");
        fprintf(stderr, "\n");
        fflush(stderr);
      } while (0);
      exit(1);
    } while (0);
  struct sockaddr_in addr;
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  struct hostent *host = gethostbyname(hostname);
  if (!host)
    do {
      do {
        err_open();
        fprintf(stderr, "%s.%s %s:%d: ", str_date_yymmdd(), str_time_hhmmss(),
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/tardyp/amake/refs/heads/"
                "master/tcd/db_tid.c",
                40);
        fprintf(stderr, "gethostbyname() failed");
        fprintf(stderr, "\n");
        fflush(stderr);
      } while (0);
      exit(1);
    } while (0);
  memcpy(&addr.sin_addr, host->h_addr, host->h_length);
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(fd);
    fd = 0;
  }
  return fd;
}


x86 assembly (clang, -O0):
<db_tid_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    129f <db_tid_connect+0xcf>
jmp    120a <db_tid_connect+0x3a>
jmp    120f <db_tid_connect+0x3f>
mov    $0x0,%al
call   10e0 <err_open@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1090 <str_date_yymmdd@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <str_time_hhmmss@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbb(%rip),%rsi
lea    0xdc2(%rip),%r8
mov    $0x22,%r9d
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    136b <db_tid_connect+0x19b>
jmp    12d6 <db_tid_connect+0x106>
jmp    12db <db_tid_connect+0x10b>
mov    $0x0,%al
call   10e0 <err_open@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1090 <str_date_yymmdd@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <str_time_hhmmss@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xcef(%rip),%rsi
lea    0xcf6(%rip),%r8
mov    $0x28,%r9d
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x20(%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    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    13ba <db_tid_connect+0x1ea>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_tid_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     126a <db_tid_connect+0x9a>
mov    %eax,%ebx
mov    %rsp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     12bb <db_tid_connect+0xeb>
lea    0x4(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1070 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jns    125f <db_tid_connect+0x8f>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   10e0 <err_open@plt>
mov    0x2d60(%rip),%r14
mov    (%r14),%ebx
xor    %eax,%eax
call   1090 <str_date_yymmdd@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   10a0 <str_time_hhmmss@plt>
lea    0xd6d(%rip),%rsi
lea    0xd74(%rip),%r8
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
mov    $0x22,%r9d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
lea    0xddb(%rip),%rsi
jmp    130a <db_tid_connect+0x13a>
xor    %eax,%eax
call   10e0 <err_open@plt>
mov    0x2d0f(%rip),%r14
mov    (%r14),%ebx
xor    %eax,%eax
call   1090 <str_date_yymmdd@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   10a0 <str_time_hhmmss@plt>
lea    0xd1c(%rip),%rsi
lea    0xd23(%rip),%r8
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
mov    $0x28,%r9d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
lea    0xdae(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
lea    0xd9b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"%s.%s %s:%d: \00"
@.str.1 = [134 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/tardyp/amake/refs/heads/master/tcd/db_tid.c\00"
@.str.2 = [34 x i8] c"db_tid_connect(): socket() failed\00"
@.str.3 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@.str.4 = [23 x i8] c"gethostbyname() failed\00"
define i32 @db_tid_connect(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %2
br label %14
14: ; preds = %13
br label %15
15: ; preds = %14
%16 = call i32 (...) @err_open()
%17 = load i32, i32* @stderr
%18 = call i8* (...) @str_date_yymmdd()
%19 = call i8* (...) @str_time_hhmmss()
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.1, i64 0, i64 0), i32 34)
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 @fflush(i32 %25)
br label %27
27: ; preds = %15
%28 = call i32 @exit(i32 1)
unreachable
29: ; No predecessors!
br label %30
30: ; preds = %29, %2
%31 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i8*, i8** %3
%35 = call %struct.hostent* @gethostbyname(i8* %34)
store %struct.hostent* %35, %struct.hostent** %7
%36 = load %struct.hostent*, %struct.hostent** %7
%37 = icmp ne %struct.hostent* %36, null
br i1 %37, label %55, label %38
38: ; preds = %30
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39
%41 = call i32 (...) @err_open()
%42 = load i32, i32* @stderr
%43 = call i8* (...) @str_date_yymmdd()
%44 = call i8* (...) @str_time_hhmmss()
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %43, i8* %44, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.1, i64 0, i64 0), i32 40)
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @stderr
%51 = call i32 @fflush(i32 %50)
br label %52
52: ; preds = %40
%53 = call i32 @exit(i32 1)
unreachable
54: ; No predecessors!
br label %55
55: ; preds = %54, %30
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%57 = load %struct.hostent*, %struct.hostent** %7
%58 = getelementptr inbounds %struct.hostent, %struct.hostent* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load %struct.hostent*, %struct.hostent** %7
%61 = getelementptr inbounds %struct.hostent, %struct.hostent* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @memcpy(i32* %56, i32 %59, i32 %62)
%64 = load i32, i32* %4
%65 = call i32 @htons(i32 %64)
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %65, i32* %66
%67 = load i32, i32* %5
%68 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%69 = call i64 @connect(i32 %67, %struct.sockaddr* %68, i32 12)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %55
%72 = load i32, i32* %5
%73 = call i32 @close(i32 %72)
store i32 0, i32* %5
br label %74
74: ; preds = %71, %55
%75 = load i32, i32* %5
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"%s.%s %s:%d: \00"
@.str.1 = [134 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/tardyp/amake/refs/heads/master/tcd/db_tid.c\00"
@.str.2 = [34 x i8] c"db_tid_connect(): socket() failed\00"
@.str.3 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@.str.4 = [23 x i8] c"gethostbyname() failed\00"
define i32 @db_tid_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_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 %21
8: ; preds = %2
%9 = tail call i32 (...) @err_open()
%10 = load i32, i32* @stderr
%11 = tail call i8* (...) @str_date_yymmdd()
%12 = tail call i8* (...) @str_time_hhmmss()
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.1, i64 0, i64 0), i32 34)
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = tail call i32 @fflush(i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = bitcast %struct.sockaddr_in* %3 to i8*
%23 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = tail call %struct.hostent* @gethostbyname(i8* %0)
%27 = icmp eq %struct.hostent* %26, null
br i1 %27, label %28, label %41
28: ; preds = %21
%29 = tail call i32 (...) @err_open()
%30 = load i32, i32* @stderr
%31 = tail call i8* (...) @str_date_yymmdd()
%32 = tail call i8* (...) @str_time_hhmmss()
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.1, i64 0, i64 0), i32 40)
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @stderr
%39 = tail call i32 @fflush(i32 %38)
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %21
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @memcpy(i32* %42, i32 %44, i32 %46)
%48 = call i32 @htons(i32 %1)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %48, i32* %49
%50 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%51 = call i64 @connect(i32 %6, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %41
%54 = call i32 @close(i32 %6)
br label %55
55: ; preds = %53, %41
%56 = phi i32 [ 0, %53 ], [ %6, %41 ]
ret i32 %56
}