#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 */
typedef unsigned char uint32_t;
struct FramebufferFormat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bzero(unsigned char *, int);
scalar_t__ do_read(int, unsigned char *, int);
int /*<<< orphan*/ do_write(int, ...);
int /*<<< orphan*/ encode_FramebufferFormat(unsigned char *,
                                            struct FramebufferFormat *);
unsigned char htonl(int);
int do_server_initialization(int clientr, int clientw,
                             struct FramebufferFormat *fbf) {
  unsigned char packet[24];
  uint32_t noauth = htonl(1);
  do_write(clientw, "RFB 003.003\n", 12);
  if (do_read(clientr, packet, 12))
    return 1;
  do_write(clientw, &noauth, 4);
  if (do_read(clientr, packet, 1))
    return 1;
  bzero(packet, 24);
  encode_FramebufferFormat(packet, fbf);
  do_write(clientw, packet, 24);
  return 0;
}


x86 assembly (clang, -O0):
<do_server_initialization>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
call   1050 <htonl@plt>
mov    %al,-0x31(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe87(%rip),%rsi
mov    $0xc,%edx
mov    $0x0,%al
call   1040 <do_write@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1030 <do_read@plt>
cmp    $0x0,%rax
je     11ac <do_server_initialization+0x5c>
movl   $0x1,-0x4(%rbp)
jmp    121b <do_server_initialization+0xcb>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <do_write@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1030 <do_read@plt>
cmp    $0x0,%rax
je     11e6 <do_server_initialization+0x96>
movl   $0x1,-0x4(%rbp)
jmp    121b <do_server_initialization+0xcb>
lea    -0x30(%rbp),%rdi
mov    $0x18,%esi
call   1060 <bzero@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <encode_FramebufferFormat@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
mov    $0x0,%al
call   1040 <do_write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_server_initialization>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
mov    $0x1,%ebx
mov    $0x1,%edi
call   1050 <htonl@plt>
mov    %al,0xf(%rsp)
lea    0xe84(%rip),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
xor    %eax,%eax
call   1040 <do_write@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1030 <do_read@plt>
test   %rax,%rax
jne    1201 <do_server_initialization+0xb1>
lea    0xf(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <do_write@plt>
lea    0x10(%rsp),%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <do_read@plt>
test   %rax,%rax
jne    1201 <do_server_initialization+0xb1>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x18,%esi
call   1060 <bzero@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <encode_FramebufferFormat@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x18,%edx
xor    %eax,%eax
call   1040 <do_write@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.FramebufferFormat = type { i32 }
@.str = [13 x i8] c"RFB 003.003\0A\00"
define i32 @do_server_initialization(i32 %0, i32 %1, %struct.FramebufferFormat* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.FramebufferFormat*
%8 = alloca [24 x i8]
%9 = alloca i8
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.FramebufferFormat* %2, %struct.FramebufferFormat** %7
%10 = call zeroext i8 @htonl(i32 1)
store i8 %10, i8* %9
%11 = load i32, i32* %6
%12 = call i32 (i32, ...) @do_write(i32 %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%13 = load i32, i32* %5
%14 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%15 = call i64 @do_read(i32 %13, i8* %14, i32 12)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 1, i32* %4
br label %35
18: ; preds = %3
%19 = load i32, i32* %6
%20 = call i32 (i32, ...) @do_write(i32 %19, i8* %9, i32 4)
%21 = load i32, i32* %5
%22 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%23 = call i64 @do_read(i32 %21, i8* %22, i32 1)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 1, i32* %4
br label %35
26: ; preds = %18
%27 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%28 = call i32 @bzero(i8* %27, i32 24)
%29 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%30 = load %struct.FramebufferFormat*, %struct.FramebufferFormat** %7
%31 = call i32 @encode_FramebufferFormat(i8* %29, %struct.FramebufferFormat* %30)
%32 = load i32, i32* %6
%33 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%34 = call i32 (i32, ...) @do_write(i32 %32, i8* %33, i32 24)
store i32 0, i32* %4
br label %35
35: ; preds = %26, %25, %17
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.FramebufferFormat = type { i32 }
@.str = [13 x i8] c"RFB 003.003\0A\00"
define i32 @do_server_initialization(i32 %0, i32 %1, %struct.FramebufferFormat* %2) {
%4 = alloca [24 x i8]
%5 = alloca i8
%6 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%7 = tail call zeroext i8 @htonl(i32 1)
store i8 %7, i8* %5
%8 = tail call i32 (i32, ...) @do_write(i32 %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%9 = call i64 @do_read(i32 %0, i8* %6, i32 12)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = call i32 (i32, ...) @do_write(i32 %1, i8* %5, i32 4)
%13 = call i64 @do_read(i32 %0, i8* %6, i32 1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @bzero(i8* %6, i32 24)
%17 = call i32 @encode_FramebufferFormat(i8* %6, %struct.FramebufferFormat* %2)
%18 = call i32 (i32, ...) @do_write(i32 %1, i8* %6, i32 24)
br label %19
19: ; preds = %11, %3, %15
%20 = phi i32 [ 0, %15 ], [ 1, %3 ], [ 1, %11 ]
ret i32 %20
}