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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = 0, ret = 0, i = 0;
  char buff[10] = "";
  if (argc != 3) {
    printf("Usage: %s cmd(1/2) arg\n", argv[0]);
    exit(0);
  }
  int cmd = atoi(argv[1]);
  int arg = atoi(argv[2]);
  fd = open("/dev/05_char_list", O_RDWR);
  printf("fd = %d\n", fd);
  if (fd > 0) {
    if (cmd == 1) {
      for (i = 0; i < sizeof(buff); i++)
        buff[i] = arg;
      ret = write(fd, buff, sizeof(buff));
      printf("Writing %d bytes\n", ret);
    } else if (cmd == 2) {
      ret = read(fd, buff, sizeof(buff));
      printf("Reading %d bytes\n", ret);
      for (i = 0; i < sizeof(buff); i++) {
        if (buff[i] != arg) {
          printf("Invalid data found: %d %d %d %d %d\n", buff[0], buff[1],
                 buff[2], buff[3], buff[4]);
          close(fd);
          exit(-1);
        }
      }
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x26(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
cmpl   $0x3,-0x8(%rbp)
je     11e1 <main+0x61>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1349 <main+0x1c9>
cmpl   $0x1,-0x2c(%rbp)
jne    1298 <main+0x118>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0xa,%rax
jae    126e <main+0xee>
mov    -0x30(%rbp),%eax
mov    %al,%cl
movslq -0x1c(%rbp),%rax
mov    %cl,-0x26(%rbp,%rax,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1245 <main+0xc5>
mov    -0x14(%rbp),%edi
lea    -0x26(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1341 <main+0x1c1>
cmpl   $0x2,-0x2c(%rbp)
jne    133c <main+0x1bc>
mov    -0x14(%rbp),%edi
lea    -0x26(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0xa,%rax
jae    1337 <main+0x1b7>
movslq -0x1c(%rbp),%rax
movsbl -0x26(%rbp,%rax,1),%eax
cmp    -0x30(%rbp),%eax
je     1324 <main+0x1a4>
movsbl -0x26(%rbp),%esi
movsbl -0x25(%rbp),%edx
movsbl -0x24(%rbp),%ecx
movsbl -0x23(%rbp),%r8d
movsbl -0x22(%rbp),%r9d
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1329 <main+0x1a9>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12ce <main+0x14e>
jmp    133c <main+0x1bc>
jmp    1341 <main+0x1c1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movw   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
cmp    $0x3,%edi
jne    12aa <main+0x13a>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe62(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jle    129f <main+0x12f>
cmp    $0x2,%ebp
je     1223 <main+0xb3>
cmp    $0x1,%ebp
jne    1298 <main+0x128>
movzbl %r14b,%eax
movabs $0x101010101010101,%rcx
01 01 01
imul   %rax,%rcx
mov    %rcx,(%rsp)
mov    %cx,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1030 <write@plt>
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1298 <main+0x128>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1060 <read@plt>
lea    0xe0c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movq   (%rsp),%xmm0
punpcklbw %xmm0,%xmm1
punpcklwd %xmm1,%xmm0
psrad  $0x18,%xmm0
punpckhwd %xmm1,%xmm1
psrad  $0x18,%xmm1
movd   %r14d,%xmm2
pshufd $0x0,%xmm2,%xmm2
pcmpeqd %xmm2,%xmm1
pcmpeqd %xmm0,%xmm2
packssdw %xmm1,%xmm2
psllw  $0xf,%xmm2
packsswb %xmm2,%xmm2
pmovmskb %xmm2,%eax
cmp    $0xff,%al
jne    12c2 <main+0x152>
movsbl 0x8(%rsp),%eax
cmp    %eax,%r14d
jne    12c2 <main+0x152>
movsbl 0x9(%rsp),%eax
cmp    %eax,%r14d
jne    12c2 <main+0x152>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd4c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
movsbl 0x2(%rsp),%ecx
movsbl 0x3(%rsp),%r8d
movsbl 0x4(%rsp),%r9d
movd   %xmm0,%esi
pshufd $0x55,%xmm0,%xmm0
movd   %xmm0,%edx
lea    0xd70(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Usage: %s cmd(1/2) arg\0A\00"
@.str.1 = [18 x i8] c"/dev/05_char_list\00"
@O_RDWR = global i32 0
@.str.2 = [9 x i8] c"fd = %d\0A\00"
@.str.3 = [18 x i8] c"Writing %d bytes\0A\00"
@.str.4 = [18 x i8] c"Reading %d bytes\0A\00"
@.str.5 = [36 x i8] c"Invalid data found: %d %d %d %d %d\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 [10 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%12 = bitcast [10 x i8]* %9 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %10
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %11
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %6
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %109
36: ; preds = %21
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %59
39: ; preds = %36
store i32 0, i32* %8
br label %40
40: ; preds = %50, %39
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = icmp ult i64 %42, 10
br i1 %43, label %44, label %53
44: ; preds = %40
%45 = load i32, i32* %11
%46 = trunc i32 %45 to i8
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 %48
store i8 %46, i8* %49
br label %50
50: ; preds = %44
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %40
53: ; preds = %40
%54 = load i32, i32* %6
%55 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%56 = call i32 @write(i32 %54, i8* %55, i32 10)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %106
59: ; preds = %36
%60 = load i32, i32* %10
%61 = icmp eq i32 %60, 2
br i1 %61, label %62, label %105
62: ; preds = %59
%63 = load i32, i32* %6
%64 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%65 = call i32 @read(i32 %63, i8* %64, i32 10)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %66)
store i32 0, i32* %8
br label %68
68: ; preds = %101, %62
%69 = load i32, i32* %8
%70 = sext i32 %69 to i64
%71 = icmp ult i64 %70, 10
br i1 %71, label %72, label %104
72: ; preds = %68
%73 = load i32, i32* %8
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 %74
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = load i32, i32* %11
%79 = icmp ne i32 %77, %78
br i1 %79, label %80, label %100
80: ; preds = %72
%81 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%82 = load i8, i8* %81
%83 = sext i8 %82 to i32
%84 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 1
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 2
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 3
%91 = load i8, i8* %90
%92 = sext i8 %91 to i32
%93 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 4
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %83, i32 %86, i32 %89, i32 %92, i32 %95)
%97 = load i32, i32* %6
%98 = call i32 @close(i32 %97)
%99 = call i32 @exit(i32 -1)
unreachable
100: ; preds = %72
br label %101
101: ; preds = %100
%102 = load i32, i32* %8
%103 = add nsw i32 %102, 1
store i32 %103, i32* %8
br label %68
104: ; preds = %68
br label %105
105: ; preds = %104, %59
br label %106
106: ; preds = %105, %53
%107 = load i32, i32* %6
%108 = call i32 @close(i32 %107)
br label %109
109: ; preds = %106, %21
%110 = load i32, i32* %3
ret i32 %110
}

LLVM IR (-O2):
@.str = [24 x i8] c"Usage: %s cmd(1/2) arg\0A\00"
@.str.1 = [18 x i8] c"/dev/05_char_list\00"
@O_RDWR = global i32 0
@.str.2 = [9 x i8] c"fd = %d\0A\00"
@.str.3 = [18 x i8] c"Writing %d bytes\0A\00"
@.str.4 = [18 x i8] c"Reading %d bytes\0A\00"
@.str.5 = [36 x i8] c"Invalid data found: %d %d %d %d %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32 %0, 3
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = icmp sgt i32 %19, 0
br i1 %21, label %22, label %66
22: ; preds = %11
switch i32 %14, label %64 [
i32 1, label %23
i32 2, label %27
]
23: ; preds = %22
%24 = trunc i32 %17 to i8
%25 = call i32 @write(i32 %19, i8* %5, i32 10)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %64
27: ; preds = %22
%28 = call i32 @read(i32 %19, i8* %5, i32 10)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = bitcast [10 x i8]* %3 to <8 x i8>*
%31 = load <8 x i8>, <8 x i8>* %30
%32 = sext <8 x i8> %31 to <8 x i32>
%33 = insertelement <8 x i32> poison, i32 %17, i64 0
%34 = shufflevector <8 x i32> %33, <8 x i32> poison, <8 x i32> zeroinitializer
%35 = icmp eq <8 x i32> %34, %32
%36 = freeze <8 x i1> %35
%37 = bitcast <8 x i1> %36 to i8
%38 = icmp eq i8 %37, -1
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 8
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %17, %41
%43 = select i1 %38, i1 %42, i1 false
%44 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 9
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %17, %46
%48 = select i1 %43, i1 %47, i1 false
br i1 %48, label %64, label %49
49: ; preds = %27
%50 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 2
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 3
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 4
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = extractelement <8 x i32> %32, i64 0
%60 = extractelement <8 x i32> %32, i64 1
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %59, i32 %60, i32 %52, i32 %55, i32 %58)
%62 = tail call i32 @close(i32 %19)
%63 = tail call i32 @exit(i32 -1)
unreachable
64: ; preds = %27, %22, %23
%65 = tail call i32 @close(i32 %19)
br label %66
66: ; preds = %64, %11
ret i32 0
}