#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 char uint16_t;

/* Variables and functions */
char ntohs(char);
int read_n_bytes(int, char *, char);
int read_message(int sd, uint16_t *code, char *message) {
  int n;
  uint16_t lon, message_code;
  n = -1;
  n = read_n_bytes(sd, &message_code, 2);
  message_code = ntohs(message_code);
  *code = message_code;
  n = read_n_bytes(sd, &lon, 2);
  lon = ntohs(lon);
  if (n != 0) {
    n = read_n_bytes(sd, message, lon);
  }
  return n;
}


x86 assembly (clang, -O0):
<read_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0x2,%edx
call   1030 <read_n_bytes@plt>
mov    %eax,-0x1c(%rbp)
movsbl -0x1e(%rbp),%edi
call   1040 <ntohs@plt>
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x2,%edx
call   1030 <read_n_bytes@plt>
mov    %eax,-0x1c(%rbp)
movsbl -0x1d(%rbp),%edi
call   1040 <ntohs@plt>
mov    %al,-0x1d(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11a0 <read_message+0x80>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
movsbl -0x1d(%rbp),%edx
call   1030 <read_n_bytes@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_message>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1030 <read_n_bytes@plt>
movsbl 0xe(%rsp),%edi
call   1040 <ntohs@plt>
mov    %al,0xe(%rsp)
mov    %al,(%rbx)
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <read_n_bytes@plt>
mov    %eax,%ebx
movsbl 0xf(%rsp),%edi
call   1040 <ntohs@plt>
mov    %al,0xf(%rsp)
test   %ebx,%ebx
je     1183 <read_message+0x63>
movsbl %al,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <read_n_bytes@plt>
jmp    1185 <read_message+0x65>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_message(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 -1, i32* %7
%10 = load i32, i32* %4
%11 = call i32 @read_n_bytes(i32 %10, i8* %9, i8 signext 2)
store i32 %11, i32* %7
%12 = load i8, i8* %9
%13 = call signext i8 @ntohs(i8 signext %12)
store i8 %13, i8* %9
%14 = load i8, i8* %9
%15 = load i8*, i8** %5
store i8 %14, i8* %15
%16 = load i32, i32* %4
%17 = call i32 @read_n_bytes(i32 %16, i8* %8, i8 signext 2)
store i32 %17, i32* %7
%18 = load i8, i8* %8
%19 = call signext i8 @ntohs(i8 signext %18)
store i8 %19, i8* %8
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i32, i32* %4
%24 = load i8*, i8** %6
%25 = load i8, i8* %8
%26 = call i32 @read_n_bytes(i32 %23, i8* %24, i8 signext %25)
store i32 %26, i32* %7
br label %27
27: ; preds = %22, %3
%28 = load i32, i32* %7
ret i32 %28
}

LLVM IR (-O2):
define i32 @read_message(i32 %0, i8* nocapture writeonly %1, i8* %2) {
%4 = alloca i8
%5 = alloca i8
%6 = call i32 @read_n_bytes(i32 %0, i8* %5, i8 signext 2)
%7 = load i8, i8* %5
%8 = call signext i8 @ntohs(i8 signext %7)
store i8 %8, i8* %5
store i8 %8, i8* %1
%9 = call i32 @read_n_bytes(i32 %0, i8* %4, i8 signext 2)
%10 = load i8, i8* %4
%11 = call signext i8 @ntohs(i8 signext %10)
store i8 %11, i8* %4
%12 = icmp eq i32 %9, 0
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = call i32 @read_n_bytes(i32 %0, i8* %2, i8 signext %11)
br label %15
15: ; preds = %13, %3
%16 = phi i32 [ %14, %13 ], [ 0, %3 ]
ret i32 %16
}