#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 stat {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EISDIR;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ send_errno(int /*<<< orphan*/);
int /*<<< orphan*/ send_long(size_t);
int /*<<< orphan*/ send_success();
scalar_t__ stat(char *, struct stat *);
int write(int, char *, int);
int /*<<< orphan*/ x_perror(char *);
void do_get(int argc, char **argv) {
  assert(argc == 1);
  struct stat st;
  if (stat(argv[0], &st) == 0 && S_ISREG(st.st_mode)) {
    FILE *fh = fopen(argv[0], "r");
    if (fh) {
      size_t size = st.st_size;
      send_success();
      send_long(size);
      while (size > 0) {
        char buf[BUFSIZ];
        int this = (size > BUFSIZ ? BUFSIZ : size);
        this = fread(buf, 1, this, fh);
        if (this == 0) {
          x_perror("get-read");
        }
        if (write(1, buf, this) != this) {
          x_perror("get-write");
        }
        size -= this;
      }
      fclose(fh);
    } else {
      send_errno(errno);
    }
  } else {
    send_errno(EISDIR);
  }
}


x86 assembly (clang, -O0):
<do_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    1336 <do_get+0x186>
mov    -0x18(%rbp),%edi
call   10b0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1336 <do_get+0x186>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf6(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1323 <do_get+0x173>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <send_success@plt>
mov    -0x30(%rbp),%rdi
call   1030 <send_long@plt>
cmpq   $0x0,-0x30(%rbp)
jbe    1315 <do_get+0x165>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d60(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jbe    1297 <do_get+0xe7>
mov    0x2d4d(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    129f <do_get+0xef>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    12d4 <do_get+0x124>
lea    0xd33(%rip),%rdi
call   1050 <x_perror@plt>
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    $0x1,%edi
call   1060 <write@plt>
cmp    -0x44(%rbp),%eax
je     12fa <do_get+0x14a>
lea    0xd16(%rip),%rdi
call   1050 <x_perror@plt>
movslq -0x44(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    1236 <do_get+0x86>
mov    -0x28(%rbp),%rdi
call   1070 <fclose@plt>
jmp    1331 <do_get+0x181>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   10d0 <send_errno@plt>
jmp    1344 <do_get+0x194>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   10d0 <send_errno@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_get>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
cmp    $0x1,%edi
sete   %al
mov    %eax,%edi
call   1080 <assert@plt>
mov    (%rbx),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     1201 <do_get+0x51>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   10d0 <send_errno@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x38(%rbp),%edi
call   10b0 <S_ISREG@plt>
test   %rax,%rax
je     11e4 <do_get+0x34>
mov    (%rbx),%rdi
lea    0xde8(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
test   %rax,%rax
je     12d5 <do_get+0x125>
mov    -0x40(%rbp),%r13
xor    %eax,%eax
call   10a0 <send_success@plt>
mov    %r13,%rdi
call   1030 <send_long@plt>
test   %r13,%r13
je     12c7 <do_get+0x117>
mov    0x2d8b(%rip),%r12
jmp    125b <do_get+0xab>
nop
mov    %r14,%rsp
movslq %r15d,%rax
sub    %rax,%r13
je     12c7 <do_get+0x117>
mov    %rsp,%r14
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movslq (%r12),%rax
cmp    %rax,%r13
mov    %r13d,%edx
cmova  %eax,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    -0x30(%rbp),%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    12a4 <do_get+0xf4>
lea    0xd63(%rip),%rdi
call   1050 <x_perror@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <write@plt>
cmp    %r15d,%eax
je     1250 <do_get+0xa0>
lea    0xd4b(%rip),%rdi
call   1050 <x_perror@plt>
jmp    1250 <do_get+0xa0>
mov    -0x30(%rbp),%rdi
call   1070 <fclose@plt>
jmp    11f2 <do_get+0x42>
mov    0x2cf4(%rip),%rax
jmp    11eb <do_get+0x3b>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [2 x i8] c"r\00"
@BUFSIZ = global i32 0
@.str.1 = [9 x i8] c"get-read\00"
@.str.2 = [10 x i8] c"get-write\00"
@errno = global i32 0
@EISDIR = global i32 0
define void @do_get(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.stat
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i64 @stat(i8* %17, %struct.stat* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %85
20: ; preds = %2
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i64 @S_ISREG(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %85
25: ; preds = %20
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %29, i32** %6
%30 = load i32*, i32** %6
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %81
32: ; preds = %25
%33 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%34 = load i64, i64* %33
store i64 %34, i64* %7
%35 = call i32 (...) @send_success()
%36 = load i64, i64* %7
%37 = call i32 @send_long(i64 %36)
br label %38
38: ; preds = %72, %32
%39 = load i64, i64* %7
%40 = icmp ugt i64 %39, 0
br i1 %40, label %41, label %78
41: ; preds = %38
%42 = load i32, i32* @BUFSIZ
%43 = zext i32 %42 to i64
%44 = call i8* @llvm.stacksave()
store i8* %44, i8** %8
%45 = alloca i8, i64 %43
store i64 %43, i64* %9
%46 = load i64, i64* %7
%47 = load i32, i32* @BUFSIZ
%48 = sext i32 %47 to i64
%49 = icmp ugt i64 %46, %48
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = load i32, i32* @BUFSIZ
%52 = sext i32 %51 to i64
br label %55
53: ; preds = %41
%54 = load i64, i64* %7
br label %55
55: ; preds = %53, %50
%56 = phi i64 [ %52, %50 ], [ %54, %53 ]
%57 = trunc i64 %56 to i32
store i32 %57, i32* %10
%58 = load i32, i32* %10
%59 = load i32*, i32** %6
%60 = call i32 @fread(i8* %45, i32 1, i32 %58, i32* %59)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = call i32 @x_perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %65
65: ; preds = %63, %55
%66 = load i32, i32* %10
%67 = call i32 @write(i32 1, i8* %45, i32 %66)
%68 = load i32, i32* %10
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @x_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %70, %65
%73 = load i32, i32* %10
%74 = sext i32 %73 to i64
%75 = load i64, i64* %7
%76 = sub i64 %75, %74
store i64 %76, i64* %7
%77 = load i8*, i8** %8
br label %38
78: ; preds = %38
%79 = load i32*, i32** %6
%80 = call i32 @fclose(i32* %79)
br label %84
81: ; preds = %25
%82 = load i32, i32* @errno
%83 = call i32 @send_errno(i32 %82)
br label %84
84: ; preds = %81, %78
br label %88
85: ; preds = %20, %2
%86 = load i32, i32* @EISDIR
%87 = call i32 @send_errno(i32 %86)
br label %88
88: ; preds = %85, %84
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [2 x i8] c"r\00"
@BUFSIZ = global i32 0
@.str.1 = [9 x i8] c"get-read\00"
@.str.2 = [10 x i8] c"get-write\00"
@errno = global i32 0
@EISDIR = global i32 0
define void @do_get(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = bitcast %struct.stat* %3 to i8*
%8 = load i8*, i8** %1
%9 = call i64 @stat(i8* %8, %struct.stat* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %55
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i64 @S_ISREG(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %55, label %16
16: ; preds = %11
%17 = load i8*, i8** %1
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %52, label %20
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = call i32 (...) @send_success()
%24 = call i32 @send_long(i64 %22)
%25 = icmp eq i64 %22, 0
br i1 %25, label %50, label %26
26: ; preds = %20, %46
%27 = phi i64 [ %48, %46 ], [ %22, %20 ]
%28 = load i32, i32* @BUFSIZ
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
%31 = alloca i8, i64 %29
%32 = load i32, i32* @BUFSIZ
%33 = sext i32 %32 to i64
%34 = icmp ugt i64 %27, %33
%35 = select i1 %34, i64 %33, i64 %27
%36 = trunc i64 %35 to i32
%37 = call i32 @fread(i8* %31, i32 1, i32 %36, i32* %18)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %26
%40 = call i32 @x_perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %26
%42 = call i32 @write(i32 1, i8* %31, i32 %37)
%43 = icmp eq i32 %42, %37
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i32 @x_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
%47 = sext i32 %37 to i64
%48 = sub i64 %27, %47
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %26
50: ; preds = %46, %20
%51 = call i32 @fclose(i32* %18)
br label %58
52: ; preds = %16
%53 = load i32, i32* @errno
%54 = call i32 @send_errno(i32 %53)
br label %58
55: ; preds = %11, %2
%56 = load i32, i32* @EISDIR
%57 = call i32 @send_errno(i32 %56)
br label %58
58: ; preds = %50, %52, %55
ret void
}