#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
scalar_t__ execvp(char *, char **);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ setuid(int /*<<< orphan*/);
int sudo(char *user, char *cmd, char *argv[]) {
  struct passwd *pw = getpwnam(user);
  if (NULL == pw) {
    printf("%s does not exist\n", user);
    return 1;
  }
  if (0 != setuid(pw->pw_uid)) {
    perror("setuid");
    return 2;
  }
  if (0 != execvp(cmd, argv)) {
    perror("execvp");
    return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sudo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1199 <sudo+0x49>
mov    -0x10(%rbp),%rsi
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1204 <sudo+0xb4>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1070 <setuid@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11ca <sudo+0x7a>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1204 <sudo+0xb4>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <execvp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11fd <sudo+0xad>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1204 <sudo+0xb4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sudo>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <getpwnam@plt>
test   %rax,%rax
je     119c <sudo+0x4c>
mov    (%rax),%edi
call   1070 <setuid@plt>
test   %rax,%rax
jne    11b6 <sudo+0x66>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11b0 <sudo+0x60>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%eax
jmp    11b0 <sudo+0x60>
lea    0xe5d(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
jmp    11b0 <sudo+0x60>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@.str = [19 x i8] c"%s does not exist\0A\00"
@.str.1 = [7 x i8] c"setuid\00"
@.str.2 = [7 x i8] c"execvp\00"
define i32 @sudo(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca %struct.passwd*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%9 = load i8*, i8** %5
%10 = call %struct.passwd* @getpwnam(i8* %9)
store %struct.passwd* %10, %struct.passwd** %8
%11 = load %struct.passwd*, %struct.passwd** %8
%12 = icmp eq %struct.passwd* null, %11
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %4
br label %32
16: ; preds = %3
%17 = load %struct.passwd*, %struct.passwd** %8
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @setuid(i32 %19)
%21 = icmp ne i64 0, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %4
br label %32
24: ; preds = %16
%25 = load i8*, i8** %6
%26 = load i8**, i8*** %7
%27 = call i64 @execvp(i8* %25, i8** %26)
%28 = icmp ne i64 0, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %4
br label %32
31: ; preds = %24
store i32 0, i32* %4
br label %32
32: ; preds = %31, %29, %22, %13
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@.str = [19 x i8] c"%s does not exist\0A\00"
@.str.1 = [7 x i8] c"setuid\00"
@.str.2 = [7 x i8] c"execvp\00"
define i32 @sudo(i8* %0, i8* %1, i8** %2) {
%4 = tail call %struct.passwd* @getpwnam(i8* %0)
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
8: ; preds = %3
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @setuid(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
15: ; preds = %8
%16 = tail call i64 @execvp(i8* %1, i8** %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %15, %18, %13, %6
%21 = phi i32 [ 1, %6 ], [ 2, %13 ], [ 3, %18 ], [ 0, %15 ]
ret i32 %21
}