#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 */
struct sockaddr_nl {
  int dummy;
};
struct nlmsghdr {
  int nlmsg_len;
};

/* Variables and functions */
int EIO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int write(int /*<<< orphan*/, struct nlmsghdr *, int);
int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) {
  int ret;
  ret = write(STDOUT_FILENO, n, n->nlmsg_len);
  if ((ret > 0) && (ret != n->nlmsg_len)) {
    fprintf(stderr, "Short write while saving nlmsg\n");
    ret = -EIO;
  }
  return ret == n->nlmsg_len ? 0 : ret;
}


x86 assembly (clang, -O0):
<save_nlmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    118b <save_nlmsg+0x6b>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
je     118b <save_nlmsg+0x6b>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2e52(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jne    11a4 <save_nlmsg+0x84>
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
jmp    11aa <save_nlmsg+0x8a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_nlmsg>:
push   %rbx
mov    %rsi,%rbx
mov    0x2ec5(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%edx
call   1030 <write@plt>
test   %eax,%eax
jle    115c <save_nlmsg+0x3c>
cmp    (%rbx),%eax
je     115c <save_nlmsg+0x3c>
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
lea    0xeb4(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e80(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
xor    %ecx,%ecx
cmp    (%rbx),%eax
cmovne %eax,%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.nlmsghdr = type { i32 }
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Short write while saving nlmsg\0A\00"
@EIO = global i32 0
define i32 @save_nlmsg(%struct.sockaddr_nl* %0, %struct.nlmsghdr* %1, i8* %2) {
%4 = alloca %struct.sockaddr_nl*
%5 = alloca %struct.nlmsghdr*
%6 = alloca i8*
%7 = alloca i32
store %struct.sockaddr_nl* %0, %struct.sockaddr_nl** %4
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %5
store i8* %2, i8** %6
%8 = load i32, i32* @STDOUT_FILENO
%9 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%10 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%11 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @write(i32 %8, %struct.nlmsghdr* %9, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %3
%17 = load i32, i32* %7
%18 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%19 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp ne i32 %17, %20
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EIO
%26 = sub nsw i32 0, %25
store i32 %26, i32* %7
br label %27
27: ; preds = %22, %16, %3
%28 = load i32, i32* %7
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%30 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = icmp eq i32 %28, %31
br i1 %32, label %33, label %34
33: ; preds = %27
br label %36
34: ; preds = %27
%35 = load i32, i32* %7
br label %36
36: ; preds = %34, %33
%37 = phi i32 [ 0, %33 ], [ %35, %34 ]
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.nlmsghdr = type { i32 }
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Short write while saving nlmsg\0A\00"
@EIO = global i32 0
define i32 @save_nlmsg(%struct.sockaddr_nl* nocapture readnone %0, %struct.nlmsghdr* nocapture %1, i8* nocapture readnone %2) {
%4 = load i32, i32* @STDOUT_FILENO
%5 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @write(i32 %4, %struct.nlmsghdr* %1, i32 %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %3
%10 = load i32, i32* %5
%11 = icmp eq i32 %7, %10
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EIO
%16 = sub nsw i32 0, %15
br label %17
17: ; preds = %12, %9, %3
%18 = phi i32 [ %16, %12 ], [ %7, %9 ], [ %7, %3 ]
%19 = load i32, i32* %5
%20 = icmp eq i32 %18, %19
%21 = select i1 %20, i32 0, i32 %18
ret i32 %21
}