#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 {
  int dummy;
};

/* Variables and functions */
int SOCK_DGRAM;
int /*<<< orphan*/ perror(char *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int write(int, char *, int);
int dsm_write(int s, int type, char *buf, int len, struct sockaddr *saddr) {
  int ret;
  if (type == SOCK_DGRAM && saddr != NULL) {
    if ((ret = sendto(s, buf, len, 0, saddr, sizeof(struct sockaddr))) < 0) {
      perror("sendto error");
      return (-1);
    }
  } else if ((ret = write(s, buf, len)) < 0) {
    perror("write error");
    return (-1);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<dsm_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%eax
jne    11aa <dsm_write+0x7a>
cmpq   $0x0,-0x28(%rbp)
je     11aa <dsm_write+0x7a>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11a5 <dsm_write+0x75>
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <dsm_write+0xb8>
jmp    11e2 <dsm_write+0xb2>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11dd <dsm_write+0xad>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <dsm_write+0xb8>
jmp    11e2 <dsm_write+0xb2>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsm_write>:
push   %rax
test   %r8,%r8
je     1159 <dsm_write+0x29>
mov    0x2eab(%rip),%rax
cmp    %esi,(%rax)
jne    1159 <dsm_write+0x29>
mov    %rdx,%rsi
mov    %ecx,%edx
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
test   %eax,%eax
js     1170 <dsm_write+0x40>
pop    %rcx
ret
mov    %rdx,%rsi
mov    %ecx,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1157 <dsm_write+0x27>
lea    0xe9f(%rip),%rdi
jmp    1177 <dsm_write+0x47>
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"sendto error\00"
@.str.1 = [12 x i8] c"write error\00"
define i32 @dsm_write(i32 %0, i32 %1, i8* %2, i32 %3, %struct.sockaddr* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.sockaddr*
%12 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store %struct.sockaddr* %4, %struct.sockaddr** %11
%13 = load i32, i32* %8
%14 = load i32, i32* @SOCK_DGRAM
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %5
%17 = load %struct.sockaddr*, %struct.sockaddr** %11
%18 = icmp ne %struct.sockaddr* %17, null
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i32, i32* %7
%21 = load i8*, i8** %9
%22 = load i32, i32* %10
%23 = load %struct.sockaddr*, %struct.sockaddr** %11
%24 = call i32 @sendto(i32 %20, i8* %21, i32 %22, i32 0, %struct.sockaddr* %23, i32 4)
store i32 %24, i32* %12
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %40
28: ; preds = %19
br label %38
29: ; preds = %16, %5
%30 = load i32, i32* %7
%31 = load i8*, i8** %9
%32 = load i32, i32* %10
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %12
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %40
37: ; preds = %29
br label %38
38: ; preds = %37, %28
%39 = load i32, i32* %12
store i32 %39, i32* %6
br label %40
40: ; preds = %38, %35, %26
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"sendto error\00"
@.str.1 = [12 x i8] c"write error\00"
define i32 @dsm_write(i32 %0, i32 %1, i8* %2, i32 %3, %struct.sockaddr* %4) {
%6 = load i32, i32* @SOCK_DGRAM
%7 = icmp eq i32 %6, %1
%8 = icmp ne %struct.sockaddr* %4, null
%9 = and i1 %8, %7
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = tail call i32 @sendto(i32 %0, i8* %2, i32 %3, i32 0, %struct.sockaddr* %4, i32 4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %19
13: ; preds = %5
%14 = tail call i32 @write(i32 %0, i8* %2, i32 %3)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13, %10
%17 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %13 ]
%18 = tail call i32 @perror(i8* %17)
br label %19
19: ; preds = %16, %10, %13
%20 = phi i32 [ %11, %10 ], [ %14, %13 ], [ -1, %16 ]
ret i32 %20
}