#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ check_connect();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                  struct sockaddr const *, int);
int /*<<< orphan*/ shout(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ socket_error(char *);
int /*<<< orphan*/ strlen(char *);
void keep_alive(const char *server, const char *session_id, const char *user) {
  struct sockaddr_in server_addr;
  int sc_keep_alive = -1;
  int namelen = 0;
  char cmd[1024];
  namelen = sizeof(server_addr);
  memset(&server_addr, 0, namelen);
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(10001);
  server_addr.sin_addr.s_addr = inet_addr(server);
  sc_keep_alive = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (-1 == sc_keep_alive) {
    socket_error("socket:");
    goto err;
  }
  snprintf(cmd, sizeof(cmd),
           "KEEP_ALIVE\r\nSESSIONID:%s\r\nUSER:%s\r\nAUTH_TYPE:%s\r\n\r\n",
           session_id, user, "DOMAIN");
  while (1) {
    if (sendto(sc_keep_alive, cmd, strlen(cmd), 0,
               (const struct sockaddr *)&server_addr, namelen) <= 0) {
      socket_error("sendto:");
      break;
    }
    shout(".");
    if (check_connect() != 0) {
      break;
    }
    sleep(30);
  }
err:
  if (sc_keep_alive != -1) {
    close(sc_keep_alive);
    sc_keep_alive = -1;
  }
  return;
}


x86 assembly (clang, -O0):
<keep_alive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0xc,-0x30(%rbp)
mov    -0x30(%rbp),%edx
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x2711,%edi
call   1060 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    1261 <keep_alive+0xa1>
lea    0xda9(%rip),%rdi
call   1040 <socket_error@plt>
jmp    131a <keep_alive+0x15a>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xd8c(%rip),%rdx
lea    0xdb9(%rip),%r9
call   1070 <snprintf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x430(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9d
xor    %ecx,%ecx
call   1090 <sendto@plt>
cmp    $0x0,%rax
jg     12e4 <keep_alive+0x124>
lea    0xd69(%rip),%rdi
call   1040 <socket_error@plt>
jmp    1315 <keep_alive+0x155>
lea    0xd60(%rip),%rdi
call   10b0 <shout@plt>
mov    $0x0,%al
call   1030 <check_connect@plt>
cmp    $0x0,%rax
je     1306 <keep_alive+0x146>
jmp    1315 <keep_alive+0x155>
mov    $0x1e,%edi
call   10d0 <sleep@plt>
jmp    1288 <keep_alive+0xc8>
jmp    131a <keep_alive+0x15a>
cmpl   $0xffffffff,-0x2c(%rbp)
je     1333 <keep_alive+0x173>
mov    -0x2c(%rbp),%edi
call   10a0 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keep_alive>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x2711,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   10c0 <inet_addr@plt>
mov    %eax,(%rsp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e7 <keep_alive+0x127>
mov    %eax,%ebp
lea    0xdcb(%rip),%rdx
lea    0xdf8(%rip),%r9
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rsp,%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1090 <sendto@plt>
test   %rax,%rax
jle    12d2 <keep_alive+0x112>
lea    0xdc4(%rip),%r14
lea    0x10(%rsp),%rbx
mov    %rsp,%r15
nop
mov    %r14,%rdi
call   10b0 <shout@plt>
xor    %eax,%eax
call   1030 <check_connect@plt>
test   %rax,%rax
jne    12de <keep_alive+0x11e>
mov    $0x1e,%edi
call   10d0 <sleep@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1090 <sendto@plt>
test   %rax,%rax
jg     1290 <keep_alive+0xd0>
lea    0xd6a(%rip),%rdi
call   1040 <socket_error@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
jmp    12f3 <keep_alive+0x133>
lea    0xd12(%rip),%rdi
call   1040 <socket_error@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [8 x i8] c"socket:\00"
@.str.1 = [52 x i8] c"KEEP_ALIVE\0D\0ASESSIONID:%s\0D\0AUSER:%s\0D\0AAUTH_TYPE:%s\0D\0A\0D\0A\00"
@.str.2 = [7 x i8] c"DOMAIN\00"
@.str.3 = [8 x i8] c"sendto:\00"
@.str.4 = [2 x i8] c".\00"
define void @keep_alive(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 -1, i32* %8
store i32 0, i32* %9
store i32 12, i32* %9
%11 = load i32, i32* %9
%12 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 %11)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 10001)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = call i32 @inet_addr(i8* %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = load i32, i32* @IPPROTO_UDP
%24 = call i32 @socket(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %29
27: ; preds = %3
%28 = call i32 @socket_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %53
29: ; preds = %3
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = call i32 @snprintf(i8* %30, i32 1024, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %29, %50
%35 = load i32, i32* %8
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%40 = load i32, i32* %9
%41 = call i64 @sendto(i32 %35, i8* %36, i32 %38, i32 0, %struct.sockaddr* %39, i32 %40)
%42 = icmp sle i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @socket_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %52
45: ; preds = %34
%46 = call i32 @shout(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = call i64 (...) @check_connect()
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
br label %52
50: ; preds = %45
%51 = call i32 @sleep(i32 30)
br label %34
52: ; preds = %49, %43
br label %53
53: ; preds = %52, %27
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %8
br label %59
59: ; preds = %56, %53
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [8 x i8] c"socket:\00"
@.str.1 = [52 x i8] c"KEEP_ALIVE\0D\0ASESSIONID:%s\0D\0AUSER:%s\0D\0AAUTH_TYPE:%s\0D\0A\0D\0A\00"
@.str.2 = [7 x i8] c"DOMAIN\00"
@.str.3 = [8 x i8] c"sendto:\00"
@.str.4 = [2 x i8] c".\00"
define void @keep_alive(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @htons(i32 10001)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @inet_addr(i8* %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = load i32, i32* @IPPROTO_UDP
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @socket_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %41
22: ; preds = %3
%23 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @strlen(i8* %7)
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i64 @sendto(i32 %18, i8* %7, i32 %24, i32 0, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %34, %22
%29 = call i32 @socket_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %39
30: ; preds = %22, %34
%31 = call i32 @shout(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = call i64 (...) @check_connect()
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = call i32 @sleep(i32 30)
%36 = call i32 @strlen(i8* %7)
%37 = call i64 @sendto(i32 %18, i8* %7, i32 %36, i32 0, %struct.sockaddr* %25, i32 12)
%38 = icmp slt i64 %37, 1
br i1 %38, label %28, label %30
39: ; preds = %30, %28
%40 = call i32 @close(i32 %18)
br label %41
41: ; preds = %20, %39
ret void
}