#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 */
int /*<<< orphan*/ LOG_AUTHPRIV;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_ODELAY;
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
char *malloc(scalar_t__);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, scalar_t__);
int snprintf(char *, scalar_t__, char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, char *,
                          char const *, char *, char const *, char *);
char *ttyname(int /*<<< orphan*/);
int do_log(const char *from_user, const char *to_user, const char *cwd,
           const char *cmdpath, char **args) {
  int ret = 1;
  int len;
  char *cmd;
  char *tmp;
  char *cdn = get_current_dir_name();
  char *tty;
  cmd = malloc(strlen(cmdpath) + 1);
  if (!cmd) {
    perror("malloc cmd");
    ret = 0;
    goto out;
  }
  len = snprintf(cmd, strlen(cmdpath) + 1, "%s", cmdpath);
  args += 3;
  for (; *args != NULL; args++) {
    tmp = realloc(cmd, strlen(cmd) + strlen(*args) + 2);
    if (!tmp) {
      perror("realloc tmp");
      ret = 0;
      goto out;
    }
    cmd = tmp;
    len += snprintf(cmd + len, strlen(*args) + 2, " %s", *args);
  }
  openlog("runasuser", LOG_ODELAY, LOG_AUTHPRIV);
  tty = ttyname(0);
  syslog(LOG_INFO,
         "%s : TTY=%s ; EWD=%s ; PWD=%s ; USER=%s ; "
         "COMMAND=%s",
         from_user, (tty) ? tty + 5 : "(none)", cwd, cdn, to_user, cmd);
  closelog();
  free(cdn);
  free(cmd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<do_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    $0x0,%al
call   1070 <get_current_dir_name@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    121d <do_log+0x6d>
lea    0xdf4(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x2c(%rbp)
jmp    13d5 <do_log+0x225>
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x20(%rbp),%rcx
lea    0xdd4(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     131a <do_log+0x16a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdi
add    %rax,%rsi
add    $0x2,%rsi
call   10b0 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12c2 <do_log+0x112>
lea    0xd5a(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x2c(%rbp)
jmp    13d5 <do_log+0x225>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,%rsi
add    $0x2,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd19(%rip),%rdx
call   1060 <snprintf@plt>
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    1258 <do_log+0xa8>
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
mov    0x2cae(%rip),%rax
mov    (%rax),%edx
lea    0xce8(%rip),%rdi
call   10d0 <openlog@plt>
xor    %edi,%edi
call   10a0 <ttyname@plt>
mov    %rax,-0x50(%rbp)
mov    0x2c86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1376 <do_log+0x1c6>
mov    -0x50(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x88(%rbp)
jmp    1389 <do_log+0x1d9>
lea    0xcdd(%rip),%rax
mov    %rax,-0x88(%rbp)
jmp    1389 <do_log+0x1d9>
mov    -0x78(%rbp),%rdx
mov    -0x7c(%rbp),%edi
mov    -0x88(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x48(%rbp),%r9
mov    -0x10(%rbp),%r10
mov    -0x38(%rbp),%rax
lea    0xc77(%rip),%rsi
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1090 <syslog@plt>
mov    $0x0,%al
call   1040 <closelog@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_log>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rdx,0x18(%rsp)
mov    %rsi,0x20(%rsp)
mov    %rdi,0x10(%rsp)
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <get_current_dir_name@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
add    $0x1,%r12
mov    %r12,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1333 <do_log+0x183>
mov    %rax,%r13
mov    %rbp,0x8(%rsp)
lea    0xe09(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
mov    0x18(%r15),%r12
test   %r12,%r12
je     129c <do_log+0xec>
mov    %eax,%r14d
add    $0x20,%r15
nopl   (%rax)
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r13,%rbx
mov    %rax,%r13
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%rsi
add    $0x2,%rsi
mov    %rbx,%rdi
call   10b0 <realloc@plt>
test   %rax,%rax
je     1341 <do_log+0x191>
mov    %rax,%r13
movslq %r14d,%r14
mov    %rax,%rbx
add    %r14,%rbx
mov    -0x8(%r15),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x2(%rax),%rsi
mov    %rbx,%rdi
lea    0xd92(%rip),%rdx
mov    %rbp,%rcx
call   1060 <snprintf@plt>
add    %eax,%r14d
mov    (%r15),%r12
add    $0x8,%r15
test   %r12,%r12
jne    1230 <do_log+0x80>
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
mov    0x2d2c(%rip),%rax
mov    (%rax),%edx
lea    0xd66(%rip),%rdi
call   10d0 <openlog@plt>
xor    %edi,%edi
call   10a0 <ttyname@plt>
mov    0x2d08(%rip),%rcx
mov    (%rcx),%edi
lea    0x5(%rax),%rdx
test   %rax,%rax
lea    0xd82(%rip),%rcx
cmovne %rdx,%rcx
lea    0xd42(%rip),%rsi
mov    0x10(%rsp),%rdx
mov    0x18(%rsp),%r8
mov    0x8(%rsp),%rbx
mov    %rbx,%r9
push   %r13
push   0x28(%rsp)
call   1090 <syslog@plt>
add    $0x10,%rsp
xor    %eax,%eax
call   1040 <closelog@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc6(%rip),%rdi
call   10c0 <perror@plt>
jmp    1321 <do_log+0x171>
lea    0xcc3(%rip),%rdi
call   10c0 <perror@plt>
xor    %r14d,%r14d
jmp    1321 <do_log+0x171>

LLVM IR (-O0):
@.str = [11 x i8] c"malloc cmd\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [12 x i8] c"realloc tmp\00"
@.str.3 = [4 x i8] c" %s\00"
@.str.4 = [10 x i8] c"runasuser\00"
@LOG_ODELAY = global i32 0
@LOG_AUTHPRIV = global i32 0
@LOG_INFO = global i32 0
@.str.5 = [53 x i8] c"%s : TTY=%s ; EWD=%s ; PWD=%s ; USER=%s ; COMMAND=%s\00"
@.str.6 = [7 x i8] c"(none)\00"
define i32 @do_log(i8* %0, i8* %1, i8* %2, i8* %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8** %4, i8*** %10
store i32 1, i32* %11
%17 = call i8* (...) @get_current_dir_name()
store i8* %17, i8** %15
%18 = load i8*, i8** %9
%19 = call i64 @strlen(i8* %18)
%20 = add nsw i64 %19, 1
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %13
%22 = load i8*, i8** %13
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %5
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %11
br label %96
26: ; preds = %5
%27 = load i8*, i8** %13
%28 = load i8*, i8** %9
%29 = call i64 @strlen(i8* %28)
%30 = add nsw i64 %29, 1
%31 = load i8*, i8** %9
%32 = call i32 @snprintf(i8* %27, i64 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 %32, i32* %12
%33 = load i8**, i8*** %10
%34 = getelementptr inbounds i8*, i8** %33, i64 3
store i8** %34, i8*** %10
br label %35
35: ; preds = %68, %26
%36 = load i8**, i8*** %10
%37 = load i8*, i8** %36
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %71
39: ; preds = %35
%40 = load i8*, i8** %13
%41 = load i8*, i8** %13
%42 = call i64 @strlen(i8* %41)
%43 = load i8**, i8*** %10
%44 = load i8*, i8** %43
%45 = call i64 @strlen(i8* %44)
%46 = add nsw i64 %42, %45
%47 = add nsw i64 %46, 2
%48 = call i8* @realloc(i8* %40, i64 %47)
store i8* %48, i8** %14
%49 = load i8*, i8** %14
%50 = icmp ne i8* %49, null
br i1 %50, label %53, label %51
51: ; preds = %39
%52 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %11
br label %96
53: ; preds = %39
%54 = load i8*, i8** %14
store i8* %54, i8** %13
%55 = load i8*, i8** %13
%56 = load i32, i32* %12
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8**, i8*** %10
%60 = load i8*, i8** %59
%61 = call i64 @strlen(i8* %60)
%62 = add nsw i64 %61, 2
%63 = load i8**, i8*** %10
%64 = load i8*, i8** %63
%65 = call i32 @snprintf(i8* %58, i64 %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %12
%67 = add nsw i32 %66, %65
store i32 %67, i32* %12
br label %68
68: ; preds = %53
%69 = load i8**, i8*** %10
%70 = getelementptr inbounds i8*, i8** %69, i32 1
store i8** %70, i8*** %10
br label %35
71: ; preds = %35
%72 = load i32, i32* @LOG_ODELAY
%73 = load i32, i32* @LOG_AUTHPRIV
%74 = call i32 @openlog(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %72, i32 %73)
%75 = call i8* @ttyname(i32 0)
store i8* %75, i8** %16
%76 = load i32, i32* @LOG_INFO
%77 = load i8*, i8** %6
%78 = load i8*, i8** %16
%79 = icmp ne i8* %78, null
br i1 %79, label %80, label %83
80: ; preds = %71
%81 = load i8*, i8** %16
%82 = getelementptr inbounds i8, i8* %81, i64 5
br label %84
83: ; preds = %71
br label %84
84: ; preds = %83, %80
%85 = phi i8* [ %82, %80 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %83 ]
%86 = load i8*, i8** %8
%87 = load i8*, i8** %15
%88 = load i8*, i8** %7
%89 = load i8*, i8** %13
%90 = call i32 @syslog(i32 %76, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %77, i8* %85, i8* %86, i8* %87, i8* %88, i8* %89)
%91 = call i32 (...) @closelog()
%92 = load i8*, i8** %15
%93 = call i32 @free(i8* %92)
%94 = load i8*, i8** %13
%95 = call i32 @free(i8* %94)
br label %96
96: ; preds = %84, %51, %24
%97 = load i32, i32* %11
ret i32 %97
}

LLVM IR (-O2):
@.str = [11 x i8] c"malloc cmd\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [12 x i8] c"realloc tmp\00"
@.str.3 = [4 x i8] c" %s\00"
@.str.4 = [10 x i8] c"runasuser\00"
@LOG_ODELAY = global i32 0
@LOG_AUTHPRIV = global i32 0
@LOG_INFO = global i32 0
@.str.5 = [53 x i8] c"%s : TTY=%s ; EWD=%s ; PWD=%s ; USER=%s ; COMMAND=%s\00"
@.str.6 = [7 x i8] c"(none)\00"
define i32 @do_log(i8* %0, i8* %1, i8* %2, i8* %3, i8** nocapture %4) {
%6 = tail call i8* (...) @get_current_dir_name()
%7 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %56
13: ; preds = %5
%14 = tail call i32 @snprintf(i8* %9, i64 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%15 = getelementptr inbounds i8*, i8** %4, i64 3
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %42, label %18
18: ; preds = %13, %31
%19 = phi i8* [ %40, %31 ], [ %16, %13 ]
%20 = phi i8** [ %39, %31 ], [ %15, %13 ]
%21 = phi i32 [ %38, %31 ], [ %14, %13 ]
%22 = phi i8* [ %27, %31 ], [ %9, %13 ]
%23 = tail call i64 @strlen(i8* %22)
%24 = tail call i64 @strlen(i8* %19)
%25 = add i64 %23, 2
%26 = add i64 %25, %24
%27 = tail call i8* @realloc(i8* %22, i64 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %56
31: ; preds = %18
%32 = sext i32 %21 to i64
%33 = getelementptr inbounds i8, i8* %27, i64 %32
%34 = load i8*, i8** %20
%35 = tail call i64 @strlen(i8* dereferenceable(1) %34)
%36 = add nsw i64 %35, 2
%37 = tail call i32 @snprintf(i8* %33, i64 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%38 = add nsw i32 %37, %21
%39 = getelementptr inbounds i8*, i8** %20, i64 1
%40 = load i8*, i8** %39
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %18
42: ; preds = %31, %13
%43 = phi i8* [ %9, %13 ], [ %27, %31 ]
%44 = load i32, i32* @LOG_ODELAY
%45 = load i32, i32* @LOG_AUTHPRIV
%46 = tail call i32 @openlog(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 %45)
%47 = tail call i8* @ttyname(i32 0)
%48 = load i32, i32* @LOG_INFO
%49 = icmp eq i8* %47, null
%50 = getelementptr inbounds i8, i8* %47, i64 5
%51 = select i1 %49, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* %50
%52 = tail call i32 @syslog(i32 %48, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %51, i8* %2, i8* %6, i8* %1, i8* %43)
%53 = tail call i32 (...) @closelog()
%54 = tail call i32 @free(i8* %6)
%55 = tail call i32 @free(i8* %43)
br label %56
56: ; preds = %42, %29, %11
%57 = phi i32 [ 0, %29 ], [ 1, %42 ], [ 0, %11 ]
ret i32 %57
}