#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int send_ca(void) {
  char buf[4048];
  int nbytes;
  int fd = open("ca-cert.pem", O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Config error: can't find ca-cert.pem");
    return 1;
  }
  while (0 < (nbytes = read(fd, buf, sizeof(buf)))) {
    if (nbytes != write(1, buf, nbytes)) {
      fprintf(stderr, "Write error: can't send ca-cert.pem");
      close(fd);
      return 1;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<send_ca>:
push   %rbp
mov    %rsp,%rbp
sub    $0xff0,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xed2(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xfe8(%rbp)
cmpl   $0x0,-0xfe8(%rbp)
jge    11a4 <send_ca+0x54>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <send_ca+0xf8>
jmp    11a9 <send_ca+0x59>
mov    -0xfe8(%rbp),%edi
lea    -0xfe0(%rbp),%rsi
mov    $0xfd0,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0xfe4(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    1236 <send_ca+0xe6>
mov    -0xfe4(%rbp),%eax
mov    %eax,-0xfec(%rbp)
lea    -0xfe0(%rbp),%rsi
mov    -0xfe4(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xfec(%rbp),%eax
cmp    %ecx,%eax
je     1231 <send_ca+0xe1>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0xfe8(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <send_ca+0xf8>
jmp    11a9 <send_ca+0x59>
mov    -0xfe8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xff0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_ca>:
push   %rbp
push   %r14
push   %rbx
sub    $0xfd0,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xed2(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11cd <send_ca+0x7d>
mov    %eax,%r14d
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xfd0,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e9 <send_ca+0x99>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
je     1180 <send_ca+0x30>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0x1,%eax
jmp    11f3 <send_ca+0xa3>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    11f3 <send_ca+0xa3>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0xfd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"ca-cert.pem\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"Config error: can't find ca-cert.pem\00"
@.str.2 = [36 x i8] c"Write error: can't send ca-cert.pem\00"
define i32 @send_ca() {
%1 = alloca i32
%2 = alloca [4048 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %33
12: ; preds = %0
br label %13
13: ; preds = %29, %12
%14 = load i32, i32* %4
%15 = getelementptr inbounds [4048 x i8], [4048 x i8]* %2, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 4048)
store i32 %16, i32* %3
%17 = icmp slt i32 0, %16
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = load i32, i32* %3
%20 = getelementptr inbounds [4048 x i8], [4048 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* %3
%22 = call i32 @write(i32 1, i8* %20, i32 %21)
%23 = icmp ne i32 %19, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
store i32 1, i32* %1
br label %33
29: ; preds = %18
br label %13
30: ; preds = %13
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
store i32 0, i32* %1
br label %33
33: ; preds = %30, %24, %9
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [12 x i8] c"ca-cert.pem\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"Config error: can't find ca-cert.pem\00"
@.str.2 = [36 x i8] c"Write error: can't send ca-cert.pem\00"
define i32 @send_ca() {
%1 = alloca [4048 x i8]
%2 = getelementptr inbounds [4048 x i8], [4048 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %21
9: ; preds = %0, %12
%10 = call i32 @read(i32 %4, i8* %2, i32 4048)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = call i32 @write(i32 1, i8* %2, i32 %10)
%14 = icmp eq i32 %10, %13
br i1 %14, label %9, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @close(i32 %4)
br label %21
19: ; preds = %9
%20 = tail call i32 @close(i32 %4)
br label %21
21: ; preds = %19, %15, %6
%22 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 0, %19 ]
ret i32 %22
}