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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ gx_getpkt(char *);
int gx_putpkt(char *);
int /*<<< orphan*/ gx_remote_close();
int gx_remote_open(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int process_remote_request(char *);
int main(int argc, char **argv) {
  char *remote_buf;
  int exit_rc = 0;
  remote_buf = malloc(4096);
  if (remote_buf == NULL) {
    printf("failed to allocate communication buffer\n");
    exit(3);
  }
  if (gx_remote_open(argv[0]) == -1) {
    return 1;
  }
  while (gx_getpkt(remote_buf) > 0) {
    if ((exit_rc = process_remote_request(remote_buf)))
      break;
    if (gx_putpkt(remote_buf) == -1) {
      exit_rc = 1;
      break;
    }
  }
  if (exit_rc == 0) {
    printf("Exiting.. Remote side has terminated connection\n");
  }
  gx_remote_close();
  return exit_rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x1000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cc <main+0x4c>
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <gx_remote_open@plt>
cmp    $0xffffffff,%eax
jne    11ed <main+0x6d>
movl   $0x1,-0x4(%rbp)
jmp    1265 <main+0xe5>
jmp    11f2 <main+0x72>
mov    -0x18(%rbp),%rdi
call   1070 <gx_getpkt@plt>
cmp    $0x0,%rax
jle    1242 <main+0xc2>
mov    -0x18(%rbp),%rdi
call   1090 <process_remote_request@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     121f <main+0x9f>
jmp    1242 <main+0xc2>
mov    -0x18(%rbp),%rdi
call   1060 <gx_putpkt@plt>
cmp    $0xffffffff,%eax
jne    123d <main+0xbd>
movl   $0x1,-0x1c(%rbp)
jmp    1242 <main+0xc2>
jmp    11f2 <main+0x72>
cmpl   $0x0,-0x1c(%rbp)
jne    1258 <main+0xd8>
lea    0xdd6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <gx_remote_close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    $0x1000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1206 <main+0x86>
mov    %rax,%rbx
mov    0x0(%rbp),%rdi
call   1030 <gx_remote_open@plt>
cmp    $0xffffffff,%eax
je     11dd <main+0x5d>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <gx_getpkt@plt>
test   %rax,%rax
jle    11e4 <main+0x64>
mov    %rbx,%rdi
call   1090 <process_remote_request@plt>
test   %eax,%eax
jne    11f4 <main+0x74>
mov    %rbx,%rdi
call   1060 <gx_putpkt@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x30>
mov    $0x1,%ebp
jmp    11f6 <main+0x76>
mov    $0x1,%ebp
jmp    11fd <main+0x7d>
lea    0xe15(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
jmp    11f6 <main+0x76>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <gx_remote_close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"failed to allocate communication buffer\0A\00"
@.str.1 = [49 x i8] c"Exiting.. Remote side has terminated connection\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%8 = call i8* @malloc(i32 4096)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 3)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @gx_remote_open(i8* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 1, i32* %3
br label %45
21: ; preds = %14
br label %22
22: ; preds = %36, %21
%23 = load i8*, i8** %6
%24 = call i64 @gx_getpkt(i8* %23)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = call i32 @process_remote_request(i8* %27)
store i32 %28, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %37
31: ; preds = %26
%32 = load i8*, i8** %6
%33 = call i32 @gx_putpkt(i8* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 1, i32* %7
br label %37
36: ; preds = %31
br label %22
37: ; preds = %35, %30, %22
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = call i32 (...) @gx_remote_close()
%44 = load i32, i32* %7
store i32 %44, i32* %3
br label %45
45: ; preds = %42, %20
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@str = [48 x i8] c"Exiting.. Remote side has terminated connection\00"
@str.2 = [40 x i8] c"failed to allocate communication buffer\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 3)
unreachable
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @gx_remote_open(i8* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %26, label %12
12: ; preds = %8, %18
%13 = tail call i64 @gx_getpkt(i8* %3)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = tail call i32 @process_remote_request(i8* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = tail call i32 @gx_putpkt(i8* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %23, label %12
21: ; preds = %12
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %18, %15, %21
%24 = phi i32 [ 0, %21 ], [ %16, %15 ], [ 1, %18 ]
%25 = tail call i32 (...) @gx_remote_close()
br label %26
26: ; preds = %8, %23
%27 = phi i32 [ %24, %23 ], [ 1, %8 ]
ret i32 %27
}