#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_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
struct TYPE_3__ {
  int length;
  int /*<<< orphan*/ msg;
};
typedef TYPE_1__ ca_msg_t;

/* Variables and functions */
int /*<<< orphan*/ CA_GET_MSG;
int /*<<< orphan*/ dump(int /*<<< orphan*/ *, int);
int ioctl(int, int /*<<< orphan*/, TYPE_1__ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int ci_getmsg(int fd, int slot, uint8_t *buf) {
  ca_msg_t msg;
  int res;
  res = ioctl(fd, CA_GET_MSG, &msg);
  if (res < 0)
    return res;
  memcpy(buf, &msg.msg, msg.length);
  dump(buf, msg.length);
  return msg.length;
}


x86 assembly (clang, -O0):
<ci_getmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    116f <ci_getmsg+0x3f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <ci_getmsg+0x65>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <dump@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ci_getmsg>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
js     1170 <ci_getmsg+0x40>
lea    0xc(%rsp),%rsi
mov    0x8(%rsp),%edx
mov    %rbx,%rdi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
call   1040 <dump@plt>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@CA_GET_MSG = global i32 0
define i32 @ci_getmsg(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.TYPE_3__
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32, i32* %5
%11 = load i32, i32* @CA_GET_MSG
%12 = call i32 @ioctl(i32 %10, i32 %11, %struct.TYPE_3__* %8)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %9
store i32 %16, i32* %4
br label %29
17: ; preds = %3
%18 = load i32*, i32** %7
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i32* %18, i32* %19, i32 %21)
%23 = load i32*, i32** %7
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @dump(i32* %23, i32 %25)
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%28 = load i32, i32* %27
store i32 %28, i32* %4
br label %29
29: ; preds = %17, %15
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@CA_GET_MSG = global i32 0
define i32 @ci_getmsg(i32 %0, i32 %1, i32* %2) {
%4 = alloca %struct.TYPE_3__
%5 = bitcast %struct.TYPE_3__* %4 to i8*
%6 = load i32, i32* @CA_GET_MSG
%7 = call i32 @ioctl(i32 %0, i32 %6, %struct.TYPE_3__* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @memcpy(i32* %2, i32* %10, i32 %12)
%14 = load i32, i32* %11
%15 = call i32 @dump(i32* %2, i32 %14)
%16 = load i32, i32* %11
br label %17
17: ; preds = %3, %9
%18 = phi i32 [ %16, %9 ], [ %7, %3 ]
ret i32 %18
}