#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;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
struct passwd *getpwnam(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv) {
  struct passwd *pwd;
  char user[] = "cvmfs-test";
  char buf[4096];
  char *p = buf;
  int i = 1;
  int setuid_success;
  pwd = getpwnam(user);
  setuid_success = setuid(pwd->pw_uid);
  if (setuid_success == 0) {
    memset(buf, 0, sizeof(buf));
    while (argv[i]) {
      p += sprintf(p, " %s", argv[i++]);
    }
    system(buf);
  } else {
    printf("Cannot setuid to cvmfs-test. Exiting.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe80(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe7d(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe75(%rip),%al
mov    %al,-0x19(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1038(%rbp)
movl   $0x1,-0x103c(%rbp)
lea    -0x23(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1080 <setuid@plt>
mov    %eax,-0x1040(%rbp)
cmpl   $0x0,-0x1040(%rbp)
jne    125f <main+0xff>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movslq -0x103c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     124e <main+0xee>
mov    -0x1038(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x103c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x103c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xddd(%rip),%rsi
call   1070 <sprintf@plt>
mov    %eax,%ecx
mov    -0x1038(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1038(%rbp)
jmp    11ee <main+0x8e>
lea    -0x1030(%rbp),%rdi
call   1030 <system@plt>
jmp    126b <main+0x10b>
lea    0xda9(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r14
movabs $0x65742d73666d7663,%rax
2d 74 65
mov    %rax,(%rsp)
movl   $0x747365,0x7(%rsp)
mov    %rsp,%rdi
call   1060 <getpwnam@plt>
mov    (%rax),%edi
call   1080 <setuid@plt>
test   %eax,%eax
je     11a6 <main+0x46>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
jmp    11f6 <main+0x96>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0x8(%r14),%rdx
test   %rdx,%rdx
je     11ec <main+0x8c>
add    $0x10,%r14
lea    0xe3d(%rip),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <sprintf@plt>
cltq
add    %rax,%rbx
mov    (%r14),%rdx
add    $0x8,%r14
test   %rdx,%rdx
jne    11d0 <main+0x70>
lea    0x10(%rsp),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@__const.main.user = [11 x i8] c"cvmfs-test\00"
@.str = [4 x i8] c" %s\00"
@.str.1 = [39 x i8] c"Cannot setuid to cvmfs-test. Exiting.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca [11 x i8]
%8 = alloca [4096 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [11 x i8]* %7 to i8*
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
store i8* %13, i8** %9
store i32 1, i32* %10
%14 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%15 = call %struct.passwd* @getpwnam(i8* %14)
store %struct.passwd* %15, %struct.passwd** %6
%16 = load %struct.passwd*, %struct.passwd** %6
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @setuid(i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %47
22: ; preds = %2
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 4096)
br label %25
25: ; preds = %32, %22
%26 = load i8**, i8*** %5
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %44
32: ; preds = %25
%33 = load i8*, i8** %9
%34 = load i8**, i8*** %5
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
store i32 %36, i32* %10
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8*, i8** %34, i64 %37
%39 = load i8*, i8** %38
%40 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %9
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %9
br label %25
44: ; preds = %25
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%46 = call i32 @system(i8* %45)
br label %49
47: ; preds = %2
%48 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
ret i32 0
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@__const.main.user = [11 x i8] c"cvmfs-test\00"
@.str = [4 x i8] c" %s\00"
@str = [38 x i8] c"Cannot setuid to cvmfs-test. Exiting.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [11 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = call %struct.passwd* @getpwnam(i8* %5)
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @setuid(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %2
%13 = call i32 @memset(i8* %6, i32 0, i32 4096)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %28, label %17
17: ; preds = %12, %17
%18 = phi i64 [ %21, %17 ], [ 1, %12 ]
%19 = phi i8* [ %26, %17 ], [ %15, %12 ]
%20 = phi i8* [ %24, %17 ], [ %6, %12 ]
%21 = add nuw i64 %18, 1
%22 = call i32 @sprintf(i8* dereferenceable(1) %20, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %19)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = getelementptr inbounds i8*, i8** %1, i64 %21
%26 = load i8*, i8** %25
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %17
28: ; preds = %17, %12
%29 = call i32 @system(i8* %6)
br label %32
30: ; preds = %2
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
ret i32 0
}