#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 */
char *dirname(char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  char *bname;
  char *t;
  char ws[256];
  bname = strdup(argv[0]);
  t = dirname(bname);
  if (strlen(t) >= 240) {
    fprintf(stderr, "%s: Command path too big\n", argv[0]);
    exit(1);
  }
  strncpy(ws, dirname(bname), 220);
  ws[240] = 0;
  free(bname);
  strcat(ws, "/sh");
  t = getenv("SHSQL");
  if (t == NULL)
    t = "postgres";
  else if (!(*t))
    t = "postgres";
  if (strlen(t) > 32) {
    fprintf(stderr, "%s: Invalid SHSQL value\n", argv[0]);
    exit(1);
  }
  strcat(ws, t);
  execvp(ws, argv);
  fprintf(stderr, "%s: Failed to start %s\n", argv[0], ws);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0xf0,%eax
jl     1212 <main+0x72>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    -0x128(%rbp),%rdi
mov    %rax,%rsi
mov    $0xdc,%edx
call   1050 <strncpy@plt>
movb   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
lea    -0x120(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   10a0 <strcat@plt>
lea    0xdba(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1288 <main+0xe8>
lea    0xda5(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    12a5 <main+0x105>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12a0 <main+0x100>
lea    0xd88(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    12a5 <main+0x105>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x20,%eax
jle    12df <main+0x13f>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <execvp@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    -0x120(%rbp),%rcx
lea    0xd29(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    (%rsi),%rdi
call   10c0 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%rdi
call   1060 <strlen@plt>
cmp    $0xf0,%eax
jl     11e9 <main+0x49>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe1c(%rip),%rsi
jmp    126b <main+0xcb>
mov    %rbx,%rdi
call   1070 <dirname@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0xdc,%edx
call   1050 <strncpy@plt>
movb   $0x0,0xf0(%rsp)
mov    %rbx,%rdi
call   1040 <free@plt>
lea    0xdff(%rip),%rsi
mov    %r15,%rdi
call   10a0 <strcat@plt>
lea    0xdf4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    123d <main+0x9d>
lea    0xde9(%rip),%rbx
jmp    124b <main+0xab>
cmpb   $0x0,(%rax)
lea    0xddd(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    $0x21,%eax
jl     127c <main+0xdc>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdc2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <execvp@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd9e(%rip),%rsi
mov    %r15,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"%s: Command path too big\0A\00"
@.str.1 = [4 x i8] c"/sh\00"
@.str.2 = [6 x i8] c"SHSQL\00"
@.str.3 = [9 x i8] c"postgres\00"
@.str.4 = [25 x i8] c"%s: Invalid SHSQL value\0A\00"
@.str.5 = [24 x i8] c"%s: Failed to start %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = call i8* @dirname(i8* %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 @strlen(i8* %15)
%17 = icmp sge i32 %16, 240
br i1 %17, label %18, label %25
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%27 = load i8*, i8** %6
%28 = call i8* @dirname(i8* %27)
%29 = call i32 @strncpy(i8* %26, i8* %28, i32 220)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 240
store i8 0, i8* %30
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%35 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %39
38: ; preds = %25
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %45
39: ; preds = %25
%40 = load i8*, i8** %7
%41 = load i8, i8* %40
%42 = icmp ne i8 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %39
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %44
44: ; preds = %43, %39
br label %45
45: ; preds = %44, %38
%46 = load i8*, i8** %7
%47 = call i32 @strlen(i8* %46)
%48 = icmp sgt i32 %47, 32
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 0
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %45
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%58 = load i8*, i8** %7
%59 = call i32 @strcat(i8* %57, i8* %58)
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%61 = load i8**, i8*** %5
%62 = call i32 @execvp(i8* %60, i8** %61)
%63 = load i32, i32* @stderr
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 0
%66 = load i8*, i8** %65
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%68 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %66, i8* %67)
%69 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"%s: Command path too big\0A\00"
@.str.1 = [4 x i8] c"/sh\00"
@.str.2 = [6 x i8] c"SHSQL\00"
@.str.3 = [9 x i8] c"postgres\00"
@.str.4 = [25 x i8] c"%s: Invalid SHSQL value\0A\00"
@.str.5 = [24 x i8] c"%s: Failed to start %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = load i8*, i8** %1
%6 = tail call i8* @strdup(i8* %5)
%7 = tail call i8* @dirname(i8* %6)
%8 = tail call i32 @strlen(i8* %7)
%9 = icmp sgt i32 %8, 239
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = tail call i8* @dirname(i8* %6)
%17 = call i32 @strncpy(i8* %4, i8* %16, i32 220)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 240
store i8 0, i8* %18
%19 = call i32 @free(i8* %6)
%20 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%21 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = load i8, i8* %21
%25 = icmp eq i8 %24, 0
%26 = select i1 %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %21
br label %27
27: ; preds = %15, %23
%28 = phi i8* [ %26, %23 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %15 ]
%29 = call i32 @strlen(i8* %28)
%30 = icmp sgt i32 %29, 32
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %1
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = call i32 @strcat(i8* %4, i8* %28)
%38 = call i32 @execvp(i8* %4, i8** %1)
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %1
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %40, i8* %4)
%42 = call i32 @exit(i32 1)
unreachable
}