#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
void checksys() {
  char *memptr[512];
  int fd[256];
  const char file[] = "/dev/null";
  int i;
  for (i = 0; i < 512; i++) {
    memptr[i] = (char *)malloc(1024 * 1024);
    if (memptr[i] == NULL)
      break;
  }
  if (i >= 512) {
    printf("checksys: memory: > %d MB\n", 512);
  } else {
    printf("checksys: memory: %d MB\n", i);
  }
  for (i--; i >= 0; i--) {
    free(memptr[i]);
  }
  for (i = 0; i < 256; i++) {
    fd[i] = open(file, O_RDONLY);
    if (fd[i] < 0)
      break;
  }
  if (i >= 256) {
    printf("checksys: file descriptors: > %d\n", 256);
  } else {
    printf("checksys: file descriptors: %d\n", i);
  }
  for (i--; i >= 0; i--) {
    close(fd[i]);
  }
  return;
}


x86 assembly (clang, -O0):
<checksys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1410,%rsp
mov    0xe9e(%rip),%rax
mov    %rax,-0x140a(%rbp)
mov    0xe98(%rip),%ax
mov    %ax,-0x1402(%rbp)
movl   $0x0,-0x1410(%rbp)
cmpl   $0x200,-0x1410(%rbp)
jge    11e1 <checksys+0x91>
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
movslq -0x1410(%rbp),%rax
mov    %rcx,-0x1000(%rbp,%rax,8)
ff
movslq -0x1410(%rbp),%rax
cmpq   $0x0,-0x1000(%rbp,%rax,8)
jne    11c8 <checksys+0x78>
jmp    11e1 <checksys+0x91>
jmp    11cd <checksys+0x7d>
mov    -0x1410(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1410(%rbp)
jmp    1181 <checksys+0x31>
cmpl   $0x200,-0x1410(%rbp)
jl     1207 <checksys+0xb7>
lea    0xe12(%rip),%rdi
mov    $0x200,%esi
call   1040 <printf@plt>
jmp    1219 <checksys+0xc9>
mov    -0x1410(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1410(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1410(%rbp)
cmpl   $0x0,-0x1410(%rbp)
jl     125d <checksys+0x10d>
movslq -0x1410(%rbp),%rax
mov    -0x1000(%rbp,%rax,8),%rdi
ff
call   1030 <free@plt>
mov    -0x1410(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1410(%rbp)
jmp    1228 <checksys+0xd8>
movl   $0x0,-0x1410(%rbp)
cmpl   $0x100,-0x1410(%rbp)
jge    12cf <checksys+0x17f>
lea    -0x140a(%rbp),%rdi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
movslq -0x1410(%rbp),%rax
mov    %ecx,-0x1400(%rbp,%rax,4)
movslq -0x1410(%rbp),%rax
cmpl   $0x0,-0x1400(%rbp,%rax,4)
jge    12b6 <checksys+0x166>
jmp    12cf <checksys+0x17f>
jmp    12bb <checksys+0x16b>
mov    -0x1410(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1410(%rbp)
jmp    1267 <checksys+0x117>
cmpl   $0x100,-0x1410(%rbp)
jl     12f5 <checksys+0x1a5>
lea    0xd58(%rip),%rdi
mov    $0x100,%esi
call   1040 <printf@plt>
jmp    1307 <checksys+0x1b7>
mov    -0x1410(%rbp),%esi
lea    0xd5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1410(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1410(%rbp)
cmpl   $0x0,-0x1410(%rbp)
jl     134a <checksys+0x1fa>
movslq -0x1410(%rbp),%rax
mov    -0x1400(%rbp,%rax,4),%edi
call   1050 <close@plt>
mov    -0x1410(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1410(%rbp)
jmp    1316 <checksys+0x1c6>
add    $0x1410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksys>:
push   %r15
push   %r14
push   %rbx
sub    $0x1410,%rsp
movabs $0x6c756e2f7665642f,%rax
6e 75 6c
mov    %rax,(%rsp)
movw   $0x6c,0x8(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,0x410(%rsp,%rbx,8)
test   %rax,%rax
je     11bc <checksys+0x6c>
add    $0x1,%rbx
cmp    $0x200,%rbx
jne    1180 <checksys+0x30>
lea    0xe5f(%rip),%rdi
mov    $0x200,%esi
call   1040 <printf@plt>
mov    $0x200,%ebx
jmp    11ce <checksys+0x7e>
lea    0xe62(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
jle    11f7 <checksys+0xa7>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x400(%rsp,%rbx,8),%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     11e0 <checksys+0x90>
xor    %ebx,%ebx
mov    0x2de8(%rip),%r15
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,0x10(%rsp,%rbx,4)
test   %eax,%eax
js     1248 <checksys+0xf8>
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    1210 <checksys+0xc0>
lea    0xe07(%rip),%rdi
mov    $0x100,%esi
call   1040 <printf@plt>
mov    $0x100,%ebx
jmp    125a <checksys+0x10a>
lea    0xe11(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
jle    1273 <checksys+0x123>
add    $0x1,%rbx
xchg   %ax,%ax
mov    0x8(%rsp,%rbx,4),%edi
call   1050 <close@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1260 <checksys+0x110>
add    $0x1410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.checksys.file = [10 x i8] c"/dev/null\00"
@.str = [27 x i8] c"checksys: memory: > %d MB\0A\00"
@.str.1 = [25 x i8] c"checksys: memory: %d MB\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [34 x i8] c"checksys: file descriptors: > %d\0A\00"
@.str.3 = [32 x i8] c"checksys: file descriptors: %d\0A\00"
define void @checksys() {
%1 = alloca [512 x i8*]
%2 = alloca [256 x i32]
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = bitcast [10 x i8]* %3 to i8*
store i32 0, i32* %4
br label %6
6: ; preds = %22, %0
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 512
br i1 %8, label %9, label %25
9: ; preds = %6
%10 = call i64 @malloc(i32 1048576)
%11 = inttoptr i64 %10 to i8*
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [512 x i8*], [512 x i8*]* %1, i64 0, i64 %13
store i8* %11, i8** %14
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [512 x i8*], [512 x i8*]* %1, i64 0, i64 %16
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %9
br label %25
21: ; preds = %9
br label %22
22: ; preds = %21
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %6
25: ; preds = %20, %6
%26 = load i32, i32* %4
%27 = icmp sge i32 %26, 512
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 512)
br label %33
30: ; preds = %25
%31 = load i32, i32* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %30, %28
%34 = load i32, i32* %4
%35 = add nsw i32 %34, -1
store i32 %35, i32* %4
br label %36
36: ; preds = %45, %33
%37 = load i32, i32* %4
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [512 x i8*], [512 x i8*]* %1, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %39
%46 = load i32, i32* %4
%47 = add nsw i32 %46, -1
store i32 %47, i32* %4
br label %36
48: ; preds = %36
store i32 0, i32* %4
br label %49
49: ; preds = %66, %48
%50 = load i32, i32* %4
%51 = icmp slt i32 %50, 256
br i1 %51, label %52, label %69
52: ; preds = %49
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%54 = load i32, i32* @O_RDONLY
%55 = call i32 @open(i8* %53, i32 %54)
%56 = load i32, i32* %4
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %57
store i32 %55, i32* %58
%59 = load i32, i32* %4
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %60
%62 = load i32, i32* %61
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %52
br label %69
65: ; preds = %52
br label %66
66: ; preds = %65
%67 = load i32, i32* %4
%68 = add nsw i32 %67, 1
store i32 %68, i32* %4
br label %49
69: ; preds = %64, %49
%70 = load i32, i32* %4
%71 = icmp sge i32 %70, 256
br i1 %71, label %72, label %74
72: ; preds = %69
%73 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 256)
br label %77
74: ; preds = %69
%75 = load i32, i32* %4
%76 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %75)
br label %77
77: ; preds = %74, %72
%78 = load i32, i32* %4
%79 = add nsw i32 %78, -1
store i32 %79, i32* %4
br label %80
80: ; preds = %89, %77
%81 = load i32, i32* %4
%82 = icmp sge i32 %81, 0
br i1 %82, label %83, label %92
83: ; preds = %80
%84 = load i32, i32* %4
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %85
%87 = load i32, i32* %86
%88 = call i32 @close(i32 %87)
br label %89
89: ; preds = %83
%90 = load i32, i32* %4
%91 = add nsw i32 %90, -1
store i32 %91, i32* %4
br label %80
92: ; preds = %80
ret void
}

LLVM IR (-O2):
@__const.checksys.file = [10 x i8] c"/dev/null\00"
@.str = [27 x i8] c"checksys: memory: > %d MB\0A\00"
@.str.1 = [25 x i8] c"checksys: memory: %d MB\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [34 x i8] c"checksys: file descriptors: > %d\0A\00"
@.str.3 = [32 x i8] c"checksys: file descriptors: %d\0A\00"
define void @checksys() {
%1 = alloca [512 x i8*]
%2 = alloca [256 x i32]
%3 = alloca [10 x i8]
%4 = bitcast [512 x i8*]* %1 to i8*
%5 = bitcast [256 x i32]* %2 to i8*
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
br label %7
7: ; preds = %0, %13
%8 = phi i64 [ 0, %0 ], [ %14, %13 ]
%9 = tail call i64 @malloc(i32 1048576)
%10 = inttoptr i64 %9 to i8*
%11 = getelementptr inbounds [512 x i8*], [512 x i8*]* %1, i64 0, i64 %8
store i8* %10, i8** %11
%12 = icmp eq i64 %9, 0
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = add nuw nsw i64 %8, 1
%15 = icmp eq i64 %14, 512
br i1 %15, label %16, label %7
16: ; preds = %13
%17 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 512)
br label %22
18: ; preds = %7
%19 = trunc i64 %8 to i32
%20 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp sgt i32 %19, 0
br i1 %21, label %22, label %32
22: ; preds = %16, %18
%23 = phi i64 [ 512, %16 ], [ %8, %18 ]
%24 = and i64 %23, 4294967295
br label %25
25: ; preds = %22, %25
%26 = phi i64 [ %24, %22 ], [ %27, %25 ]
%27 = add nsw i64 %26, -1
%28 = getelementptr inbounds [512 x i8*], [512 x i8*]* %1, i64 0, i64 %27
%29 = load i8*, i8** %28
%30 = tail call i32 @free(i8* %29)
%31 = icmp ugt i64 %26, 1
br i1 %31, label %25, label %32
32: ; preds = %25, %18
br label %33
33: ; preds = %32, %39
%34 = phi i64 [ %40, %39 ], [ 0, %32 ]
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %6, i32 %35)
%37 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %34
store i32 %36, i32* %37
%38 = icmp slt i32 %36, 0
br i1 %38, label %44, label %39
39: ; preds = %33
%40 = add nuw nsw i64 %34, 1
%41 = icmp eq i64 %40, 256
br i1 %41, label %42, label %33
42: ; preds = %39
%43 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 256)
br label %48
44: ; preds = %33
%45 = trunc i64 %34 to i32
%46 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = icmp sgt i32 %45, 0
br i1 %47, label %48, label %58
48: ; preds = %42, %44
%49 = phi i64 [ 256, %42 ], [ %34, %44 ]
%50 = and i64 %49, 4294967295
br label %51
51: ; preds = %48, %51
%52 = phi i64 [ %50, %48 ], [ %53, %51 ]
%53 = add nsw i64 %52, -1
%54 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %53
%55 = load i32, i32* %54
%56 = tail call i32 @close(i32 %55)
%57 = icmp ugt i64 %52, 1
br i1 %57, label %51, label %58
58: ; preds = %51, %44
ret void
}