#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 read(int, char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int dsm_read(int s, int type, char *buf, int len, struct sockaddr *saddr) {
  int slen = sizeof(struct sockaddr);
  if (type == SOCK_DGRAM && saddr != NULL) {
    if ((len = recvfrom(s, buf, len, 0, saddr, &slen)) < 0)
      perror("read error");
  } else if ((len = read(s, buf, len)) < 0)
    perror("read error");
  return (len);
}


x86 assembly (clang, -O0):
<dsm_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%eax
jne    11a3 <dsm_read+0x73>
cmpq   $0x0,-0x20(%rbp)
je     11a3 <dsm_read+0x73>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x24(%rbp),%r9
call   1040 <recvfrom@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119e <dsm_read+0x6e>
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
jmp    11cf <dsm_read+0x9f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ca <dsm_read+0x9a>
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
jmp    11cf <dsm_read+0x9f>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsm_read>:
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0xc(%rsp)
test   %r8,%r8
je     1160 <dsm_read+0x30>
mov    0x2e9f(%rip),%rax
cmp    %esi,(%rax)
jne    1160 <dsm_read+0x30>
lea    0xc(%rsp),%r9
mov    %rdx,%rsi
mov    %ecx,%edx
xor    %ecx,%ecx
call   1040 <recvfrom@plt>
jmp    116a <dsm_read+0x3a>
mov    %rdx,%rsi
mov    %ecx,%edx
call   1030 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1178 <dsm_read+0x48>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
jmp    1170 <dsm_read+0x40>

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

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