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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freev(char **);
int /*<<< orphan*/ getenv(char *);
scalar_t__ log_init();
int /*<<< orphan*/ readcmd(int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
char **tokenize(int /*<<< orphan*/, char *);
int main(int argc, char *argv[]) {
  int ret = 0;
  char **pathv = tokenize(getenv("PATH"), ":");
  if (log_init())
    return -3;
  if (argc < 2) {
    readcmd(stdin, pathv);
  } else if (argc == 2) {
    FILE *bf = fopen(argv[1], "r");
    if (bf == NULL) {
      fprintf(stderr, "Error: Can't open %s.\n", argv[1]);
      return -2;
    }
    readcmd(bf, pathv);
    fclose(bf);
  } else {
    fprintf(stderr, "Error: Invalid arguments.\n");
    fprintf(stderr, "Usage: %s [batch_file]\n", argv[0]);
    ret = -1;
  }
  freev(pathv);
  return ret;
}


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)
lea    0xe5c(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
lea    0xe53(%rip),%rsi
call   1080 <tokenize@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <log_init@plt>
cmp    $0x0,%rax
je     11d8 <main+0x58>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12bd <main+0x13d>
cmpl   $0x2,-0x8(%rbp)
jge    11fa <main+0x7a>
mov    0x2dff(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <readcmd@plt>
jmp    12ae <main+0x12e>
cmpl   $0x2,-0x8(%rbp)
jne    126d <main+0xed>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf4(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1252 <main+0xd2>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12bd <main+0x13d>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <readcmd@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
jmp    12a9 <main+0x129>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    12ae <main+0x12e>
mov    -0x20(%rbp),%rdi
call   1070 <freev@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe66(%rip),%rsi
mov    %eax,%edi
call   1080 <tokenize@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <log_init@plt>
mov    $0xfffffffd,%ebx
test   %rax,%rax
jne    1244 <main+0xc4>
cmp    $0x1,%ebp
jg     11d9 <main+0x59>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rdi
mov    %r15,%rsi
call   1090 <readcmd@plt>
xor    %ebx,%ebx
jmp    123c <main+0xbc>
cmp    $0x2,%ebp
jne    120d <main+0x8d>
mov    0x8(%r14),%rdi
lea    0xe1e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1251 <main+0xd1>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1090 <readcmd@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
jmp    123c <main+0xbc>
mov    0x2dc4(%rip),%rbx
mov    (%rbx),%edi
lea    0xe03(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0xe0b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1070 <freev@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xda4(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xfffffffe,%ebx
jmp    1244 <main+0xc4>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@stdin = global i32* null
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [23 x i8] c"Error: Can't open %s.\0A\00"
@.str.4 = [27 x i8] c"Error: Invalid arguments.\0A\00"
@.str.5 = [24 x i8] c"Usage: %s [batch_file]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%9 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = call i8** @tokenize(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8** %10, i8*** %7
%11 = call i64 (...) @log_init()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -3, i32* %3
br label %56
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32*, i32** @stdin
%19 = load i8**, i8*** %7
%20 = call i32 @readcmd(i32* %18, i8** %19)
br label %52
21: ; preds = %14
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %43
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %8
%29 = load i32*, i32** %8
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %37
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i32 -2, i32* %3
br label %56
37: ; preds = %24
%38 = load i32*, i32** %8
%39 = load i8**, i8*** %7
%40 = call i32 @readcmd(i32* %38, i8** %39)
%41 = load i32*, i32** %8
%42 = call i32 @fclose(i32* %41)
br label %51
43: ; preds = %21
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @stderr
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %49)
store i32 -1, i32* %6
br label %51
51: ; preds = %43, %37
br label %52
52: ; preds = %51, %17
%53 = load i8**, i8*** %7
%54 = call i32 @freev(i8** %53)
%55 = load i32, i32* %6
store i32 %55, i32* %3
br label %56
56: ; preds = %52, %31, %13
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@stdin = global i32* null
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [23 x i8] c"Error: Can't open %s.\0A\00"
@.str.4 = [27 x i8] c"Error: Invalid arguments.\0A\00"
@.str.5 = [24 x i8] c"Usage: %s [batch_file]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8** @tokenize(i32 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i64 (...) @log_init()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %35
7: ; preds = %2
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = load i32*, i32** @stdin
%11 = tail call i32 @readcmd(i32* %10, i8** %4)
br label %32
12: ; preds = %7
%13 = icmp eq i32 %0, 2
br i1 %13, label %14, label %26
14: ; preds = %12
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %15
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %21)
br label %35
23: ; preds = %14
%24 = tail call i32 @readcmd(i32* %17, i8** %4)
%25 = tail call i32 @fclose(i32* %17)
br label %32
26: ; preds = %12
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %1
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %23, %26, %9
%33 = phi i32 [ 0, %9 ], [ 0, %23 ], [ -1, %26 ]
%34 = tail call i32 @freev(i8** %4)
br label %35
35: ; preds = %19, %2, %32
%36 = phi i32 [ %33, %32 ], [ -3, %2 ], [ -2, %19 ]
ret i32 %36
}