#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*/ sampling_freq;
typedef int /*<<< orphan*/ num_samples;
typedef int /*<<< orphan*/ channel_mask;

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int count_channels(char);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strtol(char *, char **, int);
scalar_t__ write(int, ...);
int main(int argc, char **argv) {
  if (argc != 4) {
    fprintf(stderr, "Usage: sample CHANNEL_MASK SAMPLING_FREQ NUM_SAMPLES\n");
    fprintf(stderr,
            "Usage: takes NUM_SAMPLES at SAMPLING_FREQ Hz from CHANNEL_MASK\n");
    exit(1);
  }
  char channel_mask;
  int num_samples, sampling_freq;
  char *endptr;
  channel_mask = (char)strtol(argv[1], &endptr, 2);
  if (*endptr != '\0') {
    fprintf(stderr, "ERROR: first argument must be a binary of 8 bits\n");
    exit(1);
  }
  sampling_freq = (int)strtol(argv[2], &endptr, 10);
  if (*endptr != '\0') {
    fprintf(stderr, "ERROR: second argument must be a positive integer\n");
    exit(1);
  } else if (sampling_freq <= 0 || sampling_freq > 44100) {
    fprintf(stderr,
            "ERROR: sampling frequency must be between 1 and 44100 Hz\n");
    exit(1);
  }
  num_samples = (int)strtol(argv[3], &endptr, 10);
  if (*endptr != '\0' || num_samples < 0) {
    fprintf(stderr, "ERROR: third argument must be a non-negative integer\n");
    exit(1);
  }
  fprintf(stderr,
          "Channel mask: %d\nSampling frequency: %d\nNumber of samples: %d\n",
          channel_mask, sampling_freq, num_samples);
  int fd;
  char byte;
  int bytes_read = 0;
  int i = 0;
  int num_channels = count_channels(channel_mask);
  fd = open("/dev/ttyS101", O_RDWR | O_NOCTTY);
  if (fd == -1) {
    perror("Unable to open /dev/S101");
    exit(1);
  }
  fcntl(fd, F_SETFL, 0);
  if (write(fd, &channel_mask, sizeof(channel_mask)) < 0) {
    perror("Could not write");
    exit(1);
  }
  if (write(fd, &sampling_freq, sizeof(sampling_freq)) < 0) {
    perror("Could not write");
    exit(1);
  }
  if (write(fd, &num_samples, sizeof(num_samples)) < 0) {
    perror("Could not write");
    exit(1);
  }
  while (!num_samples || bytes_read < num_samples * 2 * num_channels) {
    if ((i = read(fd, &byte, 1)) < 0) {
      perror("Could not read");
      fprintf(stderr, "%d\n", i);
      close(fd);
      exit(1);
    }
    bytes_read = bytes_read + i;
    write(1, &byte, i);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11f8 <main+0x58>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x2,%edx
call   1080 <strtol@plt>
mov    %al,-0x11(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1242 <main+0xa2>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     128c <main+0xec>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    12a3 <main+0x103>
cmpl   $0xac44,-0x1c(%rbp)
jle    12c4 <main+0x124>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12c9 <main+0x129>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12fc <main+0x15c>
cmpl   $0x0,-0x18(%rbp)
jge    131d <main+0x17d>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
movsbl -0x11(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movsbl -0x11(%rbp),%edi
call   1090 <count_channels@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2c88(%rip),%rax
mov    (%rax),%esi
mov    0x2c6f(%rip),%rax
or     (%rax),%esi
lea    0xe18(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    139a <main+0x1fa>
lea    0xe0c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    0x2c24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <write@plt>
cmp    $0x0,%rax
jge    13e0 <main+0x240>
lea    0xddf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1413 <main+0x273>
lea    0xdac(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1446 <main+0x2a6>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    144b <main+0x2ab>
mov    $0x1,%al
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x3d(%rbp)
je     146f <main+0x2cf>
mov    -0x34(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
imul   -0x3c(%rbp),%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    147f <main+0x2df>
jmp    14f5 <main+0x355>
mov    -0x2c(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    14d4 <main+0x334>
lea    0xd1d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2b21(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%edx
lea    0xccb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edx
mov    $0x1,%edi
lea    -0x2d(%rbp),%rsi
mov    $0x0,%al
call   1040 <write@plt>
jmp    144b <main+0x2ab>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x4,%edi
jne    13bd <main+0x21d>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0x18(%rsp),%rsi
mov    $0x2,%edx
call   1080 <strtol@plt>
mov    %rax,%r14
mov    %r14b,0xf(%rsp)
mov    0x18(%rsp),%rax
cmpb   $0x0,(%rax)
jne    13df <main+0x23f>
mov    0x10(%rbp),%rdi
lea    0x18(%rsp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%rbx
mov    %ebx,0x24(%rsp)
mov    0x18(%rsp),%rax
cmpb   $0x0,(%rax)
jne    13f1 <main+0x251>
lea    -0xac45(%rbx),%eax
cmp    $0xffff53bb,%eax
jbe    1403 <main+0x263>
mov    0x18(%rbp),%rdi
lea    0x18(%rsp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,0x10(%rsp)
mov    0x18(%rsp),%rcx
cmpb   $0x0,(%rcx)
mov    0x2d8e(%rip),%r12
mov    (%r12),%edi
jne    1415 <main+0x275>
test   %eax,%eax
js     1415 <main+0x275>
movsbl %r14b,%ebp
lea    0xeec(%rip),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1090 <count_channels@plt>
mov    %eax,%r14d
mov    0x2d6a(%rip),%rax
mov    0x2d53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xefa(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     142d <main+0x28d>
mov    %eax,%r15d
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <write@plt>
test   %rax,%rax
js     13b4 <main+0x214>
lea    0x24(%rsp),%rsi
mov    $0x4,%edx
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <write@plt>
test   %rax,%rax
js     13b4 <main+0x214>
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <write@plt>
test   %rax,%rax
js     13b4 <main+0x214>
add    %r14d,%r14d
mov    0x10(%rsp),%eax
test   %eax,%eax
je     131e <main+0x17e>
imul   %r14d,%eax
test   %eax,%eax
jle    1369 <main+0x1c9>
lea    0x17(%rsp),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1382 <main+0x1e2>
add    %ebx,%ebp
mov    $0x1,%edi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    0x10(%rsp),%eax
test   %eax,%eax
je     1330 <main+0x190>
imul   %r14d,%eax
cmp    %eax,%ebp
jl     1330 <main+0x190>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe37(%rip),%rdi
call   10b0 <perror@plt>
mov    (%r12),%edi
lea    0xded(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdf5(%rip),%rdi
jmp    1434 <main+0x294>
mov    0x2c0c(%rip),%rbx
mov    (%rbx),%edi
lea    0xc33(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xc59(%rip),%rsi
jmp    141c <main+0x27c>
mov    0x2bea(%rip),%rax
mov    (%rax),%edi
lea    0xc87(%rip),%rsi
jmp    141c <main+0x27c>
mov    0x2bd8(%rip),%rax
mov    (%rax),%edi
lea    0xca7(%rip),%rsi
jmp    141c <main+0x27c>
mov    0x2bc6(%rip),%rax
mov    (%rax),%edi
lea    0xcc8(%rip),%rsi
jmp    141c <main+0x27c>
lea    0xcf9(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd63(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"Usage: sample CHANNEL_MASK SAMPLING_FREQ NUM_SAMPLES\0A\00"
@.str.1 = [64 x i8] c"Usage: takes NUM_SAMPLES at SAMPLING_FREQ Hz from CHANNEL_MASK\0A\00"
@.str.2 = [50 x i8] c"ERROR: first argument must be a binary of 8 bits\0A\00"
@.str.3 = [51 x i8] c"ERROR: second argument must be a positive integer\0A\00"
@.str.4 = [58 x i8] c"ERROR: sampling frequency must be between 1 and 44100 Hz\0A\00"
@.str.5 = [54 x i8] c"ERROR: third argument must be a non-negative integer\0A\00"
@.str.6 = [63 x i8] c"Channel mask: %d\0ASampling frequency: %d\0ANumber of samples: %d\0A\00"
@.str.7 = [13 x i8] c"/dev/ttyS101\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.8 = [25 x i8] c"Unable to open /dev/S101\00"
@F_SETFL = global i32 0
@.str.9 = [16 x i8] c"Could not write\00"
@.str.10 = [15 x i8] c"Could not read\00"
@.str.11 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i64 @strtol(i8* %26, i8** %9, i32 2)
%28 = trunc i64 %27 to i8
store i8 %28, i8* %6
%29 = load i8*, i8** %9
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %23
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i64 @strtol(i8* %40, i8** %9, i32 10)
%42 = trunc i64 %41 to i32
store i32 %42, i32* %8
%43 = load i8*, i8** %9
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %37
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %37
%52 = load i32, i32* %8
%53 = icmp sle i32 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = load i32, i32* %8
%56 = icmp sgt i32 %55, 44100
br i1 %56, label %57, label %61
57: ; preds = %54, %51
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %54
br label %62
62: ; preds = %61
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 3
%65 = load i8*, i8** %64
%66 = call i64 @strtol(i8* %65, i8** %9, i32 10)
%67 = trunc i64 %66 to i32
store i32 %67, i32* %7
%68 = load i8*, i8** %9
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 0
br i1 %71, label %75, label %72
72: ; preds = %62
%73 = load i32, i32* %7
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %72, %62
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %72
%80 = load i32, i32* @stderr
%81 = load i8, i8* %6
%82 = sext i8 %81 to i32
%83 = load i32, i32* %8
%84 = load i32, i32* %7
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %82, i32 %83, i32 %84)
store i32 0, i32* %12
store i32 0, i32* %13
%86 = load i8, i8* %6
%87 = call i32 @count_channels(i8 signext %86)
store i32 %87, i32* %14
%88 = load i32, i32* @O_RDWR
%89 = load i32, i32* @O_NOCTTY
%90 = or i32 %88, %89
%91 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %90)
store i32 %91, i32* %10
%92 = load i32, i32* %10
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %97
94: ; preds = %79
%95 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%96 = call i32 @exit(i32 1)
unreachable
97: ; preds = %79
%98 = load i32, i32* %10
%99 = load i32, i32* @F_SETFL
%100 = call i32 @fcntl(i32 %98, i32 %99, i32 0)
%101 = load i32, i32* %10
%102 = call i64 (i32, ...) @write(i32 %101, i8* %6, i64 1)
%103 = icmp slt i64 %102, 0
br i1 %103, label %104, label %107
104: ; preds = %97
%105 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%106 = call i32 @exit(i32 1)
unreachable
107: ; preds = %97
%108 = load i32, i32* %10
%109 = call i64 (i32, ...) @write(i32 %108, i32* %8, i64 4)
%110 = icmp slt i64 %109, 0
br i1 %110, label %111, label %114
111: ; preds = %107
%112 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%113 = call i32 @exit(i32 1)
unreachable
114: ; preds = %107
%115 = load i32, i32* %10
%116 = call i64 (i32, ...) @write(i32 %115, i32* %7, i64 4)
%117 = icmp slt i64 %116, 0
br i1 %117, label %118, label %121
118: ; preds = %114
%119 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%120 = call i32 @exit(i32 1)
unreachable
121: ; preds = %114
br label %122
122: ; preds = %146, %121
%123 = load i32, i32* %7
%124 = icmp ne i32 %123, 0
br i1 %124, label %125, label %132
125: ; preds = %122
%126 = load i32, i32* %12
%127 = load i32, i32* %7
%128 = mul nsw i32 %127, 2
%129 = load i32, i32* %14
%130 = mul nsw i32 %128, %129
%131 = icmp slt i32 %126, %130
br label %132
132: ; preds = %125, %122
%133 = phi i1 [ true, %122 ], [ %131, %125 ]
br i1 %133, label %134, label %152
134: ; preds = %132
%135 = load i32, i32* %10
%136 = call i32 @read(i32 %135, i8* %11, i32 1)
store i32 %136, i32* %13
%137 = icmp slt i32 %136, 0
br i1 %137, label %138, label %146
138: ; preds = %134
%139 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%140 = load i32, i32* @stderr
%141 = load i32, i32* %13
%142 = call i32 (i32, i8*, ...) @fprintf(i32 %140, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i32 %141)
%143 = load i32, i32* %10
%144 = call i32 @close(i32 %143)
%145 = call i32 @exit(i32 1)
unreachable
146: ; preds = %134
%147 = load i32, i32* %12
%148 = load i32, i32* %13
%149 = add nsw i32 %147, %148
store i32 %149, i32* %12
%150 = load i32, i32* %13
%151 = call i64 (i32, ...) @write(i32 1, i8* %11, i32 %150)
br label %122
152: ; preds = %132
%153 = load i32, i32* %10
%154 = call i32 @close(i32 %153)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"Usage: sample CHANNEL_MASK SAMPLING_FREQ NUM_SAMPLES\0A\00"
@.str.1 = [64 x i8] c"Usage: takes NUM_SAMPLES at SAMPLING_FREQ Hz from CHANNEL_MASK\0A\00"
@.str.2 = [50 x i8] c"ERROR: first argument must be a binary of 8 bits\0A\00"
@.str.3 = [51 x i8] c"ERROR: second argument must be a positive integer\0A\00"
@.str.4 = [58 x i8] c"ERROR: sampling frequency must be between 1 and 44100 Hz\0A\00"
@.str.5 = [54 x i8] c"ERROR: third argument must be a non-negative integer\0A\00"
@.str.6 = [63 x i8] c"Channel mask: %d\0ASampling frequency: %d\0ANumber of samples: %d\0A\00"
@.str.7 = [13 x i8] c"/dev/ttyS101\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.8 = [25 x i8] c"Unable to open /dev/S101\00"
@F_SETFL = global i32 0
@.str.9 = [16 x i8] c"Could not write\00"
@.str.10 = [15 x i8] c"Could not read\00"
@.str.11 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
%8 = icmp eq i32 %0, 4
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = bitcast i32* %4 to i8*
%17 = bitcast i32* %5 to i8*
%18 = bitcast i8** %6 to i8*
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @strtol(i8* %20, i8** %6, i32 2)
%22 = trunc i64 %21 to i8
store i8 %22, i8* %3
%23 = load i8*, i8** %6
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = call i64 @strtol(i8* %32, i8** %6, i32 10)
%34 = trunc i64 %33 to i32
store i32 %34, i32* %5
%35 = load i8*, i8** %6
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %30
%43 = add i32 %34, -44101
%44 = icmp ult i32 %43, -44100
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %42
%50 = getelementptr inbounds i8*, i8** %1, i64 3
%51 = load i8*, i8** %50
%52 = call i64 @strtol(i8* %51, i8** %6, i32 10)
%53 = trunc i64 %52 to i32
store i32 %53, i32* %4
%54 = load i8*, i8** %6
%55 = load i8, i8* %54
%56 = icmp ne i8 %55, 0
%57 = icmp slt i32 %53, 0
%58 = select i1 %56, i1 true, i1 %57
%59 = load i32, i32* @stderr
br i1 %58, label %60, label %63
60: ; preds = %49
%61 = tail call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
%62 = tail call i32 @exit(i32 1)
unreachable
63: ; preds = %49
%64 = sext i8 %22 to i32
%65 = tail call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %64, i32 %34, i32 %53)
%66 = tail call i32 @count_channels(i8 signext %22)
%67 = load i32, i32* @O_RDWR
%68 = load i32, i32* @O_NOCTTY
%69 = or i32 %68, %67
%70 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %69)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %75
72: ; preds = %63
%73 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%74 = tail call i32 @exit(i32 1)
unreachable
75: ; preds = %63
%76 = load i32, i32* @F_SETFL
%77 = tail call i32 @fcntl(i32 %70, i32 %76, i32 0)
%78 = call i64 (i32, ...) @write(i32 %70, i8* %3, i64 1)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %75
%84 = call i64 (i32, ...) @write(i32 %70, i32* %5, i64 4)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %83
%90 = call i64 (i32, ...) @write(i32 %70, i32* %4, i64 4)
%91 = icmp slt i64 %90, 0
br i1 %91, label %99, label %92
92: ; preds = %89
%93 = shl i32 %66, 1
%94 = load i32, i32* %4
%95 = icmp eq i32 %94, 0
%96 = mul i32 %93, %94
%97 = icmp sgt i32 %96, 0
%98 = select i1 %95, i1 true, i1 %97
br i1 %98, label %102, label %120
99: ; preds = %89
%100 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%101 = call i32 @exit(i32 1)
unreachable
102: ; preds = %92, %112
%103 = phi i32 [ %113, %112 ], [ 0, %92 ]
%104 = call i32 @read(i32 %70, i8* %7, i32 1)
%105 = icmp slt i32 %104, 0
br i1 %105, label %106, label %112
106: ; preds = %102
%107 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%108 = load i32, i32* @stderr
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i32 %104)
%110 = call i32 @close(i32 %70)
%111 = call i32 @exit(i32 1)
unreachable
112: ; preds = %102
%113 = add nsw i32 %104, %103
%114 = call i64 (i32, ...) @write(i32 1, i8* %7, i32 %104)
%115 = load i32, i32* %4
%116 = icmp eq i32 %115, 0
%117 = mul i32 %93, %115
%118 = icmp slt i32 %113, %117
%119 = select i1 %116, i1 true, i1 %118
br i1 %119, label %102, label %120
120: ; preds = %112, %92
%121 = call i32 @close(i32 %70)
ret i32 0
}