#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 */
scalar_t__ EINVAL;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SG_SET_RESERVED_SIZE;
int /*<<< orphan*/ SG_SET_TIMEOUT;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char **, int);
int main(int argc, char **argv) {
  int fd;
  int rc;
  int rsz = 131072;
  int tout = 10800000;
  char buf[42] = {0};
  if (argc != 2) {
    printf("usage: %s /dev/sgX\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  rc = ioctl(fd, SG_SET_RESERVED_SIZE, &rsz);
  if (rc < 0) {
    perror("ioctl SG_SET_RESERVED_SIZE");
    goto out_close;
  }
  rc = ioctl(fd, SG_SET_TIMEOUT, &tout);
  if (rc < 0) {
    perror("ioctl SG_SET_TIMEOUT");
    goto out_close;
  }
  buf[4] = 'H';
  rc = write(fd, &buf, sizeof(buf));
  if (rc < 0) {
    perror("write");
    if (errno == EINVAL)
      printf("FAIL\n");
    goto out_close;
  }
  printf("PASS\n");
  rc = 0;
out_close:
  close(fd);
  return (rc ? 1 : 0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x20000,-0x1c(%rbp)
movl   $0xa4cb80,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x2a,%edx
call   1050 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
je     11cf <main+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120a <main+0x9a>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x14(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    123d <main+0xcd>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
jmp    12e0 <main+0x170>
mov    -0x14(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1270 <main+0x100>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
jmp    12e0 <main+0x170>
movb   $0x48,-0x4c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x2a,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12cb <main+0x15b>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%rax
mov    0x2d11(%rip),%rcx
cmp    (%rcx),%rax
jne    12c6 <main+0x156>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12e0 <main+0x170>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
movl   $0x20000,0xc(%rsp)
movl   $0xa4cb80,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x2a(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
cmp    $0x2,%edi
jne    121c <main+0xac>
mov    0x8(%rsi),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     123b <main+0xcb>
mov    %eax,%ebx
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1249 <main+0xd9>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1252 <main+0xe2>
movb   $0x48,0x14(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2a,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1265 <main+0xf5>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1232 <main+0xc2>
mov    (%rsi),%rsi
lea    0xdda(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
jmp    122d <main+0xbd>
lea    0xdc9(%rip),%rdi
jmp    1259 <main+0xe9>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebp
jmp    1213 <main+0xa3>
lea    0xddd(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%rax
mov    0x2d3e(%rip),%rcx
mov    $0x1,%ebp
cmp    (%rcx),%rax
jne    1213 <main+0xa3>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
jmp    1213 <main+0xa3>

LLVM IR (-O0):
@.str = [20 x i8] c"usage: %s /dev/sgX\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SG_SET_RESERVED_SIZE = global i32 0
@.str.2 = [27 x i8] c"ioctl SG_SET_RESERVED_SIZE\00"
@SG_SET_TIMEOUT = global i32 0
@.str.3 = [21 x i8] c"ioctl SG_SET_TIMEOUT\00"
@.str.4 = [6 x i8] c"write\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.5 = [6 x i8] c"FAIL\0A\00"
@.str.6 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [42 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 131072, i32* %8
store i32 10800000, i32* %9
%11 = bitcast [42 x i8]* %10 to i8*
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %69
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %69
29: ; preds = %19
%30 = load i32, i32* %6
%31 = load i32, i32* @SG_SET_RESERVED_SIZE
%32 = call i32 @ioctl(i32 %30, i32 %31, i32* %8)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %62
37: ; preds = %29
%38 = load i32, i32* %6
%39 = load i32, i32* @SG_SET_TIMEOUT
%40 = call i32 @ioctl(i32 %38, i32 %39, i32* %9)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %62
45: ; preds = %37
%46 = getelementptr inbounds [42 x i8], [42 x i8]* %10, i64 0, i64 4
store i8 72, i8* %46
%47 = load i32, i32* %6
%48 = bitcast [42 x i8]* %10 to i8**
%49 = call i32 @write(i32 %47, i8** %48, i32 42)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %45
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%54 = load i64, i64* @errno
%55 = load i64, i64* @EINVAL
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %52
br label %62
60: ; preds = %45
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %7
br label %62
62: ; preds = %60, %59, %43, %35
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %7
%66 = icmp ne i32 %65, 0
%67 = zext i1 %66 to i64
%68 = select i1 %66, i32 1, i32 0
store i32 %68, i32* %3
br label %69
69: ; preds = %62, %27, %14
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [20 x i8] c"usage: %s /dev/sgX\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SG_SET_RESERVED_SIZE = global i32 0
@.str.2 = [27 x i8] c"ioctl SG_SET_RESERVED_SIZE\00"
@SG_SET_TIMEOUT = global i32 0
@.str.3 = [21 x i8] c"ioctl SG_SET_TIMEOUT\00"
@.str.4 = [6 x i8] c"write\00"
@errno = global i64 0
@EINVAL = global i64 0
@str = [5 x i8] c"PASS\00"
@str.7 = [5 x i8] c"FAIL\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [42 x i8]
%6 = bitcast i32* %3 to i8*
store i32 131072, i32* %3
%7 = bitcast i32* %4 to i8*
store i32 10800000, i32* %4
%8 = getelementptr inbounds [42 x i8], [42 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %50
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* %15, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %50
21: ; preds = %13
%22 = load i32, i32* @SG_SET_RESERVED_SIZE
%23 = call i32 @ioctl(i32 %17, i32 %22, i32* %3)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %47
27: ; preds = %21
%28 = load i32, i32* @SG_SET_TIMEOUT
%29 = call i32 @ioctl(i32 %17, i32 %28, i32* %4)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %47
33: ; preds = %27
%34 = getelementptr inbounds [42 x i8], [42 x i8]* %5, i64 0, i64 4
store i8 72, i8* %34
%35 = bitcast [42 x i8]* %5 to i8**
%36 = call i32 @write(i32 %17, i8** %35, i32 42)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINVAL
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.7, i64 0, i64 0))
br label %47
45: ; preds = %33
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %47
47: ; preds = %38, %43, %45, %31, %25
%48 = phi i32 [ 1, %38 ], [ 1, %43 ], [ 0, %45 ], [ 1, %31 ], [ 1, %25 ]
%49 = call i32 @close(i32 %17)
br label %50
50: ; preds = %47, %19, %10
%51 = phi i32 [ 1, %10 ], [ 1, %19 ], [ %48, %47 ]
ret i32 %51
}